Files
numbers/src/pv_routes.py
Wolfgang Hottgenroth cab9ed705e
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/tag/woodpecker Pipeline was successful
order by year
2025-12-15 18:19:38 +01:00

70 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from flask import Flask, session, g, render_template_string
from loguru import logger
import json
import plotly.express as px
import plotly.graph_objects as po
import pandas as pd
import psycopg
import sqlalchemy
import time
from app import app
from app import oidc
@app.route('/')
@oidc.require_login
def pvstats():
try:
dbh = psycopg.connect()
engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh)
df = pd.read_sql("SELECT month, cast(year AS varchar), current_energy AS value FROM pv_energy_by_month ORDER BY year, month", con=engine)
fig_1 = px.bar(df, x='month', y='value', color='year', barmode='group')
fig_1.update_layout(
title=f"Jahreswerte Exportierte Energie PV-Anlage",
xaxis_title="",
yaxis_title="",
legend_title="Jahr",
xaxis=dict(
tickmode="array",
tickvals=list(range(1, 13)), # Monate 112
ticktext=["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
),
yaxis=dict(ticksuffix=" kWh")
)
graph_html_1 = fig_1.to_html(full_html=False, default_height='30%')
df = pd.read_sql("SELECT month, cast(year AS varchar), current_energy AS value FROM car_energy_by_month ORDER BY year, month", con=engine)
fig_2 = px.bar(df, x='month', y='value', color='year', barmode='group')
fig_2.update_layout(
title=f"Jahreswerte Verbrauch Elektroauto",
xaxis_title="",
yaxis_title="",
legend_title="Jahr",
xaxis=dict(
tickmode="array",
tickvals=list(range(1, 13)), # Monate 112
ticktext=["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]
),
yaxis=dict(ticksuffix=" kWh")
)
graph_html_2 = fig_2.to_html(full_html=False, default_height='30%')
return render_template_string(f"""
<html>
<head>
<title>Jahreswerte PV und Auto</title>
</head>
<body>
{graph_html_1}
{graph_html_2}
</body>
</html>
""")
except Exception as e:
raise Exception(f"Error when querying energy values: {e}")
finally:
if dbh is not None:
dbh.close()