70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
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 1–12
|
||
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 1–12
|
||
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()
|
||
|