Compare commits

..
7 Commits
Author SHA1 Message Date
Felix Blanke 196972b90b Fix fallback 2023-12-03 12:57:01 +01:00
Felix Blanke d205625ef4 Update plot 2023-09-28 20:38:48 +02:00
Felix Blanke 6060930208 Tweak plot params 2023-09-20 02:49:26 +02:00
Felix Blanke 0cd5377442 Tweak plot params 2023-09-20 02:46:34 +02:00
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 32 additions and 16 deletions
+1 -2
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/"): def create_dfs(url: str = "https://beschaeftigtenbefragung.verdi.de/"):
try: try:
curr_datetime = datetime.datetime.now() df, df_state, curr_datetime = get_tables(url)
df, df_state = get_tables(url)
df = df.sort_values( df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"], ["Digitale Befragung", "Bundesland", "Bezirk"],
+31 -14
View File
@@ -46,13 +46,13 @@ app.config.from_mapping(config)
cache = Cache(app) 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) bez_data = get_bez_data(["bez_data_0", "bez_data_2"], url)
df = construct_dataframe(bez_data=bez_data[0], special_tag="stud") df = construct_dataframe(bez_data=bez_data[0], special_tag="stud")
df_state = construct_dataframe(bez_data=bez_data[1]) df_state = construct_dataframe(bez_data=bez_data[1])
return df, df_state return df, df_state, datetime.datetime.now()
def create_plot_df( def create_plot_df(
@@ -113,7 +113,10 @@ def plot(
fix_lims: bool = True, fix_lims: bool = True,
max_shading_date=None, max_shading_date=None,
) -> str: ) -> str:
fig = plt.figure(dpi=300) fig = plt.figure(dpi=300, figsize=(8.5, 5))
target_time = pd.Timestamp("2023-10-01")
plt.axvline(x=target_time, color="tab:green", linestyle=":")
if fix_lims: if fix_lims:
for total_target in total_targets: for total_target in total_targets:
@@ -173,7 +176,7 @@ def plot(
idx = np.argmin(nearest_target) idx = np.argmin(nearest_target)
ceil_val = max(max_val, total_targets[idx]) ceil_val = max(max_val, total_targets[idx])
plt.ylim(0, ceil_val * 1.025) plt.ylim(0, ceil_val * 1.04)
plt.legend() plt.legend()
# use timezone offset to center tick labels # use timezone offset to center tick labels
@@ -202,14 +205,14 @@ def plot(
# fill weekends # fill weekends
if max_shading_date is None: if max_shading_date is None:
max_shading_date = df.index.max() + datetime.timedelta(days=3) max_shading_date = df.index.max() + datetime.timedelta(days=4)
days = pd.date_range(start="2023-08-14", end=max_shading_date) days = pd.date_range(start="2023-08-14", end=max_shading_date)
for idx, day in enumerate(days[:-1]): for idx, day in enumerate(days[:-1]):
if day.weekday() >= 5: if day.weekday() >= 5:
plt.gca().axvspan(days[idx], days[idx + 1], alpha=0.2, color="gray") plt.gca().axvspan(days[idx], days[idx + 1], alpha=0.2, color="gray")
# reset xlim # reset xlim
plt.xlim(xlim) plt.xlim((xlim[0], pd.Timestamp("2023-10-02")))
plt.tight_layout() plt.tight_layout()
@@ -224,7 +227,7 @@ def create_fig(
): ):
curr_datetime = datetime.datetime.now() curr_datetime = datetime.datetime.now()
try: try:
df, df_state = get_tables(url) df, df_state, curr_datetime = get_tables(url)
df = df.sort_values( df = df.sort_values(
["Digitale Befragung", "Bundesland", "Bezirk"], ["Digitale Befragung", "Bundesland", "Bezirk"],
@@ -251,7 +254,7 @@ def create_fig(
{"Digitale Befragung": "Int32"} {"Digitale Befragung": "Int32"}
) )
plot_df = create_plot_df(curr_datetime) plot_df = create_plot_df(curr_datetime, df_state)
annotate_current = False annotate_current = False
timestamp = Markup(f'<font color="red">{key} 10:00:00</font>') timestamp = Markup(f'<font color="red">{key} 10:00:00</font>')
@@ -291,11 +294,19 @@ def _print_as_html(
dropna: bool = True, dropna: bool = True,
) -> list[str]: ) -> list[str]:
df = df.astype({"Digitale Befragung": "Int32"}) 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 total = df_state["Digitale Befragung"].sum() if df_state is not None else None
if df_state is not 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_tmp = df.loc[df["Landesbezirk"] == idx]
df_state_tmp = df_state.loc[df_state["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 missing_idx = df_tmp.loc[df_tmp.isna().any(axis=1)].iloc[0].name
@@ -334,7 +345,7 @@ def _print_as_html(
) )
if total and (diff := total - df["Digitale Befragung"].sum()): if total and (diff := total - df["Digitale Befragung"].sum()):
tfoot.append(" <tr>") tfoot.append(" <tr>")
num_missing = missing_df['Digitale Befragung'].sum() num_missing = missing_df["Digitale Befragung"].sum()
tfoot.append( tfoot.append(
f" <td>Weitere Bezirke ({num_missing})</td>" f" <td>Weitere Bezirke ({num_missing})</td>"
if num_missing if num_missing
@@ -385,9 +396,7 @@ def state_dashboard(state: str):
output_str = [] output_str = []
output_str = _print_as_html(df_state, output_str, dropna=False) output_str = _print_as_html(df_state, output_str, dropna=False)
output_str = _print_as_html( output_str = _print_as_html(df, output_str, df_state=df_state, dropna=False)
df, output_str, df_state=df_state, dropna=False
)
return render_template( return render_template(
"base.html", "base.html",
@@ -425,5 +434,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__": if __name__ == "__main__":
app.run() app.run()