Move to flask
This commit is contained in:
parent
3109b1eddf
commit
27c196ee18
52
app.py
Normal file
52
app.py
Normal file
@ -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 = [
|
||||||
|
" <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>")
|
||||||
|
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))
|
||||||
@ -71,13 +71,11 @@ bundesland_dict = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def main(
|
def construct_dataframe(
|
||||||
url: str = "https://beschaeftigtenbefragung.verdi.de/",
|
url: str = "https://beschaeftigtenbefragung.verdi.de/",
|
||||||
tag: str = "bez_data_2",
|
tag: str = "bez_data_2",
|
||||||
dry_run: bool = False,
|
|
||||||
grouped: bool = False,
|
grouped: bool = False,
|
||||||
html: bool = False,
|
):
|
||||||
) -> pd.DataFrame:
|
|
||||||
r = requests.get(url)
|
r = requests.get(url)
|
||||||
soup = BeautifulSoup(r.text, "html.parser")
|
soup = BeautifulSoup(r.text, "html.parser")
|
||||||
|
|
||||||
@ -109,49 +107,19 @@ def main(
|
|||||||
if grouped:
|
if grouped:
|
||||||
df = df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum()
|
df = df.groupby("Bundesland", as_index=False)[["Digitale Befragung"]].sum()
|
||||||
|
|
||||||
if dry_run or html:
|
return df
|
||||||
if html:
|
|
||||||
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 = [
|
def main(
|
||||||
" <tfoot>",
|
url: str = "https://beschaeftigtenbefragung.verdi.de/",
|
||||||
" <td>Gesamt</td>",
|
tag: str = "bez_data_2",
|
||||||
]
|
dry_run: bool = False,
|
||||||
for i in range(len(df.columns) - 2):
|
grouped: bool = False,
|
||||||
tfoot.append(" <td/>")
|
) -> None:
|
||||||
tfoot.extend(
|
df = construct_dataframe(url=url, tag=tag, grouped=grouped)
|
||||||
[
|
|
||||||
f" <td>{df['Digitale Befragung'].sum()}</td>",
|
if dry_run:
|
||||||
" </tr>",
|
|
||||||
" </tfoot>",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
tfoot = "\n".join(tfoot)
|
|
||||||
idx = table.index("</table>")
|
|
||||||
print(table[: idx - 1])
|
|
||||||
print(tfoot)
|
|
||||||
print(table[idx:])
|
|
||||||
else:
|
|
||||||
print(df)
|
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():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user