来自Py的一个小问题,PLOTLY可视化GitHub里绘制统计图,网页无法显示统计结果 。

本人在学习《python编程从入门到实践》,在学习到用PLOTLY可视化GitHub里绘制统计图时,运行书上自带的代码,网页只打开自己设置的主页,而没有显示该有的画面!

P.S. 自己测试了一下之前写的代码是可以正常打开网页上的绘图页面的。

代码如下:

import requests
from plotly.graph_objs import Bar
from plotly import offline
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers={'Accept':'application/vnd.github.v3+json'}
r = requests.get(url,headers=headers)
print(f"Status code: {r.status_code}")
response_dict=r.json()
print(f"Total responsitories: {response_dict['total_count']}")
repo_dicts=response_dict['items']
repo_names,stars=[],[]
for repo_dict in repo_dicts:
    repo_names.append(repo_dict['name'])
    stars.append(repo_dict['stargazers_count'])
data=[{
    'type':'bar',
    'x':repo_names,
    'y':stars,
}]
my_layout={
    'title'    :'Github上最受欢迎的Python项目',
    'xaxis':{'title':'Repositories'},
    'yaxis':{'title':'Stars'},
}
fig={'data':data,'layout':my_layout}
offline.plot(fig,filename='python_repos.html')