From d3c424d90aa5e2fe29f6b0306efada43bd1f3774 Mon Sep 17 00:00:00 2001 From: Felix Blanke Date: Fri, 1 Sep 2023 14:47:13 +0200 Subject: [PATCH] Add route for single states --- wsgi.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/wsgi.py b/wsgi.py index 119c801..c7eda5a 100644 --- a/wsgi.py +++ b/wsgi.py @@ -11,10 +11,10 @@ import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as np 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 download_digital import construct_dataframe, get_bez_data, get_landesbezirk +from download_digital import construct_dataframe, get_bez_data, get_landesbezirk, landesbezirk_dict config = { "CACHE_TYPE": "FileSystemCache", @@ -23,6 +23,14 @@ config = { "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" time.tzset() @@ -312,9 +320,46 @@ def _print_as_html(df: pd.DataFrame, output_str: list[str], total: int | None = return output_str +@app.route("/") +@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("/") @cache.cached(query_string=True) -def tables(): +def dashboard(): importance_factor = request.args.get("importance") if not importance_factor: importance_factor = 1.0