use template tables instead of flask tables

This commit is contained in:
Simon 2024-01-06 10:02:30 +07:00
parent 6fa62cb79e
commit 15a1340e15
Signed by: simon
GPG Key ID: 2C15AA5E89985DD4
5 changed files with 50 additions and 40 deletions

View File

@ -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

View File

@ -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';

View File

@ -91,7 +91,26 @@
</div>
<div class="content graph">
<div class="graph-item">
{{ table }}
<table class="comp-table">
<thead>
<tr>
<th></th>
<th>this year</th>
<th>last year</th>
<th>change</th>
</tr>
</thead>
<tbody>
{% for row in table %}
<tr>
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
<td>{{ row.3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="graph-item">
<a href="/static/dyn/year-graph.png" data-lightbox="year">

View File

@ -16,7 +16,26 @@
</a>
</div>
<div class="graph-item">
{{month.table}}
<table class="comp-table">
<thead>
<tr>
<th></th>
<th>this year</th>
<th>last year</th>
<th>change</th>
</tr>
</thead>
<tbody>
{% for row in month.table %}
<tr>
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
<td>{{ row.3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endfor %}

View File

@ -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,