From c3ddc9ddfa459365b2ac22d4f18eca594346dd1d Mon Sep 17 00:00:00 2001 From: Felix Blanke Date: Sat, 19 Aug 2023 21:10:22 +0200 Subject: [PATCH] Improve html output --- download_digital.py | 51 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/download_digital.py b/download_digital.py index e9f8afc..b3d36a5 100644 --- a/download_digital.py +++ b/download_digital.py @@ -76,6 +76,7 @@ def main( 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") @@ -106,11 +107,51 @@ def main( data["Digitale Befragung"] = pd.Series(tot_col_data, index=tot_col_index) df = pd.DataFrame(data=data) if grouped: - df = df.groupby("Bundesland")[["Digitale Befragung"]].sum() - if dry_run: - df.loc["Total"] = df.sum(numeric_only=True) - with pd.option_context("display.max_rows", None): - print(df.astype({"Digitale Befragung": "Int32"})) + df = df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum() + + if dry_run or html: + if html: + print("") + print("") + + 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() + ) + else: filename = f"data/{datetime.today().strftime('%Y-%m-%d')}_data.ods" if Path(filename).exists():