bethanyP bethanyP - 29 days ago 13x
HTML Question

Trying to insert a table into jinja2 template from flask

I have been trying to create my first Flask website, using super simple templates in

. (I started with my regular html and abandoned it quickly because figuring out where to put things was a nightmare. I will go back in after this hard part and tweak the flask code, for now, I have eliminated EVERYTHING but the html declaration and the attempt at dropping in the table.)

Flask Code:

from flask import Flask
from flask import render_template
import redis
from flask import json
from dateutils.parser import parse
from flask_table import Table, Col, DatetimeCol
r=redis.StrictRedis(host='localhost', port =6379, db=0)

app = Flask(__name__)

def p():
raw = r.get('rData')
raw= yaml.load(raw)
raw = json.loads(raw)
for item in raw:
item['date_time'] = parse(item['date_time'])
class ItemTable(Table):
Sensor = Col("Sensor")
date_time = DatetimeCol("Date & Time")
Reading = Col("Reading")
table = ItemTable(raw)
return render_template('p.html'), table

if __name__ == '__main__':

This code will call the page /p/ locally and if i ask it to show
it will print out the data for this table (which I have included below).

{{ raw }}

I get my dictionary back!

But when I try to show the table that
should create, it crashes my page or nothing shows at all. I have also tried using the return code this way:

return render_template('p.html', table=table)

passing it as a variable of fully executed html, nothing good comes of it.
{{ table }}

Actual Data in a list of dictionaries:

[{"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 27.5, "Sensor": "Water Temperature (degrees Celsius)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 0.91, "Sensor": "Lake Gage Height (in feet)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 53.0, "Sensor": "Specific Conductivity in microSiemens"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 7.3, "Sensor": "Dissolved Oxygen (ppm)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 6.9, "Sensor": "pH (standard field)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 27.7, "Sensor": "Air Temperature (in degrees Celsius)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 2.5, "Sensor": "Wind Speed (in mph)"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 238.0, "Sensor": "Wind Direction (degrees CW from North)"}, {"Reading": 30.7, "Sensor": "Relative Humidity", "date-Time": "2016-08-03T14:00:00.000-04:00"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 1.26, "Sensor": "Marsh Gage Height in feet"}, {"date_time": "2016-08-03T14:00:00.000-04:00", "Reading": 0.01, "Sensor": "24 Cumulative Precipication (inches)"}]

I pull this out of a redis cache I created, but you can just set
to the above list and substitute it.


What do I need to insert in the Jinja2 to make the table show up from one of these two
presentations, or is there some other way to tell flask to port the converted list of dictionaries to the template?

I have searched everywhere and the page on flask-table does not show how to present it on the other side.

My goal is to drop it in a container that is a child to the base layout. but just getting it to show up as a table is enough, I can figure the rest out from there.

Simplest HTML/Jinja template I could create:

<!doctype html>

<h1> test </h1>


I tried using
{% table %}
also to no avail.

I am not sure if the problem is that I generated html in flask and tried to send it to the template or if I am not telling Jinja how to present this
element that I have created and included with the

<h1> test </h1>
is just to allow me to see if the page is rendering at all when I send the table. It is not important.

I believe the problem that I am having is that most tutorials are over simplified and each set of documentation (
) are light on what happens on the other side of the equation. I found ONE explanation of
presented on two pages but the same stuff. Neither made a suggestion about calling it with a template.


I am getting a KeyError when I run it now: the changes are as follows:

raw= yaml.load(raw) #new to make a list for reading after next line
raw = json.loads(raw) #makes it iterable in UTF-8
for item in raw:
item['date_time'] = parse(item['date_time']) #creates dates but throws
errors which stops flask

New Edit: Part of the Data after json.loads:

[{u'Reading': 25.9,
u'Sensor': u'Water Temperature (degrees Celsius)',
u'date_time': u'2016-08-05T08:45:00.000-04:00'},
{u'Reading': 0.88,
u'Sensor': u'Lake Gage Height (in feet)',
u'date_time': u'2016-08-05T08:45:00.000-04:00'}]

Data After `parse['date_time']:

datetime.datetime(2016, 8, 5, 8, 45, tzinfo=tzoffset(None, -14400))

New Update: I somehow missed a capital T in the
field, so correcting that fixed the KeyError...but I still cannot get it to pass over into the HTML. I get a whole bunch of lines referring to the folders and files, then I get:

AttributeError: 'NoneType' object has no attribute 'datetime_formats'

Why are dates and times so much fun?


You need to convert timestamps to datetime objects

from dateutil.parser import parse # pip install python-dateutil

raw = json.loads(raw)
for item in raw:
    item['date_time'] = parse(item['date_time'])

And pass table to template

return render_template('p.html', table=table)

Upd: All items must have a date_time key. Some items in your example have date-Time instead.