使用API獲取數據并顯示熱門ruby應用

通過調用api可以得到網站上實時的數據并顯示出來。這里以github上的數據為例,得到star最多的ruby應用并通過柱狀圖顯示數據結果
首先看效果


Paste_Image.png

從圖上可以看出,最多的是rails,通過圖表顯示,直觀又方便。
代碼如下:

import requests
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
url = 'https://api.github.com/search/repositories?q=language:ruby&sort=stars'
r = requests.get(url)
if r.status_code == 200:
    response_dict = r.json()
    print("Total repositories:", response_dict['total_count'])
    repo_dicts = response_dict['items']

    names, stars = [], []
    print("Repositories returned:", len(repo_dicts))
    # 顯示第一個信息
    # repo_dict = repo_dicts[0]
    # print("\nkeys:",len(repo_dict))
    # print("name:", repo_dict['name'])
    # print("owner:", repo_dict['owner']['login'])
    for repo_dict in repo_dicts:
        print('\nname:', repo_dict['name'])
        print("owner:", repo_dict['owner']['login'])
        print("Stars:", repo_dict['stargazers_count'])
        print("Des:", repo_dict['description'])
        names.append(repo_dict['name'])
        stars.append(repo_dict['stargazers_count'])
    my_style = LS("#113366", 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.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'
    chart.x_labels = names
    chart.add('', stars)
    chart.render_to_file('python_repos.svg')

else:
    print("Get data error.")

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容