暫無描述

python_repos.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import requests
  2. import pygal
  3. from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
  4. # Make an API call, and store the response.
  5. url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
  6. r = requests.get(url)
  7. print("Status code:", r.status_code)
  8. # Store API response in a variable.
  9. response_dict = r.json()
  10. print("Total repositories:", response_dict['total_count'])
  11. # Explore information about the repositories.
  12. repo_dicts = response_dict['items']
  13. names, plot_dicts = [], []
  14. for repo_dict in repo_dicts:
  15. names.append(repo_dict['name'])
  16. plot_dict = {
  17. 'value': repo_dict['stargazers_count'],
  18. 'label': repo_dict['description'],
  19. 'xlink': repo_dict['html_url'],
  20. }
  21. plot_dicts.append(plot_dict)
  22. # Make visualization.
  23. my_style = LS('#333366', base_style=LCS)
  24. my_config = pygal.Config()
  25. my_config.x_label_rotation = 45
  26. my_config.show_legend = False
  27. my_config.title_font_size = 24
  28. my_config.label_font_size = 14
  29. my_config.major_label_font_size = 18
  30. my_config.truncate_label = 15
  31. my_config.show_y_guides = False
  32. my_config.width = 1000
  33. chart = pygal.Bar(my_config, style=my_style)
  34. chart.title = 'Most-Starred Python Projects on GitHub'
  35. chart.x_labels = names
  36. chart.add('', plot_dicts)
  37. chart.render_to_file('python_repos.svg')