I am struggling to set up a tornado web application so that every time a new record inserted into MySQL table, after refreshing the html page (or even better without refreshing), the data in the rendered html table will update.
The request handler code is as follows:
class TestHandler(tornado.web.RequestHandler):
def get(self):
data=sess.query(Country).all()
self.render("test.html", data=data)
The html table code is as follows:
<table id="example" >
<thead>
<tr>
<th>name</th>
<th>capital</th>
</tr>
</thead>
<tbody>
{% for dt in data%}
<tr>
<td>{{dt.name}}</td>
<td>{{dt.capital}}</td>
</tr>
{% end %}
</tbody>
</table>
Currently the html table isn't updated with regard to any updates from mysql table side. Only when restarting tornado server, will the new data appear. This question may be very elementary, but I really need some directions here.
Problem solved. Since I am using SQLAlchemy to access MySQL Database, I need to end the origin session and create a new session.
class TestHandler(tornado.web.RequestHandler):
def get(self):
sess = Session()
data=sess.query(Country).all()
sess.close()
self.render("test.html", data=data)