energy graph

This commit is contained in:
Wolfgang Hottgenroth 2025-01-27 16:21:58 +01:00
parent c54db7b820
commit 9b65eeee85
Signed by: wn
GPG Key ID: 18FDFA577A8871AD
2 changed files with 49 additions and 19 deletions

View File

@ -17,12 +17,22 @@ Jinja2==3.1.5
loguru==0.7.3
MarkupSafe==3.0.2
msgspec==0.19.0
numpy==2.0.2
packaging==24.2
pandas==2.2.3
plotly==5.24.1
psycopg==3.2.4
psycopg-binary==3.2.4
pycparser==2.22
python-dateutil==2.9.0.post0
pytz==2024.2
redis==5.2.1
requests==2.32.3
six==1.17.0
SQLAlchemy==2.0.37
tenacity==9.0.0
typing_extensions==4.12.2
tzdata==2025.1
urllib3==2.3.0
Werkzeug==3.1.3
zipp==3.21.0

View File

@ -4,7 +4,10 @@ from flask_oidc import OpenIDConnect
from loguru import logger
import redis
import os
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import psycopg
import sqlalchemy
try:
redis_url = os.environ['REDIS_URL']
@ -52,25 +55,42 @@ def index():
@app.route('/plot')
@oidc.require_login
def plot():
fig1 = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[4, 1, 6], mode='lines+markers')])
graph_html_1 = fig1.to_html(full_html=False) # Nur das Diagramm-HTML
fig2 = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[1, 1, 2], mode='lines+markers')])
graph_html_2 = fig2.to_html(full_html=False) # Nur das Diagramm-HTML
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", con=engine)
fig = px.bar(df, x='month', y='value', color='year', barmode='group')
fig.update_layout(
title="Jahreswerte Exportierte Energie",
xaxis_title="Monat",
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 = fig.to_html(full_html=False)
return render_template_string(f"""
<html>
<head>
<title>Jahreswerte PV-Energie</title>
</head>
<body>
{graph_html}
</body>
</html>
""")
except Exception as e:
raise Exception(f"Error when querying energy export values: {e}")
finally:
if dbh is not None:
dbh.close()
# Diagramm in eine HTML-Seite einbetten
return render_template_string(f"""
<html>
<head>
<title>Plotly ohne Dash</title>
</head>
<body>
<h1>Plotly-Diagramm 1</h1>
{graph_html_1}
<h1>Plotly-Diagramm 2</h1>
{graph_html_2}
</body>
</html>
""")
if __name__ == '__main__':