|
| 1 | +import requests |
| 2 | +import pygal |
| 3 | +from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS |
| 4 | + |
| 5 | +# Make an API request and store response |
| 6 | +url = 'https://api..com/search/repositories?q=language:javascript&sort=stars' |
| 7 | +r = requests.get(url) |
| 8 | +print('API Request Status: ', r.status_code) |
| 9 | + |
| 10 | +# Store API response in a variable |
| 11 | +response_dict = r.json() |
| 12 | +print('Total repositories: ', response_dict['total_count']) |
| 13 | + |
| 14 | +# Explore information about the repositories |
| 15 | +repo_dicts = response_dict['items'] |
| 16 | + |
| 17 | +names, plot_dicts = [], [] |
| 18 | +for repo_dict in repo_dicts: |
| 19 | +names.append(repo_dict['name']) |
| 20 | + |
| 21 | +plot_dict = { |
| 22 | +'value': repo_dict['stargazers_count'], |
| 23 | +'label': repo_dict['description'], |
| 24 | +'xlink': repo_dict['html_url'], |
| 25 | +} |
| 26 | + |
| 27 | +plot_dicts.append(plot_dict) |
| 28 | + |
| 29 | +# Visualize with pygal bar chart |
| 30 | +my_style = LS('#733380', base_style=LCS) |
| 31 | +my_style.title_font_size = 24 |
| 32 | +my_style.label_font_size = 14 |
| 33 | +my_style.major_label_font_size = 18 |
| 34 | + |
| 35 | +my_config = pygal.Config() |
| 36 | +my_config.value_formatter = lambda y: f'{y:,.0f}' # format (123,456) |
| 37 | +my_config.x_label_rotation = 45 |
| 38 | +my_config.show_legend = False |
| 39 | +my_config.tooltip_fancy_mode = False |
| 40 | +my_config.truncate_label = 15 |
| 41 | +my_config.show_y_guides = False |
| 42 | +my_config.width = 1000 |
| 43 | + |
| 44 | +chart = pygal.Bar(my_config, style=my_style) |
| 45 | +chart.title = 'Most-Starred JavaScript Projects on ' |
| 46 | +chart.x_labels = names |
| 47 | + |
| 48 | +chart.add('', plot_dicts) |
| 49 | +chart.render_to_file('./working_with_apis/images/svg/javascript_repos.svg') |
0 commit comments