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 import json
from flask_table import create_table, Col # from flask_table import create_table, Col
def get_config(): def get_config():
@ -102,37 +102,9 @@ def chart_fill(plt, y_ticks):
) )
class Table: def get_table(path):
""" create html table from filename to pass to template """ """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'] return table_data
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

View File

@ -246,7 +246,7 @@ function colorTables() {
cell.textContent = '\u25B2'; cell.textContent = '\u25B2';
cell.style.backgroundColor = '#ff4d4d'; cell.style.backgroundColor = '#ff4d4d';
} else if (cellContent == 'same') { } else if (cellContent == 'same') {
cell.textContent = '\u301C'; cell.textContent = '\uFF5E';
cell.style.backgroundColor = '#bdbdbd'; cell.style.backgroundColor = '#bdbdbd';
} else if (cellContent == 'nan') { } else if (cellContent == 'nan') {
cell.style.backgroundColor = '#eeeeee'; cell.style.backgroundColor = '#eeeeee';

View File

@ -91,7 +91,26 @@
</div> </div>
<div class="content graph"> <div class="content graph">
<div class="graph-item"> <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>
<div class="graph-item"> <div class="graph-item">
<a href="/static/dyn/year-graph.png" data-lightbox="year"> <a href="/static/dyn/year-graph.png" data-lightbox="year">

View File

@ -16,7 +16,26 @@
</a> </a>
</div> </div>
<div class="graph-item"> <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>
</div> </div>
{% endfor %} {% endfor %}

View File

@ -9,7 +9,7 @@ from flask_httpauth import HTTPBasicAuth
from apscheduler.schedulers.background import BackgroundScheduler 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.db import get_current, insert_data
from src.graph_current import main as current_graph from src.graph_current import main as current_graph
from src.graph_nightly import main as nightly_graph from src.graph_nightly import main as nightly_graph
@ -92,7 +92,7 @@ def about():
@app.route("/graphs") @app.route("/graphs")
def graphs(): def graphs():
""" graphs page """ """ 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) 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_graph = os.path.join('static/dyn/monthly', month_clean + '.png')
month_name = datetime.strptime(month_clean, "%Y-%m").strftime('%B %Y') month_name = datetime.strptime(month_clean, "%Y-%m").strftime('%B %Y')
month_json = os.path.join('static/dyn/monthly', month) month_json = os.path.join('static/dyn/monthly', month)
table = Table(month_json).create_table() table = get_table(month_json)
month_dict = { month_dict = {
'month_graph': month_graph, 'month_graph': month_graph,
'month_name': month_name, 'month_name': month_name,