Refactor
This commit is contained in:
parent
0292db36e2
commit
dbf477ed17
83
wsgi.py
83
wsgi.py
@ -257,73 +257,72 @@ def convert_fig_to_svg(fig: plt.Figure) -> str:
|
||||
return imgdata.read()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@cache.cached(query_string=True)
|
||||
def tables():
|
||||
def _print_as_html(df: pd.DataFrame, total: int | None = None) -> None:
|
||||
df = df.astype({"Digitale Befragung": "Int32"})
|
||||
df = df.dropna()
|
||||
with pd.option_context("display.max_rows", None):
|
||||
table = df.to_html(
|
||||
index_names=False,
|
||||
justify="left",
|
||||
index=False,
|
||||
classes="sortable dataframe",
|
||||
)
|
||||
def _print_as_html(df: pd.DataFrame, output_str: list[str], total: int | None = None) -> list[str]:
|
||||
df = df.astype({"Digitale Befragung": "Int32"})
|
||||
df = df.dropna()
|
||||
with pd.option_context("display.max_rows", None):
|
||||
table = df.to_html(
|
||||
index_names=False,
|
||||
justify="left",
|
||||
index=False,
|
||||
classes="sortable dataframe",
|
||||
)
|
||||
|
||||
tfoot = [
|
||||
" <tfoot>",
|
||||
" <tr>",
|
||||
" <td>Gesamt</td>",
|
||||
tfoot = [
|
||||
" <tfoot>",
|
||||
" <tr>",
|
||||
" <td>Gesamt</td>",
|
||||
]
|
||||
for i in range(len(df.columns) - 2):
|
||||
tfoot.append(" <td></td>")
|
||||
tfoot.extend(
|
||||
[
|
||||
f" <td>{df['Digitale Befragung'].sum()}</td>",
|
||||
" </tr>",
|
||||
]
|
||||
)
|
||||
if total:
|
||||
tfoot.extend([
|
||||
" <tr>",
|
||||
" <td>Weitere Bezirke</td>",
|
||||
])
|
||||
for i in range(len(df.columns) - 2):
|
||||
tfoot.append(" <td></td>")
|
||||
tfoot.extend(
|
||||
[
|
||||
f" <td>{df['Digitale Befragung'].sum()}</td>",
|
||||
f" <td>{total - df['Digitale Befragung'].sum()}</td>",
|
||||
" </tr>",
|
||||
]
|
||||
)
|
||||
if total:
|
||||
tfoot.extend([
|
||||
" <tr>",
|
||||
" <td>Weitere Bezirke</td>",
|
||||
])
|
||||
for i in range(len(df.columns) - 2):
|
||||
tfoot.append(" <td></td>")
|
||||
tfoot.extend(
|
||||
[
|
||||
f" <td>{total - df['Digitale Befragung'].sum()}</td>",
|
||||
" </tr>",
|
||||
]
|
||||
)
|
||||
tfoot.append(" </tfoot>")
|
||||
tfoot.append(" </tfoot>")
|
||||
|
||||
tfoot = "\n".join(tfoot)
|
||||
idx = table.index("</table>")
|
||||
output_str.append(table[: idx - 1])
|
||||
output_str.append(tfoot)
|
||||
output_str.append(table[idx:])
|
||||
tfoot = "\n".join(tfoot)
|
||||
idx = table.index("</table>")
|
||||
output_str.append(table[: idx - 1])
|
||||
output_str.append(tfoot)
|
||||
output_str.append(table[idx:])
|
||||
return output_str
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@cache.cached(query_string=True)
|
||||
def tables():
|
||||
importance_factor = request.args.get("importance")
|
||||
if not importance_factor:
|
||||
importance_factor = 1.0
|
||||
else:
|
||||
importance_factor = float(importance_factor)
|
||||
|
||||
output_str = []
|
||||
|
||||
fig, df, df_state, timestamp = create_fig(importance_factor=importance_factor)
|
||||
svg_string = convert_fig_to_svg(fig)
|
||||
plt.close()
|
||||
|
||||
_print_as_html(df_state)
|
||||
|
||||
df["Bundesland"] = df.index.map(get_landesbezirk)
|
||||
df = df.rename(columns={"Bundesland": "Landesbezirk"})
|
||||
|
||||
_print_as_html(df, total=df_state['Digitale Befragung'].sum())
|
||||
output_str = []
|
||||
output_str = _print_as_html(df_state, output_str)
|
||||
output_str = _print_as_html(df, output_str, total=df_state['Digitale Befragung'].sum())
|
||||
|
||||
return render_template(
|
||||
"base.html",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user