def get_most_starred_project(repository_dicts, names, plot_dicts):
"""获取点赞最多的项目信息"""
for repository_dict in repository_dicts:
names.append(repository_dict["name"])
# 将获赞数、描述和链接存入字典中
plot_dict = {}
star = repository_dict["stargazers_count"]
url = repository_dict["html_url"]
description = repository_dict["description"]
plot_dict["value"] = star
plot_dict["label"] = description
plot_dict["xlink"] = url
plot_dicts.append(plot_dict)
def drew_bar_graph(names, plot_dicts):
"""绘制GitHub托管项目条形图"""
# 使用LightenStyle定义条形图的样式
my_style = LS("#333366", base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = "Most-Starred Python Project on GitHub"
chart.x_labels = names
chart.add("", plot_dicts)
chart.render_to_file("most_starred_python_project.svg")
# 调用GitHub的API获取所有托管项目信息
url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
response = requests.get(url)
response_dict = response.json()
repository_dicts = response_dict["items"]
names = []
plot_dicts = []
get_most_starred_project(repository_dicts, names, plot_dicts)
drew_bar_graph(names, plot_dicts)
用pygal绘图时出现【AttributeError: 'NoneType' object has no attribute 'decode'】的错误提示。
这个提示原因时生成的SVG文件太大,有两种方式可以修复。
方法一
将条形图中的【“label”值】强制转换为字符串格式,修改代码如下:
方法二
截取部分内容显示,修改代码如下:
参考资料:https://blog.csdn.net/weixin_42427638/article/details/80640817
网友评论