-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_repos.py
63 lines (51 loc) · 2.08 KB
/
python_repos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
import plotly.express as px
#执行api调用并查看响应
url = 'https://api.github.com/search/repositories'
url += '?q=language:python+sort:stars+stars:>10000'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
#将响应转换为字典
response_dict = r.json()
# print(response_dict.keys())
# print(f"Total repositories: {response_dict['total_count']}")
# print(f"Complete results: {not response_dict['incomplete_results']}")
#搜索有关仓库的信息
repo_dicts = response_dict['items']
# print(f"Repositories returned: {len(repo_dicts)}")
#研究第一个仓库
# repo_dict = repo_dicts[0]
# print(f"\nKeys: {len(repo_dict)}")
# for key in sorted(repo_dict.keys()):
# print(key)
#打印仓库的具体信息
# print("\nSelected information about each repository:")
# for repo_dict in repo_dicts:
# print(f"Name: {repo_dict['name']}")
# print(f"Owner: {repo_dict['owner']['login']}")
# print(f"Stars: {repo_dict['stargazers_count']}")
# print(f"Repository: {repo_dict['html_url']}")
# print(f"Created: {repo_dict['created_at']}")
# print(f"Updated: {repo_dict['updated_at']}")
# print(f"Description: {repo_dict['description']}")
repo_links, stars, hover_texts = [], [], []
for repo_dict in repo_dicts:
#将仓库名转换为链接
repo_name = repo_dict['name']
repo_url = repo_dict['html_url']
repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
repo_links.append(repo_link)
stars.append(repo_dict['stargazers_count'])
#创建悬停文本
owner = repo_dict['owner']['login']
description = repo_dict['description']
hover_text = f"{owner}<br />{description}"
hover_texts.append(hover_text)
#可视化
title = "Most-Starred Python Projects"
labels = {'x':'Repositories', 'y':'Stars'}
fig = px.bar(x=repo_links, y=stars, title=title, labels=labels, hover_name=hover_texts)
fig.update_layout(title_font_size=20, xaxis_title_font_size=10, yaxis_title_font_size=10)
fig.update_traces(marker_color='SteelBlue', marker_opacity=0.6)
fig.show()