Add possibility for multi-plots

This commit is contained in:
Felix Blanke 2023-08-29 01:26:40 +02:00
parent bb214df990
commit dbce381a71

58
wsgi.py
View File

@ -42,10 +42,10 @@ def get_tables(url: str) -> tuple[pd.DataFrame, pd.DataFrame]:
def create_plot_df( def create_plot_df(
current_df: pd.DataFrame | None,
data_folder: str,
sheet_name: str,
curr_datetime, curr_datetime,
current_df: pd.DataFrame | None,
data_folder: str = "data",
sheet_name: str = "digital",
) -> pd.DataFrame: ) -> pd.DataFrame:
data_dict = {} data_dict = {}
@ -90,21 +90,13 @@ def create_plot_df(
def plot( def plot(
current_df: pd.DataFrame | None = None, curr_datetime,
data_folder: str = "data", df: pd.DataFrame,
sheet_name: str = "digital", annotate_current: bool = False,
total_targets: tuple[int, ...] = (1500,), total_targets: tuple[int, ...] = (1500,),
alpha: float | None = None, alpha: float | None = None,
landesbez_str: str | None = None, landesbez_str: str | None = None,
) -> str: ) -> str:
curr_datetime = datetime.datetime.now()
df = create_plot_df(
current_df=current_df,
data_folder=data_folder,
sheet_name=sheet_name,
curr_datetime=curr_datetime,
)
fig = plt.figure(dpi=300) fig = plt.figure(dpi=300)
# fill weekends # fill weekends
@ -114,7 +106,8 @@ def plot(
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")
series = df.sum(axis=1) if landesbez_str is None else df[landesbez_str] for bez in landesbez_str:
series = df.sum(axis=1) if bez is None else df[bez]
plot_df = series.to_frame("Digitale Befragung").replace(0, np.nan) plot_df = series.to_frame("Digitale Befragung").replace(0, np.nan)
plot_df = plot_df.astype({"Digitale Befragung": "float32"}) plot_df = plot_df.astype({"Digitale Befragung": "float32"})
if not pd.isna(plot_df).all().item(): if not pd.isna(plot_df).all().item():
@ -126,18 +119,18 @@ def plot(
alpha=alpha, alpha=alpha,
) )
plt.plot( (line,) = plt.plot(
plot_df.dropna().index, plot_df.dropna().index,
plot_df.dropna()["Digitale Befragung"], plot_df.dropna()["Digitale Befragung"],
ls="--", ls="--",
marker="o", marker="o",
lw=1, lw=1,
color="#e4004e", color="#e4004e" if bez is None else None,
markersize=4, markersize=4,
label=landesbez_str, label=bez if bez is not None else "Bundesweit",
) )
if current_df is not None: if annotate_current and bez is None:
plt.annotate( plt.annotate(
"Jetzt", "Jetzt",
( (
@ -152,13 +145,14 @@ def plot(
plot_df.index, plot_df.index,
plot_df["Digitale Befragung"], plot_df["Digitale Befragung"],
lw=1.5, lw=1.5,
color="#e4004e", color=line.get_color(),
label=landesbez_str, # label=bez,
) )
plt.title("Teilnahme an Digitaler Beschäftigtenbefragung") plt.title("Teilnahme an Digitaler Beschäftigtenbefragung")
plt.ylabel("# Teilnahmen") plt.ylabel("# Teilnahmen")
plt.ylim(0, total_targets[0] + 100) plt.ylim(0, total_targets[0] + 100)
plt.legend()
# use timezone offset to center tick labels # use timezone offset to center tick labels
plt.gca().xaxis.set_major_locator( plt.gca().xaxis.set_major_locator(
@ -203,6 +197,7 @@ def convert_fig_to_svg(fig: plt.Figure) -> str:
@cache.cached() @cache.cached()
def tables( def tables(
url: str = "https://beschaeftigtenbefragung.verdi.de/", url: str = "https://beschaeftigtenbefragung.verdi.de/",
importance_factor: float = 1,
): ):
def _print_as_html(df: pd.DataFrame): def _print_as_html(df: pd.DataFrame):
df = df.astype({"Digitale Befragung": "Int32"}) df = df.astype({"Digitale Befragung": "Int32"})
@ -236,6 +231,8 @@ def tables(
output_str = [] output_str = []
curr_datetime = datetime.datetime.now()
try: try:
df, df_state = get_tables(url) df, df_state = get_tables(url)
@ -246,7 +243,8 @@ def tables(
df_state = df_state.sort_values("Landesbezirk") df_state = df_state.sort_values("Landesbezirk")
fig = plot(df_state) plot_df = create_plot_df(curr_datetime, df_state)
annotate_current = True
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
except Exception as e: except Exception as e:
@ -263,9 +261,23 @@ def tables(
{"Digitale Befragung": "Int32"} {"Digitale Befragung": "Int32"}
) )
fig = plot() plot_df = create_plot_df(curr_datetime)
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>')
total = plot_df.loc[curr_datetime].sum()
landesbez_strs = [None] + [
bez
for bez in plot_df.columns
if plot_df.loc[curr_datetime][bez] >= importance_factor * total
]
fig = plot(
curr_datetime,
plot_df,
annotate_current=annotate_current,
landesbez_str=landesbez_strs,
)
_print_as_html(df_state) _print_as_html(df_state)
_print_as_html(df) _print_as_html(df)