I'm trying to dynamically create html tables using python flask. I know in the flask documentation (https://flask-table.readthedocs.io/en/stable/), it says you can pass attributes to the td and th html elements, but this only allows you to pass things at the column level using the td_html_attrs, th_attml_attrs and columns_html_attrs parameters, not at the cell level. E.g. if I have 3 columns i can only set 3 different td/ th attributes. I'm looking for a way to pass unique td/ th attributes to individual cells in the table.
Below is the python code that i have right now. It creates a flask project that has onclick attributes that go to the respective pages, but as you can see if you run it/ open the flask app in a browser (I'm using chrome) at localhost:5000 (the default port), you can't specify attributes of a particular cell, only the column.
from flask import Flask, render_template, Markup
from flask_table import Table, Col
app = Flask(__name__)
d_tda = {"onclick":"location.href='https://google.com';"}
d_tdb = {"onclick":"location.href='https://yahoo.com';"}
d_tdc = {"onclick":"location.href='https://bing.com';"}
class ItemTable(Table):
a = Col("a",td_html_attrs=d_tda)
b = Col("b",td_html_attrs=d_tdb)
c = Col("c",td_html_attrs=d_tdc)
class Item(object):
def __init__(self,a,b,c):
self.a= a
self.b= b
self.c= c
@app.route('/')
def results():
items = [Item('r1c1','r1c2','r1c3'),
Item('r2c1','r2c2','r2c3'),
Item('r3c1','r3c2','r3c3')]
table = ItemTable(items) # items will be a list of item objects, each item will create a row in the table
return render_template("results.html", table=Markup(table.__html__()))
if __name__ == '__main__':
app.run(debug=True)
^ That python file (call it app.py) must be in the same directory as a folder with the name "templates", and that "templates" folder must contain a file called "results.html" which will contain this:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{{ table }}
</body>
</html>
you can then run the app from the terminal/ command line in the directory that the app.py file is in with the following:
python "app.py"