使用Web API 和Pygal可视化GitHub最受欢迎的仓库

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("Status code", r.status_code)

# # 将API响应存储在一个变量中
response_dict = r.json()
print("Total respositories:", response_dict['total_count'])

# 探索有关仓库的信息
# response_dict 是个字典 ”items"是他的键,response_dict['items']是他的值
repo_dicts = response_dict['items']
names, plot_dicts = [], []

# 研究所有仓库
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    # 仓库的描述信息可能为空 ,空内容无法解码,做个条件判断
    if repo_dict['description'] == None:
        repo_dict['description'] = 'None'
    plot_dict ={
        # 根据value来判断高度
        "value":repo_dict['stargazers_count'],
        "label":repo_dict['description'],
        # 'xlink'可以把仓库链接加入到每个柱形中
        'xlink': repo_dict['html_url'],
        }
    plot_dicts.append(plot_dict)

# 使用LS定义了一种样式, 基色为深蓝色
my_style = LS('#333366', base_style=LCS)
# 创建一个Pygal类Config的实例
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False

# 设置标题、副标签和主标签字体(y轴5000的整数倍)的大小
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
# 将较长的项目显示为缩短的15个字符
my_config.truncate_label = 15
# 隐藏图标水平线
my_config.show_y_guides = False
my_config.width = 1000

# 创建条形图,传递my_config 和 my_style
chart = pygal.Bar( my_config,style=my_style)
chart.title = 'Most-Starred Pytohn Projects on Github'
chart.x_labels = names
# 标签设置位空字符串
chart.add('', plot_dicts)
print(plot_dicts)
chart.render_to_file('python_repos6.svg')



实现效果

猜你喜欢

转载自blog.csdn.net/w18306890492/article/details/82948195