परिचय
API लाइव डेटा निकालने का शक्तिशाली तरीका है। Python से लगभग किसी भी API को कॉल कर सकते हैं। यहां GitHub API से JavaScript रिपॉजिटरी डेटा लेकर Plotly से विज़ुअलाइज़ करेंगे।
चरण
1. Requests इंस्टॉल करें
pip install requests plotly
2. GitHub API कॉल करें
import requests
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)
print(f"Response status: {response.status_code}") # 200
response_json = response.json()
print(f"Keys: {response_json.keys()}")
print(f"Total JS repos: {response_json['total_count']}") # ~11 मिलियन
3. पहले रिपॉजिटरी की जांच करें
repositories = response_json['items']
first_repo = repositories[0]
print(f"Name: {first_repo['name']}")
print(f"Owner: {first_repo['owner']['login']}")
print(f"Stars: {first_repo['stargazers_count']}")
print(f"Watchers: {first_repo['watchers_count']}")
4. डेटा निकालें (नाम और स्टार्स)
repo_names = []
repo_stars = []
for repo in repositories:
repo_names.append(repo['name'])
repo_stars.append(repo['stargazers_count'])
5. Plotly से बार चार्ट बनाएं
from plotly.graph_objs import Bar
from plotly import offline
data_plots = [{
'type': 'bar',
'x': repo_names,
'y': repo_stars,
'marker': {'color': 'rgb(60, 100, 150)', 'line': {'width': 1.5, 'color': 'rgb(25, 25, 25)'}},
'opacity': 0.6
}]
layout = {
'title': 'GitHub के सबसे लोकप्रिय JavaScript प्रोजेक्ट्स',
'xaxis': {'title': 'रिपॉजिटरी'},
'yaxis': {'title': 'स्टार्स'},
}
fig = {'data': data_plots, 'layout': layout}
offline.plot(fig, filename='Most_Popular_JavaScript_Repos.html')
6. परिणाम
Most_Popular_JavaScript_Repos.html फाइल बनेगी जिसे ब्राउज़र में खोलकर इंटरैक्टिव चार्ट देख सकते हैं (होवर, ज़ूम, पैन)।
पूर्ण कोड
import requests
from plotly.graph_objs import Bar
from plotly import offline
# API कॉल
url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
data = r.json()
# डेटा निकालें
names = [repo['name'] for repo in data['items']]
stars = [repo['stargazers_count'] for repo in data['items']]
# Plotly चार्ट
trace = {'type': 'bar', 'x': names, 'y': stars, 'marker': {'color': 'steelblue'}}
layout = {'title': 'टॉप JavaScript रिपॉजिटरी (स्टार्स द्वारा)',
'xaxis': {'title': 'रिपॉजिटरी'},
'yaxis': {'title': 'स्टार्स'}}
fig = {'data': [trace], 'layout': layout}
offline.plot(fig, filename='github_js_repos.html')
विस्तार विचार
- अधिक पेज लाने के लिए pagination (
&page=2) उपयोग करें। - अन्य भाषाओं (Python, Java, Go) के लिए दोहराएं और तुलना करें।
- फोर्क्स, इश्यूज, अंतिम अपडेट जैसे अन्य मेट्रिक्स विज़ुअलाइज़ करें।
- Plotly Express (
px.bar) से और सरल कोड।
नोट
GitHub API अनधिकृत कॉल के लिए प्रति घंटे 60 अनुरोध तक सीमित है। अधिक के लिए पर्सनल एक्सेस टोकन उपयोग करें।