Skip to content

Commit 28d54b3

Browse files
committed
visualize all data from github api
1 parent b99ca43 commit 28d54b3

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

python_repos.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import requests
2+
import plotly.express as px
23

34
#执行api调用并查看响应
45
url = 'https://api.github.com/search/repositories'
@@ -11,27 +12,45 @@
1112
#将响应转换为字典
1213
response_dict = r.json()
1314
# print(response_dict.keys())
14-
15-
print(f"Total repositories: {response_dict['total_count']}")
16-
print(f"Complete results: {not response_dict['incomplete_results']}")
15+
# print(f"Total repositories: {response_dict['total_count']}")
16+
# print(f"Complete results: {not response_dict['incomplete_results']}")
1717

1818
#搜索有关仓库的信息
1919
repo_dicts = response_dict['items']
20-
print(f"Repositories returned: {len(repo_dicts)}")
20+
# print(f"Repositories returned: {len(repo_dicts)}")
2121

2222
#研究第一个仓库
23-
repo_dict = repo_dicts[0]
23+
# repo_dict = repo_dicts[0]
2424
# print(f"\nKeys: {len(repo_dict)}")
2525
# for key in sorted(repo_dict.keys()):
2626
# print(key)
2727

28-
print("\nSelected information about each repository:")
28+
#打印仓库的具体信息
29+
# print("\nSelected information about each repository:")
30+
# for repo_dict in repo_dicts:
31+
# print(f"Name: {repo_dict['name']}")
32+
# print(f"Owner: {repo_dict['owner']['login']}")
33+
# print(f"Stars: {repo_dict['stargazers_count']}")
34+
# print(f"Repository: {repo_dict['html_url']}")
35+
# print(f"Created: {repo_dict['created_at']}")
36+
# print(f"Updated: {repo_dict['updated_at']}")
37+
# print(f"Description: {repo_dict['description']}")
38+
39+
repo_names, stars, hover_texts = [], [], []
2940
for repo_dict in repo_dicts:
30-
print(f"Name: {repo_dict['name']}")
31-
print(f"Owner: {repo_dict['owner']['login']}")
32-
print(f"Stars: {repo_dict['stargazers_count']}")
33-
print(f"Repository: {repo_dict['html_url']}")
34-
print(f"Created: {repo_dict['created_at']}")
35-
print(f"Updated: {repo_dict['updated_at']}")
36-
print(f"Description: {repo_dict['description']}")
37-
41+
repo_names.append(repo_dict['name'])
42+
stars.append(repo_dict['stargazers_count'])
43+
44+
#创建悬停文本
45+
owner = repo_dict['owner']['login']
46+
description = repo_dict['description']
47+
hover_text = f"{owner}<br />{description}"
48+
hover_texts.append(hover_text)
49+
50+
51+
#可视化
52+
title = "Most-Starred Python Projects"
53+
labels = {'x':'Repositories', 'y':'Stars'}
54+
fig = px.bar(x=repo_names, y=stars, title=title, labels=labels, hover_name=hover_texts)
55+
fig.update_layout(title_font_size=20, xaxis_title_font_size=10, yaxis_title_font_size=10)
56+
fig.show()

0 commit comments

Comments
 (0)