From dbf477ed177c99b9884e0c66c5a6c85bb7c85c2b Mon Sep 17 00:00:00 2001 From: Felix Blanke Date: Fri, 1 Sep 2023 14:24:45 +0200 Subject: [PATCH] Refactor --- wsgi.py | 83 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/wsgi.py b/wsgi.py index c2d8d6a..b762354 100644 --- a/wsgi.py +++ b/wsgi.py @@ -257,73 +257,72 @@ def convert_fig_to_svg(fig: plt.Figure) -> str: return imgdata.read() -@app.route("/") -@cache.cached(query_string=True) -def tables(): - def _print_as_html(df: pd.DataFrame, total: int | None = None) -> None: - df = df.astype({"Digitale Befragung": "Int32"}) - df = df.dropna() - with pd.option_context("display.max_rows", None): - table = df.to_html( - index_names=False, - justify="left", - index=False, - classes="sortable dataframe", - ) +def _print_as_html(df: pd.DataFrame, output_str: list[str], total: int | None = None) -> list[str]: + df = df.astype({"Digitale Befragung": "Int32"}) + df = df.dropna() + with pd.option_context("display.max_rows", None): + table = df.to_html( + index_names=False, + justify="left", + index=False, + classes="sortable dataframe", + ) - tfoot = [ - " ", - " ", - " Gesamt", + tfoot = [ + " ", + " ", + " Gesamt", + ] + for i in range(len(df.columns) - 2): + tfoot.append(" ") + tfoot.extend( + [ + f" {df['Digitale Befragung'].sum()}", + " ", ] + ) + if total: + tfoot.extend([ + " ", + " Weitere Bezirke", + ]) for i in range(len(df.columns) - 2): tfoot.append(" ") tfoot.extend( [ - f" {df['Digitale Befragung'].sum()}", + f" {total - df['Digitale Befragung'].sum()}", " ", ] ) - if total: - tfoot.extend([ - " ", - " Weitere Bezirke", - ]) - for i in range(len(df.columns) - 2): - tfoot.append(" ") - tfoot.extend( - [ - f" {total - df['Digitale Befragung'].sum()}", - " ", - ] - ) - tfoot.append(" ") + tfoot.append(" ") - tfoot = "\n".join(tfoot) - idx = table.index("") - output_str.append(table[: idx - 1]) - output_str.append(tfoot) - output_str.append(table[idx:]) + tfoot = "\n".join(tfoot) + idx = table.index("") + output_str.append(table[: idx - 1]) + output_str.append(tfoot) + output_str.append(table[idx:]) + return output_str +@app.route("/") +@cache.cached(query_string=True) +def tables(): importance_factor = request.args.get("importance") if not importance_factor: importance_factor = 1.0 else: importance_factor = float(importance_factor) - output_str = [] - fig, df, df_state, timestamp = create_fig(importance_factor=importance_factor) svg_string = convert_fig_to_svg(fig) plt.close() - _print_as_html(df_state) - df["Bundesland"] = df.index.map(get_landesbezirk) df = df.rename(columns={"Bundesland": "Landesbezirk"}) - _print_as_html(df, total=df_state['Digitale Befragung'].sum()) + output_str = [] + output_str = _print_as_html(df_state, output_str) + output_str = _print_as_html(df, output_str, total=df_state['Digitale Befragung'].sum()) return render_template( "base.html",