energy graph
This commit is contained in:
@ -17,12 +17,22 @@ Jinja2==3.1.5
|
|||||||
loguru==0.7.3
|
loguru==0.7.3
|
||||||
MarkupSafe==3.0.2
|
MarkupSafe==3.0.2
|
||||||
msgspec==0.19.0
|
msgspec==0.19.0
|
||||||
|
numpy==2.0.2
|
||||||
packaging==24.2
|
packaging==24.2
|
||||||
|
pandas==2.2.3
|
||||||
plotly==5.24.1
|
plotly==5.24.1
|
||||||
|
psycopg==3.2.4
|
||||||
|
psycopg-binary==3.2.4
|
||||||
pycparser==2.22
|
pycparser==2.22
|
||||||
|
python-dateutil==2.9.0.post0
|
||||||
|
pytz==2024.2
|
||||||
redis==5.2.1
|
redis==5.2.1
|
||||||
requests==2.32.3
|
requests==2.32.3
|
||||||
|
six==1.17.0
|
||||||
|
SQLAlchemy==2.0.37
|
||||||
tenacity==9.0.0
|
tenacity==9.0.0
|
||||||
|
typing_extensions==4.12.2
|
||||||
|
tzdata==2025.1
|
||||||
urllib3==2.3.0
|
urllib3==2.3.0
|
||||||
Werkzeug==3.1.3
|
Werkzeug==3.1.3
|
||||||
zipp==3.21.0
|
zipp==3.21.0
|
||||||
|
42
src/run.py
42
src/run.py
@ -4,7 +4,10 @@ from flask_oidc import OpenIDConnect
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
import redis
|
import redis
|
||||||
import os
|
import os
|
||||||
import plotly.graph_objects as go
|
import plotly.express as px
|
||||||
|
import pandas as pd
|
||||||
|
import psycopg
|
||||||
|
import sqlalchemy
|
||||||
|
|
||||||
try:
|
try:
|
||||||
redis_url = os.environ['REDIS_URL']
|
redis_url = os.environ['REDIS_URL']
|
||||||
@ -52,25 +55,42 @@ def index():
|
|||||||
@app.route('/plot')
|
@app.route('/plot')
|
||||||
@oidc.require_login
|
@oidc.require_login
|
||||||
def plot():
|
def plot():
|
||||||
fig1 = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[4, 1, 6], mode='lines+markers')])
|
try:
|
||||||
graph_html_1 = fig1.to_html(full_html=False) # Nur das Diagramm-HTML
|
dbh = psycopg.connect()
|
||||||
fig2 = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[1, 1, 2], mode='lines+markers')])
|
engine = sqlalchemy.create_engine("postgresql+psycopg://", creator=lambda: dbh)
|
||||||
graph_html_2 = fig2.to_html(full_html=False) # Nur das Diagramm-HTML
|
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 1–12
|
||||||
|
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)
|
||||||
|
|
||||||
# Diagramm in eine HTML-Seite einbetten
|
|
||||||
return render_template_string(f"""
|
return render_template_string(f"""
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Plotly ohne Dash</title>
|
<title>Jahreswerte PV-Energie</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Plotly-Diagramm 1</h1>
|
{graph_html}
|
||||||
{graph_html_1}
|
|
||||||
<h1>Plotly-Diagramm 2</h1>
|
|
||||||
{graph_html_2}
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
""")
|
""")
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"Error when querying energy export values: {e}")
|
||||||
|
finally:
|
||||||
|
if dbh is not None:
|
||||||
|
dbh.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user