From 27c196ee187e7ab215265e8480a0f815135dfef0 Mon Sep 17 00:00:00 2001 From: Felix Blanke Date: Sat, 19 Aug 2023 22:00:38 +0200 Subject: [PATCH] Move to flask --- app.py | 52 +++++++++++++++++++++++++++++++++++++++++ download_digital.py | 56 ++++++++++----------------------------------- 2 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..bee03c3 --- /dev/null +++ b/app.py @@ -0,0 +1,52 @@ +import pandas as pd +from flask import Flask, render_template + +from download_digital import construct_dataframe + +app = Flask(__name__) + + +@app.route("/") +def tables( + url: str = "https://beschaeftigtenbefragung.verdi.de/", + tag: str = "bez_data_2", +): + df = construct_dataframe(url=url, tag=tag, grouped=False) + + output_str = [] + + def _print_as_html(df: pd.DataFrame): + df = df.astype({"Digitale Befragung": "Int32"}) + with pd.option_context("display.max_rows", None): + table = df.to_html( + index_names=False, + justify="left", + index=False, + classes="sortable dataframe", + ) + + tfoot = [ + " ", + " Gesamt", + ] + for i in range(len(df.columns) - 2): + tfoot.append(" ") + tfoot.extend( + [ + f" {df['Digitale Befragung'].sum()}", + " ", + " ", + ] + ) + tfoot = "\n".join(tfoot) + idx = table.index("") + output_str.append(table[: idx - 1]) + output_str.append(tfoot) + output_str.append(table[idx:]) + + _print_as_html(df) + _print_as_html( + df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum() + ) + + return render_template("base.html", tables="\n".join(output_str)) diff --git a/download_digital.py b/download_digital.py index b3d36a5..a8bd597 100644 --- a/download_digital.py +++ b/download_digital.py @@ -71,13 +71,11 @@ bundesland_dict = { } -def main( +def construct_dataframe( url: str = "https://beschaeftigtenbefragung.verdi.de/", tag: str = "bez_data_2", - dry_run: bool = False, grouped: bool = False, - html: bool = False, -) -> pd.DataFrame: +): r = requests.get(url) soup = BeautifulSoup(r.text, "html.parser") @@ -109,49 +107,19 @@ def main( if grouped: df = df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum() - if dry_run or html: - if html: - print("") - print("") + return df - def _print_as_html(df: pd.DataFrame): - df = df.astype({"Digitale Befragung": "Int32"}) - with pd.option_context("display.max_rows", None): - if html: - table = df.to_html( - index_names=False, - justify="left", - index=False, - classes="sortable dataframe", - ) - tfoot = [ - " ", - " Gesamt", - ] - for i in range(len(df.columns) - 2): - tfoot.append(" ") - tfoot.extend( - [ - f" {df['Digitale Befragung'].sum()}", - " ", - " ", - ] - ) - tfoot = "\n".join(tfoot) - idx = table.index("") - print(table[: idx - 1]) - print(tfoot) - print(table[idx:]) - else: - print(df) - - _print_as_html(df) - if html: - _print_as_html( - df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum() - ) +def main( + url: str = "https://beschaeftigtenbefragung.verdi.de/", + tag: str = "bez_data_2", + dry_run: bool = False, + grouped: bool = False, +) -> None: + df = construct_dataframe(url=url, tag=tag, grouped=grouped) + if dry_run: + print(df) else: filename = f"data/{datetime.today().strftime('%Y-%m-%d')}_data.ods" if Path(filename).exists():