第七课--github数据的可视化和仪表盘

汪鸿志
2023-12-01
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS

# 处理一个api
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)

print(r.status_code)
requests_dict = r.json()

print(requests_dict.keys())
# 处理响应字典
print('Total repositories:', requests_dict['total_count'])
# 仓库信息
repo_dicts = requests_dict['items']
# print(len(repo_dicts))
# 第一个仓库
# repo_dict = repo_dicts[0]
# print(len(repo_dict))
# for key in sorted(repo_dict.keys()):
#     print(key)
# print("\nSelected information about first repository:")
# print('Name:', repo_dict['name'])
# print('Owner:', repo_dict['owner']['login'])
# print('Stars:',repo_dict['stargazers_count'])
# print('Repositiory:', repo_dict['html_url'])
# print('Created',repo_dict['created_at'])
# print('Updated',repo_dict['updated_at'])
# print('Description',repo_dict['description'])
#
# # 最受欢迎的仓库
# print("\nSelected information about first repository:")
# for repo_dict in repo_dicts:
#     print('Name:', repo_dict['name'])
#     print('Owner:', repo_dict['owner']['login'])
#     print('Stars:', repo_dict['stargazers_count'])
#     print('Repositiory:', repo_dict['html_url'])
#     print('Created', repo_dict['created_at'])
#     print('Updated', repo_dict['updated_at'])
#     print('Description', repo_dict['description'])

# 可视化仓库
# names, stars = [], []
names, plot_dicts = [], []

for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    # stars.append(repo_dict['stargazers_count'])

    plot_dict = {
        'value': repo_dict['stargazers_count'],
        # 'label': repo_dict['description']
        'xlink':repo_dict['html_url']
    }
    plot_dicts.append(plot_dict)
# 可视化
my_style = LS('#333366', base_style=LCS)  # LS定义样式
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
# chart.add('Python', stars)
# chart.render_to_file('python_repos.svg')

# 改进图标
# my_config = pygal.Config()
# my_config.x_label_rotation = 45
# my_config.show_legend = False
# my_config.title_font_size = 24
# my_config.label_font_size = 14
# my_config.major_label_font_size = 18
# my_config.truncate_label = 15
# my_config.show_y_guides = False
# my_config.width = 1000
# chart = pygal.Bar(my_config, style=my_style)
# chart.title = 'Most-Starred Python Projects on GitHub'
# chart.x_labels = names
# chart.add('Python', stars)
# chart.render_to_file('python_repos1.svg')

# 自定义工具
# chart.x_labels = ['httpie', 'django', 'flask']
# plot_dicts = [
#     {'value': 16101, 'label': '爱吃糖甜品'},
#     {'value': 15028, 'label': '地建构'},
#     {'value': 14798, 'label': '弗拉斯卡'}
# ]
# chart.add('',plot_dicts)
# chart.render_to_file('bar_descriptions.svg ')

# 根据数据绘图
chart.add('', plot_dicts)
chart.render_to_file('python_repos3.svg')
 类似资料: