-
Notifications
You must be signed in to change notification settings - Fork 0
/
gráficos_covid_brasil_confirmados_vs_recuperados (1).py
138 lines (103 loc) · 2.82 KB
/
gráficos_covid_brasil_confirmados_vs_recuperados (1).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import requests as r
url = 'https://api.covid19api.com/dayone/country/brazil'
resp = r.get(url)
resp.status_code
raw_data = resp.json()
raw_data[0]
final_data = []
for obs in raw_data:
final_data.append([obs['Confirmed'], obs['Deaths'], obs['Recovered'], obs['Active'], obs['Date']])
final_data.insert(0, ['Confirmados', 'obitos', 'recuperados', 'ativos', 'data'])
final_data
CONFIRMADOS = 0
OBITOS = 1
RECUPERADOS = 2
ATIVOS = 3
DATA = 4
for i in range(1, len(final_data)):
final_data[i][DATA] = final_data[i][DATA][:10]
final_data
import datetime as dt
import csv
with open('brasil-covid.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(final_data)
for i in range(1, len(final_data)):
final_data[i][DATA] = dt.datetime.strptime(final_data[i][DATA], '%Y-%m-%d')
final_data
def get_datasets(y, labels):
if type(y[0]) == list:
datasets = []
for i in range(len(y)):
datasets.append({
'label': labels[i],
'data': y[i]
})
return datasets
else:
return [
{
'label': labels[0],
'data': y
}
]
def set_title(title=''):
if title != '':
display = 'true'
else:
display = 'false'
return {
'title': title,
'display': display
}
def create_chart(x, y, labels, kind='bar', title=''):
datasets = get_datasets(y, labels)
options = set_title(title)
chart = {
'type': kind,
'data': {
'labels': x,
'datasets': datasets
},
'options': options
}
return chart
def get_api_chart(chart):
url_base = 'https://quickchart.io/chart'
resp = r.get(f'{url_base}?c={str(chart)}')
return resp.content
def save_image(path, content):
with open(path, 'wb') as image:
image.write(content)
from PIL import Image
from IPython.display import display
def display_image(path):
img_pil = Image.open(path)
display(img_pil)
y_data_1 = []
for obs in final_data[1::10]:
y_data_1.append(obs[CONFIRMADOS])
y_data_2 = []
for obs in final_data[1::10]:
y_data_2.append(obs[RECUPERADOS])
#y_data_3 = []
#for obs in final_data[1::10]:
# y_data_3.append(obs[OBITOS])
labels = ['Confirmados', 'Recuperados']
x = []
for obs in final_data[1::10]:
x.append(obs[DATA].strftime('%d/%m/%Y'))
chart = create_chart(x, [y_data_1, y_data_2], labels, title='Gráfico Confirmados Vs Recuperados')
chart_content = get_api_chart(chart)
save_image('grafico-covid.png', chart_content)
display_image('grafico-covid.png')
from urllib.parse import quote
def get_api_qrcode(link):
text = quote(link) # parsing do link para url
url_base = 'https://quickchart.io/qr'
resp = r.get(f'{url_base}?text={text}')
return resp.content
url_base = 'https://quickchart.io/chart'
link = f'{url_base}?c={str(chart)}'
save_image('qr-code.png', get_api_qrcode(link))
display_image('qr-code.png')