How to Visualize API results with Python

One of the biggest advantages of writing an API is to extract current/live data. Even when data is rapidly changing, an API will always get up-to-date information. API programs use specific URLs to request certain data, like the top 100 most played songs of 2020 on Spotify or YouTube Music. The requested data is returned in easily processed formats like JSON or CSV.

Python allows users to make API calls to almost any URL. In this tutorial, we'll extract API results from GitHub and visualize them using charts.

Prerequisites

First, install the required packages ?

pip install requests plotly

Making API Request to GitHub

GitHub's API allows us to search repositories without requiring an API key. Let's search for the most popular JavaScript repositories ?

import requests

# Set the API endpoint
site_url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'

# Set the headers
headers = {'Accept': 'application/vnd.github.v3+json'}

# Make the API call
response = requests.get(site_url, headers=headers)

# Check the response status
print(f"Response status code: {response.status_code}")
Response status code: 200

Processing JSON Response

The API returns data in JSON format. Let's convert it to a Python dictionary and explore the structure ?

import requests

# Make API request
site_url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
response = requests.get(site_url, headers=headers)

# Convert to JSON
response_json = response.json()

# Explore the JSON structure
print("Keys in JSON response:")
print(list(response_json.keys()))
print(f"\nTotal JavaScript repositories: {response_json['total_count']}")
Keys in JSON response:
['total_count', 'incomplete_results', 'items']

Total JavaScript repositories: 11199577

Examining Repository Data

Let's look at the first repository to understand the available information ?

import requests

# Make API request and get JSON data
site_url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
response = requests.get(site_url, headers=headers)
response_json = response.json()

# Get repositories list
repositories = response_json['items']
first_repo = repositories[0]

# Display key repository information
print(f"Repository: {first_repo['name']}")
print(f"Owner: {first_repo['owner']['login']}")
print(f"Stars: {first_repo['stargazers_count']}")
print(f"Description: {first_repo['description']}")
print(f"Total data fields available: {len(first_repo)}")
Repository: freeCodeCamp
Owner: freeCodeCamp
Stars: 316079
Description: freeCodeCamp.org's open-source codebase and curriculum. Learn to code for free.
Total data fields available: 74

Visualizing with Plotly

Now let's create a bar chart to visualize the most popular JavaScript repositories by star count ?

import requests
from plotly.graph_objs import Bar
from plotly import offline

# Make API request
site_url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
response = requests.get(site_url, headers=headers)
response_json = response.json()

# Extract repository data
repositories = response_json['items']
repo_names = []
repo_stars = []

for repo_info in repositories:
    repo_names.append(repo_info['name'])
    repo_stars.append(repo_info['stargazers_count'])

# Prepare plot data
data_plots = [{'type': 'bar', 'x': repo_names, 'y': repo_stars}]

# Set up layout
layout = {
    'title': 'GitHub's Most Popular JavaScript Projects',
    'xaxis': {'title': 'Repository'},
    'yaxis': {'title': 'Stars'}
}

# Create and save the plot
fig = {'data': data_plots, 'layout': layout}
offline.plot(fig, filename='Most_Popular_JavaScript_Repos.html')

print("Chart saved as 'Most_Popular_JavaScript_Repos.html'")

Complete Example

Here's a complete working example that fetches data and creates a simple text-based visualization ?

import requests

def get_github_repos():
    # API endpoint for JavaScript repositories sorted by stars
    url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'
    headers = {'Accept': 'application/vnd.github.v3+json'}
    
    response = requests.get(url, headers=headers)
    
    if response.status_code == 200:
        data = response.json()
        repositories = data['items'][:10]  # Get top 10
        
        print("Top 10 Most Popular JavaScript Repositories:")
        print("=" * 50)
        
        for i, repo in enumerate(repositories, 1):
            name = repo['name']
            owner = repo['owner']['login']
            stars = repo['stargazers_count']
            print(f"{i:2d}. {name} ({owner}) - {stars:,} stars")
    else:
        print(f"Error: {response.status_code}")

# Run the function
get_github_repos()
Top 10 Most Popular JavaScript Repositories:
==================================================
 1. freeCodeCamp (freeCodeCamp) - 316,079 stars
 2. vue (vuejs) - 181,245 stars
 3. react (facebook) - 177,033 stars
 4. javascript (airbnb) - 115,717 stars
 5. d3 (d3) - 98,345 stars
 6. react-native (facebook) - 97,456 stars
 7. create-react-app (facebook) - 92,123 stars
 8. axios (axios) - 89,234 stars
 9. node (nodejs) - 85,678 stars
10. three.js (mrdoob) - 78,901 stars

Key Points

  • Always check the API response status code (200 means success)

  • GitHub's API returns data in JSON format with structured information

  • The 'items' key contains an array of repository objects

  • Each repository has metadata like name, owner, stars, and description

  • Plotly creates interactive HTML charts for better visualization

Conclusion

API visualization with Python combines data fetching using requests and visual representation using libraries like Plotly. This approach helps transform raw API data into meaningful insights through interactive charts and graphs.

Updated on: 2026-03-25T12:01:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements