diff --git a/wsgi.py b/wsgi.py index 7f75311..762c97a 100644 --- a/wsgi.py +++ b/wsgi.py @@ -8,7 +8,7 @@ import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as np import pandas as pd -from flask import Flask, render_template, request +from flask import Flask, Markup, render_template, request from flask_caching import Cache from download_digital import construct_dataframe, get_bez_data @@ -166,16 +166,6 @@ def plot( def tables( url: str = "https://beschaeftigtenbefragung.verdi.de/", ): - df, df_state = get_tables(url) - - df = df.sort_values( - ["Digitale Befragung", "Bundesland", "Bezirk"], ascending=[False, True, True] - ) - - df_state = df_state.sort_values("Landesbezirk") - - output_str = [] - def _print_as_html(df: pd.DataFrame): df = df.astype({"Digitale Befragung": "Int32"}) with pd.option_context("display.max_rows", None): @@ -206,15 +196,44 @@ def tables( output_str.append(tfoot) output_str.append(table[idx:]) + output_str = [] + + try: + df, df_state = get_tables(url) + + df = df.sort_values( + ["Digitale Befragung", "Bundesland", "Bezirk"], + ascending=[False, True, True], + ) + + df_state = df_state.sort_values("Landesbezirk") + + image = plot(df_state) + timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + except Exception: + last_file = sorted(Path("data").iterdir())[-1] + key = last_file.name[:10] + + with (Path("data") / f"{key}_data.ods").open("rb") as ff: + df = pd.read_excel(ff, sheet_name="digital").astype( + {"Digitale Befragung": "Int32"} + ) + with (Path("data") / f"{key}_state_data.ods").open("rb") as ff: + df_state = pd.read_excel(ff, sheet_name="digital").astype( + {"Digitale Befragung": "Int32"} + ) + + image = plot() + timestamp = Markup(f'{key} 10:00:00') + _print_as_html(df_state) _print_as_html(df) - image = plot(df_state, plot_all=True) - return render_template( "base.html", tables="\n".join(output_str), - timestamp=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + timestamp=timestamp, image=image, )