From 15a1340e15b04d437e5ce4d6459f56b932a7519f Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 6 Jan 2024 10:02:30 +0700 Subject: [PATCH] use template tables instead of flask tables --- web/src/helper.py | 40 ++++++-------------------------------- web/static/js/aqi.js | 2 +- web/templates/graphs.html | 21 +++++++++++++++++++- web/templates/monthly.html | 21 +++++++++++++++++++- web/views.py | 6 +++--- 5 files changed, 50 insertions(+), 40 deletions(-) diff --git a/web/src/helper.py b/web/src/helper.py index b5fcda1..583df25 100644 --- a/web/src/helper.py +++ b/web/src/helper.py @@ -2,7 +2,7 @@ import json -from flask_table import create_table, Col +# from flask_table import create_table, Col def get_config(): @@ -102,37 +102,9 @@ def chart_fill(plt, y_ticks): ) -class Table: - """ create html table from filename to pass to template """ +def get_table(path): + """read json file from path""" + with open(path, "r") as f: + table_data = json.loads(f.read()).get("data") - COLUMNS = [' ', 'this year', 'last year', 'change'] - - def __init__(self, filename): - self.filename = filename - self.rows = self.get_rows() - - def get_rows(self): - """ read filename to build rows dict """ - - with open(self.filename, 'r') as json_file: - json_raw = json_file.read() - - table_json = json.loads(json_raw) - - rows = [] - for i in table_json['data']: - row = dict(zip(self.COLUMNS, i)) - rows.append(row) - - return rows - - def create_table(self): - """ create the table with rows and columns """ - - blank_table = create_table(options={'classes': ['comp-table']}) - - for i in self.COLUMNS: - blank_table.add_column(i, Col(i)) - - table_obj = blank_table(self.rows) - return table_obj + return table_data diff --git a/web/static/js/aqi.js b/web/static/js/aqi.js index 706e12d..2ca7083 100644 --- a/web/static/js/aqi.js +++ b/web/static/js/aqi.js @@ -246,7 +246,7 @@ function colorTables() { cell.textContent = '\u25B2'; cell.style.backgroundColor = '#ff4d4d'; } else if (cellContent == 'same') { - cell.textContent = '\u301C'; + cell.textContent = '\uFF5E'; cell.style.backgroundColor = '#bdbdbd'; } else if (cellContent == 'nan') { cell.style.backgroundColor = '#eeeeee'; diff --git a/web/templates/graphs.html b/web/templates/graphs.html index 9585597..fe0f546 100644 --- a/web/templates/graphs.html +++ b/web/templates/graphs.html @@ -91,7 +91,26 @@
- {{ table }} + + + + + + + + + + + {% for row in table %} + + + + + + + {% endfor %} + +
this yearlast yearchange
{{ row.0 }}{{ row.1 }}{{ row.2 }}{{ row.3 }}
- {{month.table}} + + + + + + + + + + + {% for row in month.table %} + + + + + + + {% endfor %} + +
this yearlast yearchange
{{ row.0 }}{{ row.1 }}{{ row.2 }}{{ row.3 }}
{% endfor %} diff --git a/web/views.py b/web/views.py index feddcbe..ebd4427 100644 --- a/web/views.py +++ b/web/views.py @@ -9,7 +9,7 @@ from flask_httpauth import HTTPBasicAuth from apscheduler.schedulers.background import BackgroundScheduler -from src.helper import Table, get_config +from src.helper import get_config, get_table from src.db import get_current, insert_data from src.graph_current import main as current_graph from src.graph_nightly import main as nightly_graph @@ -92,7 +92,7 @@ def about(): @app.route("/graphs") def graphs(): """ graphs page """ - table = Table('static/dyn/year-table.json').create_table() + table = get_table("static/dyn/year-table.json") return render_template('graphs.html', title='Graphs', table=table) @@ -108,7 +108,7 @@ def monthly(): month_graph = os.path.join('static/dyn/monthly', month_clean + '.png') month_name = datetime.strptime(month_clean, "%Y-%m").strftime('%B %Y') month_json = os.path.join('static/dyn/monthly', month) - table = Table(month_json).create_table() + table = get_table(month_json) month_dict = { 'month_graph': month_graph, 'month_name': month_name,