Move graph generation logic into seperate file

This commit is contained in:
Peter Stockings
2023-01-23 21:51:24 +11:00
parent 6f5b76ac64
commit 2085ff21da
2 changed files with 27 additions and 19 deletions

22
app.py
View File

@@ -9,11 +9,9 @@ from flask_htmx import HTMX
import minify_html import minify_html
from urllib.parse import urlparse from urllib.parse import urlparse
from flask_socketio import SocketIO from flask_socketio import SocketIO
import pygal
from pygal.style import Style
from db import DataBase from db import DataBase
from graph import generate_graph
app = Flask(__name__) app = Flask(__name__)
# TODO CHANGE SECRET KEY TO ENVIRONMENT VARIABLE # TODO CHANGE SECRET KEY TO ENVIRONMENT VARIABLE
@@ -57,22 +55,8 @@ def overview():
cadences = db.get_all_cadences() cadences = db.get_all_cadences()
last_cadence = cadences[-1]['rpm'] last_cadence = cadences[-1]['rpm']
custom_style = Style( graph_data = generate_graph([c['logged_at'] for c in cadences[::2]], [
background='transparent', ("RPM", [c['rpm'] for c in cadences[::2]])])
plot_background='transparent',
foreground='#53E89B',
foreground_strong='#53A0E8',
foreground_subtle='#630C0D',
opacity='.6',
opacity_hover='.9',
transition='400ms ease-in',
colors=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53'))
graph = pygal.Line(show_y_guides=False,
show_legend=False, style=custom_style)
graph.x_labels = [c['logged_at'] for c in cadences[::2]]
graph.add('RPM', [c['rpm'] for c in cadences[::2]])
graph_data = graph.render_data_uri()
return render_template('overview.html', last_cadence=last_cadence, cadences=cadences[-15:], graph_data=graph_data) return render_template('overview.html', last_cadence=last_cadence, cadences=cadences[-15:], graph_data=graph_data)

24
graph.py Normal file
View File

@@ -0,0 +1,24 @@
import pygal
from pygal.style import Style
custom_style = Style(
background='transparent',
plot_background='transparent',
foreground='#53E89B',
foreground_strong='#53A0E8',
foreground_subtle='#630C0D',
opacity='.6',
opacity_hover='.9',
transition='400ms ease-in',
colors=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53'))
def generate_graph(x_labels, data, style=custom_style):
graph = pygal.Line(show_y_guides=False,
show_legend=False, style=style)
graph.x_labels = x_labels
for title, values in data:
graph.add(title, values)
graph_data = graph.render_data_uri()
return graph_data