GitHub Contribution Graph to ASCII City Guide
In this article
Turning your GitHub contribution graph into an ASCII city is a fun way to visualize your coding activity as a skyline of buildings. Each column of the graph becomes a building whose height matches the number of contributions in that week. This guide shows you how to generate that ASCII art from your own contribution data using a simple script.
What You'll Need
- A GitHub account with some contribution history (public or private, but the data must be accessible).
- Python 3 installed on your computer (check with
python --version). - Basic familiarity with the command line (terminal or PowerShell).
- Optional:
requestslibrary for Python if you want to fetch data directly from GitHub's API.
Step-by-Step
- Get your contribution data. The easiest way is to use GitHub's public API. Visit
https://api.github.com/users/YOUR_USERNAME/contributions(replace YOUR_USERNAME) in your browser. You'll see a JSON array where each object represents a day with adateandcountfield. If you prefer, you can also scrape your profile page, but the API is cleaner. - Save the data to a file. Copy the JSON response and save it as
contributions.jsonin a folder where you'll write your script. If you want to automate the fetch, use a tool likecurlor Python'srequests. - Write the Python script. Create a new file named
ascii_city.py. The script will read the JSON, group contributions by week (Sunday to Saturday), and for each week compute the total contributions. Then it will map that total to a building height, using a scale factor (e.g., 1 contribution = 1 character, but you might want to cap it). - Parse the JSON. In your script, use Python's
jsonmodule to load the data. Loop through the list and extract thedateandcount. Convert the date string to a Pythondateobject usingdatetime.strptime. - Group by week. For each date, determine the week it belongs to. You can use
date.isocalendar()to get the ISO week number and year. Create a dictionary where the key is (year, week) and the value is the sum of counts for that week. - Determine building heights. For each week, take the total contributions and divide by a scaling factor to get a height. For example, if you want buildings to be between 1 and 10 characters tall, you could use
height = min(10, max(1, total // 2)). Adjust the factor based on your typical contribution volume. - Build the ASCII city. Create a list of strings, each representing a row of the city. Start from the tallest building and go down. For each row, for each week, if the building height is greater than or equal to the current row number, place a
#(or any character) to represent a building block; otherwise, use a space. After the buildings, add a ground line of_characters. - Print the result. Join the rows with newlines and print to the console. You can also write it to a text file if you prefer.
- Run the script. In your terminal, navigate to the folder containing the script and run
python ascii_city.py. You should see your ASCII city. - Customize. Experiment with different characters (like
@for windows,|for antennas) and scaling factors to make it look more like a city skyline.
Common Mistakes
- Ignoring time zones. GitHub's API returns dates in UTC. If you group by local weeks, you might get off-by-one errors. Stick to UTC or convert consistently.
- Not handling empty weeks. If you have weeks with zero contributions, your script might skip them, causing buildings to be missing. Make sure to include all weeks between your first and last contribution, even if they have zero counts.
- Using the wrong scale. If your contributions are very high (e.g., hundreds per week), a 1:1 scale will make buildings absurdly tall. Always cap the height to something reasonable.
- Misreading the API response. The API endpoint might require authentication for private contributions. If you get a 404, check your username or use a token with
read:userscope.
Tips
- Use a monospaced font when viewing the ASCII city, otherwise the alignment will be off.
- Add a legend below the city showing the scale (e.g., "Each # = 5 contributions").
- Try different characters for variety:
#,@,%, or even Unicode blocks like█. - If you have many years of data, consider grouping by month instead of week to keep the city width manageable.
- Save your script to a gist or repository so you can reuse it and share it.
Troubleshooting
- Script errors with "KeyError: 'count'". The API response might have a different structure if you're using a different endpoint. Check the JSON in your browser and adjust the field names.
- City looks like a flat line. Your contributions might be very low, or your scaling factor is too small. Lower the divisor in your height calculation.
- City is too wide. If you have many weeks, the city becomes long. Try grouping by month instead, or limit the range to the last 52 weeks.
- No output at all. Ensure your JSON file is in the same directory as the script, and the script has read permissions. Add print statements to debug.
- API returns 403 rate limit. If you're fetching data frequently, you might hit the rate limit. Use authentication or wait an hour.
Frequently Asked Questions
Can I do this without coding?
There are online tools that generate ASCII art from GitHub graphs, but they may not be customisable. Writing a small script gives you full control.
Does it work with private contributions?
Yes, if you authenticate with a token that has access to your private contributions. The API will include them in the counts.
Can I use this for other users' graphs?
Yes, just change the username in the API URL. Make sure the user hasn't disabled public contributions.
What if I have zero contributions in a week?
The script should still include that week as a building of height 0, which appears as a gap. That's fine for a city skyline.
How do I change the building character?
In the script, replace the # in the loop with any character you like, such as @ or █.
Can I make the city taller?
Yes, increase the maximum height in the scaling logic, or reduce the divisor to make each contribution count for more.