Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import pandas as pd
2import numpy as np
3import time
4from flask import Flask
5from flask import jsonify
7#from werkzeug.middleware.proxy_fix import ProxyFix
10# -------------------------------------------------------------------------------
11app = Flask(__name__)
12app.logger.info('Start')
14_CACHE_GROUPBY = dict()
15_CACHE_RKI = dict()
16_CACHE_VALUES = dict()
17_CACHE_RKILINE = dict()
19# -------------------------------------------------------------------------------
20def toint(val):
21 try:
22 return int(val)
23 #except KeyError:
24 # return 0
25 except ValueError:
26 return 0
28# -------------------------------------------------------------------------------
29def loaddata():
30 #df=pd.read_csv("https://radamanthys.de/datasets/RKI/rki-20210308.csv", delimiter=",")
31 df=pd.read_csv("https://radamanthys.de/datasets/RKI/rki-20210324.csv", delimiter=",")
32 df['Meldedatum'] = pd.to_datetime(df.Meldedatum)
33 df['Refdatum'] = pd.to_datetime(df.Refdatum)
34 df['SummeFall'] = np.where(df.NeuerFall.isin([0,1]), df.AnzahlFall, 0)
35 df['SummeTod'] = np.where(df.NeuerTodesfall.isin([0,1]), df.AnzahlTodesfall, 0)
36 df['SummeGenesen'] = np.where(df.NeuGenesen.isin([0,1]), df.AnzahlGenesen, 0)
39 gro = pd.read_csv("https://radamanthys.de/datasets/DIVI/gemeindegroesse.csv")
40 c=pd.merge(df, gro, how='left', left_on="IdLandkreis", right_on="gemeindeschluessel")
41 c['FallPer100k'] = c.SummeFall * 100000 / c.einwohner
42 c['TodPer100k'] = c.SummeTod * 100000 / c.einwohner
43 c['GenesenPer100k'] = c.SummeGenesen * 100000 / c.einwohner
45 return c
47s=time.time()
48df = loaddata()
49app.logger.info('load time %s', (time.time()-s))
52# -------------------------------------------------------------------------------
53@app.route('/')
54def hello_world():
55 return 'Hello, World!'
58# -------------------------------------------------------------------------------
59@app.route('/lists/dates')
60def get_dates():
61 return jsonify(["Refdatum","Meldedatum"])
64# -------------------------------------------------------------------------------
65@app.route('/lists/groups')
66def get_groups():
67 return jsonify([None, "Geschlecht","Altersgruppe","Bundesland","Landkreis"])
70# -------------------------------------------------------------------------------
71@app.route('/lists/columns')
72def get_columns():
73 return jsonify(["Bundesland","Landkreis","Altersgruppe","Geschlecht"])
76# -------------------------------------------------------------------------------
77@app.route('/lists/values/<column>')
78def get_values(column):
79 r = _CACHE_VALUES.get(column)
80 if not r:
81 if column in df.columns:
82 d = sorted(list(df[column].unique()))
83 else:
84 d = list()
85 _CACHE_VALUES[column] = r = jsonify(d)
86 return r
88# -------------------------------------------------------------------------------
89@app.route('/lists/attributes')
90def get_attributes():
91 return jsonify(["SummeFall","SummeTod","SummeGenesen"])
94# -------------------------------------------------------------------------------
95@app.route('/rkiline/<datetype>/<column>/<value>')
96def rkiline(datetype, column, value):
97 r = _CACHE_RKILINE.get((datetype, column, value))
99 if not r:
101 #if column in ['Bundesland','Landkreis'] and value.find("Berlin")<0:
102 # bl = df[df[column]==value].groupby([datetype]).sum() [["FallPer100k","TodPer100k"]]
103 # d["title"] = { "text": f"{datetype} {column} {value} pro 100T Einwohner" }
104 #else:
105 # bl = df[df[column]==value].groupby([datetype]).sum() [["SummeFall","SummeTod"]]
106 # d["title"] = { "text": f"{datetype} {column} {value}" }
108 opt = dict()
110 opt["colors"] = ["#FF1654", "#247BA0"]
111 opt["stroke"] = {"width": [1,1]}
113 opt["yaxis"] = [
114 {
115 "title": { "text": "Fälle", "style": { "color": "#FF1654" } },
116 "labels": { "style": { "colors": "#FF1654" } },
117 "axisBorder": { "show": True, "color": "#FF1654"}
118 },
119 {
120 "opposite": True,
121 "title": { "text": "Todesfälle", "style": { "color": "#247BA0" } },
122 "labels": { "style": { "colors": "#247BA0" } },
123 "axisBorder": { "show": True, "color": "#247BA0"}
124 }
125 ]
126 opt["dataLabels"] = {"enabled": False}
128 bl = df[df[column]==value].groupby([datetype]).sum() [["SummeFall","SummeTod"]]
129 opt["title"] = { "text": f"{datetype} {column} {value}" }
131 bl = bl.rolling(7).mean()
132 opt["xaxis"] = dict()
133 opt["xaxis"]["categories"] = list(np.array([i.isoformat() for i in bl.index]))
135 d = dict()
136 d["options"] = opt
137 d["series"] = []
139 for col in bl.columns:
140 l = list()
141 for i in bl.index:
142 val = toint(bl.loc[i, col])
143 l.append(val)
145 d["series"].append( {"name": col, "data": l} )
146 r = d
147 _CACHE_RKILINE[(datetype, column, value)] = r = jsonify(d)
149 return r
151# -------------------------------------------------------------------------------
152@app.route('/rkibar/<attribute>/<groupby1>/<groupby2>')
153def rkibar(attribute, groupby1, groupby2):
155 r = _CACHE_RKI.get((attribute, groupby1, groupby2))
156 if not r:
157 if groupby2 == 'null':
158 ge=df.groupby([groupby1]).sum() [[attribute]]
159 d = dict()
160 d["chart"] = { "type": "bar", "height": "350px" }
161 d["title"] = { "text": f"{attribute} gruppiert nach {groupby1}" }
162 d["series"] = []
163 d["xaxis"] = list(ge.index)
165 l = list()
166 for i in ge.index:
167 val = toint(ge.loc[i, attribute])
168 l.append(val)
170 d["series"].append( {"name": attribute, "data": l} )
173 else:
174 ge=df.groupby([groupby1, groupby2]).sum() [[attribute]]
176 d = dict()
177 d["chart"] = { "type": "bar", "height": "350px" }
178 d["title"] = { "text": f"{attribute} gruppiert nach {groupby1} und {groupby2}" }
179 d["series"] = []
182 level1 = list(ge.index.get_level_values(0).unique())
183 level2 = list(ge.index.get_level_values(1).unique())
185 tmp = dict()
186 for index2 in level2:
187 #if index2 not in tmp:
188 tmp[index2] = []
189 for index1 in level1:
190 val = toint(ge.loc[(index1,index2), attribute])
191 tmp[index2].append(val)
194 d["xaxis"] = level1
195 for index2 in level2:
196 d["series"].append( {"name": index2, "data": tmp[index2]} )
198 _CACHE_RKI[(attribute, groupby1, groupby2)] = r = jsonify(d)
199 return r
201# -------------------------------------------------------------------------------
202#from gevent.pywsgi import WSGIServer
203#if __name__ == '__main__':
204# http_server = WSGIServer(('', 5000), app)
205# http_server.serve_forever()