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