Improve html output

This commit is contained in:
Felix Blanke 2023-08-19 21:10:22 +02:00
parent cd6e3bf38c
commit c3ddc9ddfa

View File

@ -76,6 +76,7 @@ def main(
tag: str = "bez_data_2", tag: str = "bez_data_2",
dry_run: bool = False, dry_run: bool = False,
grouped: bool = False, grouped: bool = False,
html: bool = False,
) -> pd.DataFrame: ) -> pd.DataFrame:
r = requests.get(url) r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser") soup = BeautifulSoup(r.text, "html.parser")
@ -106,11 +107,51 @@ def main(
data["Digitale Befragung"] = pd.Series(tot_col_data, index=tot_col_index) data["Digitale Befragung"] = pd.Series(tot_col_data, index=tot_col_index)
df = pd.DataFrame(data=data) df = pd.DataFrame(data=data)
if grouped: if grouped:
df = df.groupby("Bundesland")[["Digitale Befragung"]].sum() df = df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum()
if dry_run:
df.loc["Total"] = df.sum(numeric_only=True) if dry_run or html:
with pd.option_context("display.max_rows", None): if html:
print(df.astype({"Digitale Befragung": "Int32"})) print("<link rel='stylesheet' href='style.css'>")
print("<script src='sorttable.js'></script>")
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 = [
" <tfoot>",
" <td>Gesamt</td>",
]
for i in range(len(df.columns) - 2):
tfoot.append(" <td/>")
tfoot.extend(
[
f" <td>{df['Digitale Befragung'].sum()}</td>",
" </tr>",
" </tfoot>",
]
)
tfoot = "\n".join(tfoot)
idx = table.index("</table>")
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: else:
filename = f"data/{datetime.today().strftime('%Y-%m-%d')}_data.ods" filename = f"data/{datetime.today().strftime('%Y-%m-%d')}_data.ods"
if Path(filename).exists(): if Path(filename).exists():