0

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"
Kevin
  • 1
  • 1
  • How are you calling this code? Where's your flask routes? Right now in the example you've given you're simply printing it to the terminal but you probably want to incorporate it into a Jinja template. – Hultner Jun 17 '20 at 21:24
  • @Hultner Right now I'm creating the tables then passing them using render_template(table=Markup(table.__html__())). This render_template(...) is the return of a function with a decorator. The structure thus looks like the following: @app.route('/results', methods=["POST"]) def results(): return render_template(table=Markup(table.__html__())). The table variable is then passed as {{ table }} to a jinja template – Kevin Jun 17 '20 at 21:35
  • put code in question, not in comment - it will be more readable and more people will see it. – furas Jun 17 '20 at 22:16
  • @furas I have added the flask framework code to the original question. Thank you for the suggestion! – Kevin Jun 17 '20 at 23:35

0 Answers0