File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from random import randint
2+
3+
4+
class RandomLanguage():
5+
""" Random language class for 'rand_language_repos.pyä """
6+
7+
def __init__(self):
8+
""" Initialize class attributes """
9+
self.initialize_language_attributes()
10+
11+
def initialize_language_attributes(self):
12+
""" Sets all right attributes for the random language. """
13+
14+
languages = ['python', 'javascript',
15+
'ruby', 'java', 'haskell', 'go', 'c']
16+
language_titles = ['Python', 'JavaScript',
17+
'Ruby', 'Java', 'Haskell', 'GO', 'C']
18+
colors = ['#3776ab', '#f7df1e', '#e0115f',
19+
'#339999', '#797979', '#00a7d0', '#3744a7']
20+
21+
random_index = randint(0, len(languages)-1)
22+
23+
self.language = languages[random_index]
24+
self.language_title = language_titles[random_index]
25+
self.language_color = colors[random_index]

0 commit comments

Comments
 (0)