Compare commits

...

3 Commits

Author SHA1 Message Date
Felix Blanke
29459d5386 Add endpoint that only returns the total number 2023-09-19 19:46:38 +02:00
Felix Blanke
4f1835c8f8 Format 2023-09-19 19:46:24 +02:00
Felix Blanke
32bd83f054 Return curr datetime at table creation 2023-09-19 19:46:01 +02:00
2 changed files with 24 additions and 11 deletions

View File

@ -15,8 +15,7 @@ from wsgi import create_fig, create_plot_df, get_tables, plot
def create_dfs(url: str = "https://beschaeftigtenbefragung.verdi.de/"):
try:
curr_datetime = datetime.datetime.now()
df, df_state = get_tables(url)
df, df_state, curr_datetime = get_tables(url)
df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"],

32
wsgi.py
View File

@ -46,13 +46,13 @@ app.config.from_mapping(config)
cache = Cache(app)
def get_tables(url: str) -> tuple[pd.DataFrame, pd.DataFrame]:
def get_tables(url: str) -> tuple[pd.DataFrame, pd.DataFrame, datetime.datetime]:
bez_data = get_bez_data(["bez_data_0", "bez_data_2"], url)
df = construct_dataframe(bez_data=bez_data[0], special_tag="stud")
df_state = construct_dataframe(bez_data=bez_data[1])
return df, df_state
return df, df_state, datetime.datetime.now()
def create_plot_df(
@ -224,7 +224,7 @@ def create_fig(
):
curr_datetime = datetime.datetime.now()
try:
df, df_state = get_tables(url)
df, df_state, curr_datetime = get_tables(url)
df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"],
@ -291,11 +291,19 @@ def _print_as_html(
dropna: bool = True,
) -> list[str]:
df = df.astype({"Digitale Befragung": "Int32"})
missing_df = df[["Digitale Befragung"]].isna().join(df[["Landesbezirk"]]).groupby("Landesbezirk").sum()
missing_df = (
df[["Digitale Befragung"]]
.isna()
.join(df[["Landesbezirk"]])
.groupby("Landesbezirk")
.sum()
)
total = df_state["Digitale Befragung"].sum() if df_state is not None else None
if df_state is not None:
for idx, row in missing_df.loc[missing_df["Digitale Befragung"] == 1].iterrows():
for idx, row in missing_df.loc[
missing_df["Digitale Befragung"] == 1
].iterrows():
df_tmp = df.loc[df["Landesbezirk"] == idx]
df_state_tmp = df_state.loc[df_state["Landesbezirk"] == idx]
missing_idx = df_tmp.loc[df_tmp.isna().any(axis=1)].iloc[0].name
@ -334,7 +342,7 @@ def _print_as_html(
)
if total and (diff := total - df["Digitale Befragung"].sum()):
tfoot.append(" <tr>")
num_missing = missing_df['Digitale Befragung'].sum()
num_missing = missing_df["Digitale Befragung"].sum()
tfoot.append(
f" <td>Weitere Bezirke ({num_missing})</td>"
if num_missing
@ -385,9 +393,7 @@ def state_dashboard(state: str):
output_str = []
output_str = _print_as_html(df_state, output_str, dropna=False)
output_str = _print_as_html(
df, output_str, df_state=df_state, dropna=False
)
output_str = _print_as_html(df, output_str, df_state=df_state, dropna=False)
return render_template(
"base.html",
@ -425,5 +431,13 @@ def dashboard():
)
@app.route("/total")
@cache.cached(timeout=60)
def total_result(url: str = "https://beschaeftigtenbefragung.verdi.de/"):
df, df_state, curr_datetime = get_tables(url)
total = df_state["Digitale Befragung"].sum().item()
return f"{total}"
if __name__ == "__main__":
app.run()