From 53e044a7e05d330fa9d38ebd699d5706b36f05fc Mon Sep 17 00:00:00 2001 From: iainfogg Date: Sun, 22 Sep 2024 16:59:44 +0000 Subject: [PATCH] Add initial cut of templating --- apps/predbat/templates/apps.html | 13 ++ apps/predbat/templates/charts.html | 19 +++ apps/predbat/templates/config.html | 13 ++ apps/predbat/templates/dash.html | 10 ++ apps/predbat/templates/docs.html | 9 ++ apps/predbat/templates/layout.html | 89 +++++++++++ apps/predbat/templates/logs.html | 16 ++ apps/predbat/templates/plan.html | 11 ++ apps/predbat/web.py | 248 +++++++++++++++++------------ 9 files changed, 330 insertions(+), 98 deletions(-) create mode 100644 apps/predbat/templates/apps.html create mode 100644 apps/predbat/templates/charts.html create mode 100644 apps/predbat/templates/config.html create mode 100644 apps/predbat/templates/dash.html create mode 100644 apps/predbat/templates/docs.html create mode 100644 apps/predbat/templates/layout.html create mode 100644 apps/predbat/templates/logs.html create mode 100644 apps/predbat/templates/plan.html diff --git a/apps/predbat/templates/apps.html b/apps/predbat/templates/apps.html new file mode 100644 index 000000000..63999d198 --- /dev/null +++ b/apps/predbat/templates/apps.html @@ -0,0 +1,13 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + +{% block content %} +

Predbat Apps.yaml

+ +
+ {% autoescape false %} + {{ apps_html }} + {% endautoescape %} +
+{% endblock %} diff --git a/apps/predbat/templates/charts.html b/apps/predbat/templates/charts.html new file mode 100644 index 000000000..ccb281486 --- /dev/null +++ b/apps/predbat/templates/charts.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + +{% block content %} +

{{ chart_title }} Chart

+ - Battery + Power + Cost + Rates + InDay + PV + PV7 +
+ {% autoescape false %} + {{ chart_html }} + {% endautoescape %} +
+{% endblock %} diff --git a/apps/predbat/templates/config.html b/apps/predbat/templates/config.html new file mode 100644 index 000000000..8dd1cd6d3 --- /dev/null +++ b/apps/predbat/templates/config.html @@ -0,0 +1,13 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + +{% block content %} +

Predbat Config

+ +
+ {% autoescape false %} + {{ config_html }} + {% endautoescape %} +
+{% endblock %} diff --git a/apps/predbat/templates/dash.html b/apps/predbat/templates/dash.html new file mode 100644 index 000000000..f1f387f93 --- /dev/null +++ b/apps/predbat/templates/dash.html @@ -0,0 +1,10 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + + +{% block content %} +{% autoescape false %} +{{ dash_html }} +{% endautoescape %} +{% endblock %} diff --git a/apps/predbat/templates/docs.html b/apps/predbat/templates/docs.html new file mode 100644 index 000000000..1a0551105 --- /dev/null +++ b/apps/predbat/templates/docs.html @@ -0,0 +1,9 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + +{% block content %} +
+ +
+{% endblock %} diff --git a/apps/predbat/templates/layout.html b/apps/predbat/templates/layout.html new file mode 100644 index 000000000..8b034bf5a --- /dev/null +++ b/apps/predbat/templates/layout.html @@ -0,0 +1,89 @@ + + + + Predbat Web Interface + + + + {% if refresh is defined %} + + {% endif %} + + + +
+ + + + + + + + + + + +

Predbat

DashPlanChartsConfigapps.yamlLogDocs
+
+ {% block content %}{% endblock %} + + diff --git a/apps/predbat/templates/logs.html b/apps/predbat/templates/logs.html new file mode 100644 index 000000000..8a5788a7a --- /dev/null +++ b/apps/predbat/templates/logs.html @@ -0,0 +1,16 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + + +{% block content %} +

Logfile ({% if errors %}Errors{% elif warnings %}Warnings{% else %}All{% endif %})

+ - All Warnings Errors
+ + {% for line in lines %} + + {% endfor %} +
{{ line.line_no}}{{ line.start_line }} {{ line.rest_line }}
+ {% autoescape false %} + {% endautoescape %} +{% endblock %} diff --git a/apps/predbat/templates/plan.html b/apps/predbat/templates/plan.html new file mode 100644 index 000000000..d3a5e4865 --- /dev/null +++ b/apps/predbat/templates/plan.html @@ -0,0 +1,11 @@ +{% extends "layout.html" %} + +{% block title %}Home{% endblock %} + + +{% block content %} +Plan +{% autoescape false %} +{{ plan_html }} +{% endautoescape %} +{% endblock %} diff --git a/apps/predbat/web.py b/apps/predbat/web.py index 980ed327a..57292b69d 100644 --- a/apps/predbat/web.py +++ b/apps/predbat/web.py @@ -20,6 +20,15 @@ def __init__(self, base) -> None: self.pv_power_hist = {} self.pv_forecast_hist = {} + from jinja2 import Environment, FileSystemLoader, select_autoescape + self.template_env = Environment( + loader=FileSystemLoader("templates"), + autoescape=select_autoescape() + ) + + # Disable autoescaping for the HTML plan + self.template_env.filters['raw'] = lambda value: value + def history_attribute(self, history, state_key="state", last_updated_key="last_updated", scale=1.0): results = {} if history: @@ -60,8 +69,8 @@ def history_update(self): async def start(self): # Start the web server on port 5052 - app = web.Application() - app.router.add_get("/", self.html_index) + app = web.Application(debug=True) + app.router.add_get("/", self.html_dash) app.router.add_get("/plan", self.html_plan) app.router.add_get("/log", self.html_log) app.router.add_get("/menu", self.html_menu) @@ -69,7 +78,8 @@ async def start(self): app.router.add_get("/charts", self.html_charts) app.router.add_get("/config", self.html_config) app.router.add_post("/config", self.html_config_post) - app.router.add_get("/dash", self.html_dash) + # app.router.add_get("/dash", self.html_dash) + app.router.add_get("/docs", self.html_docs) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, "0.0.0.0", 5052) @@ -266,24 +276,15 @@ def render_chart(self, series_data, yaxis_name, chart_name, now_str):