Add route for single states

This commit is contained in:
Felix Blanke 2023-09-01 14:47:13 +02:00
parent aaf4ee1863
commit d3c424d90a

51
wsgi.py
View File

@ -11,10 +11,10 @@ import matplotlib.pyplot as plt
import matplotlib.ticker as mtick import matplotlib.ticker as mtick
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from flask import Flask, Markup, render_template, request from flask import Flask, Markup, abort, render_template, request
from flask_caching import Cache from flask_caching import Cache
from download_digital import construct_dataframe, get_bez_data, get_landesbezirk from download_digital import construct_dataframe, get_bez_data, get_landesbezirk, landesbezirk_dict
config = { config = {
"CACHE_TYPE": "FileSystemCache", "CACHE_TYPE": "FileSystemCache",
@ -23,6 +23,14 @@ config = {
"CACHE_DIR": "cache", "CACHE_DIR": "cache",
} }
abbrev_dict = {
"BBR": "Berlin-Brandenburg",
"BaWü": "Baden-Württemberg",
"NRW": "Nordrhein-Westfalen",
"RLP": "Rheinland-Pfalz-Saarland",
"SAT": "Sachsen, Sachsen-Anhalt, Thüringen",
}
os.environ["TZ"] = "Europe/Berlin" os.environ["TZ"] = "Europe/Berlin"
time.tzset() time.tzset()
@ -312,9 +320,46 @@ def _print_as_html(df: pd.DataFrame, output_str: list[str], total: int | None =
return output_str return output_str
@app.route("/<state>")
@cache.cached(query_string=True)
def state_dashboard(state: str):
if state in abbrev_dict:
state = abbrev_dict[state]
if state not in landesbezirk_dict.values():
abort(404)
importance_factor = request.args.get("importance")
if not importance_factor:
importance_factor = 1.0
else:
importance_factor = float(importance_factor)
fig, df, df_state, timestamp = create_fig(landesbez_strs=[state], fix_lims=False)
svg_string = convert_fig_to_svg(fig)
plt.close()
df["Bundesland"] = df.index.map(get_landesbezirk)
df = df.rename(columns={"Bundesland": "Landesbezirk"})
df_state = df_state.loc[df_state["Landesbezirk"] == state]
df = df.loc[df["Landesbezirk"] == state]
output_str = []
output_str = _print_as_html(df_state, output_str, dropna=False)
output_str = _print_as_html(df, output_str, total=df_state['Digitale Befragung'].sum(), dropna=False)
return render_template(
"base.html",
tables="\n".join(output_str),
timestamp=timestamp,
image=svg_string,
)
@app.route("/") @app.route("/")
@cache.cached(query_string=True) @cache.cached(query_string=True)
def tables(): def dashboard():
importance_factor = request.args.get("importance") importance_factor = request.args.get("importance")
if not importance_factor: if not importance_factor:
importance_factor = 1.0 importance_factor = 1.0