欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第十七章 使用API

程序员文章站 2024-01-28 16:10:40
...

第十七章 使用API

17.1使用web API(有待修改)

import requests

url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print(r.status_code)

response_dict = r.json()

17.2使用Pygal可视化仓库

import os
import pygal
import json
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
path = "/Users/anchror/Documents/python/20200305"   
os.chdir(path)

file_name='repositories.json'
with open(file_name) as f:
	requests_dict=json.load(f)


repo_dicts = requests_dict['items']
names, stars = [], []
for repo_dict in repo_dicts:
	names.append(repo_dict['name'])
	stars.append(repo_dict['stargazers_count'])

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 projects'
chart.x_labels = names

chart.add('', stars)
chart.render_to_file('python_repos.svg')

第十七章 使用API

17.3改进Pygal图表

import os
import pygal
import json
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
path = "/Users/anchror/Documents/python/20200305"   
os.chdir(path)

file_name='repositories.json'
with open(file_name) as f:
	requests_dict=json.load(f)


repo_dicts = requests_dict['items']
names, plot_dicts = [], []
for repo_dict in repo_dicts:
	names.append(repo_dict['name'])
	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)
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False 
my_config.title_font_size = 24
my_config.major_label_font_size =18 # 坐标字体
my_config.truncate_label =15 # 名称缩短为15个字符
my_config.show_y_guides = Fales # 隐藏X轴
my_config.width = 1000
# chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'most-starred python projects'
chart.x_labels = names

chart.add('', plot_dicts)
chart.render_to_file('python_repos_full.svg')

第十七章 使用API