-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (58 loc) · 2.04 KB
/
main.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
import pandas as pd
import requests
import folium
from datetime import datetime
from flask import Flask, render_template
re = requests.get("https://corona.lmao.ninja/v2/countries?yesterday&sort")
re = re.json()
re = pd.DataFrame.from_dict(re)
country_info = re['countryInfo']
country_info = pd.json_normalize(country_info)
df = pd.concat([re, country_info], axis=1,sort=False)
df = df.drop('countryInfo', axis=1)
def top_countries(n=10):
by_country = df.groupby('country').sum()[['cases']]
top_five = by_country.nlargest(n, 'cases')[['cases']]
top_five['cases'] = top_five['cases'].apply(lambda x: "{:,}".format(x))
return top_five
top_country = top_countries()
def convert_time_stamp(time):
t = datetime.fromtimestamp(time / 1000.0)
s = t.strftime('%d-%b-%Y %H:%M:%S')
return s[:-3]
df['updated'] = df['updated'].apply(convert_time_stamp)
last_update = df['updated'][0]
df['recovered'] = df['recovered'].apply(lambda x: "{:,}".format(x))
df['cases'] = df['cases'].apply(lambda x: "{:,}".format(x))
df['deaths'] = df['deaths'].apply(lambda x: "{:,}".format(x))
m = folium.Map(
tiles="CartoDB positron",
location=[32,0],
zoom_start=2,
)
country_geo = 'world-countries.json'
folium.Choropleth (
geo_data= country_geo,
data=df,
columns=['lat', 'long'],
key_on='feature.id',
fill_color='YlGnBu',
fill_opacity=0.7,
line_opacity=0.2,
).add_to(m)
def circle_maker(x):
folium.Circle(location=[x[0],x[1]],
radius=10,
color="red",
popup='{}\n Cases: {}\n Deaths: {}\n Recovered: {}'.format(x[2], x[3], x[4], x[5])).add_to(m)
df[['lat','long','country','cases','deaths', 'recovered']].apply(lambda x:circle_maker(x),axis=1)
html_map = m._repr_html_()
app = Flask(__name__)
@app.route('/')
def home():
return render_template("home.html", top_country=top_country,update=last_update, map=html_map)
@app.route('/about')
def about():
return render_template("about.html")
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080, debug=True)