How to continuously refresh page data?

688 views
Skip to first unread message

Anmol Sarma

unread,
Jun 24, 2013, 2:41:41 AM6/24/13
to python-...@googlegroups.com
I am trying to use Tornado to display data from a serial port on a web page using this:

from tornado import ioloop, web
from serial import Serial

class MainHandler(web.RequestHandler):
    def get(self):
serialInterface = Serial("/dev/ttyUSB0",19200)
self.render("template.html", serialData = serialInterface.readline())
serialInterface.close()


application = web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
     application.listen(8000)
     ioloop.IOLoop.instance().start()
 
While this works, I need a way to continuously refresh the page every second to show the latest data received from the serial port. How do I go about doing this?

Much obliged!  

Nicholas Dudfield

unread,
Jun 24, 2013, 12:11:37 PM6/24/13
to python-...@googlegroups.com
Learn about websocket 


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Serge S. Koval

unread,
Jun 24, 2013, 12:55:17 PM6/24/13
to Tornado Mailing List
If you don't care about older browsers, use WebSocket as suggested.

I'd do it this way:
1. Create thread, which will read from serial port
2. Will feed messages to main thread
3. Main thread will broadcast received messages to all connected WebSocket clients

Serge.

aliane abdelouahab

unread,
Jun 24, 2013, 2:57:18 PM6/24/13
to python-...@googlegroups.com
noticed that Facebook uses a clever way to refresh the page, if the user is idle the  page will not get refreshed in small amount of time, only if you move your mouse that the page gets refreshed!
i dont know about websocket, but i advice you to attach the refresh to an event triggered by the user, for exemple,
if the user move the mouse:
    refresh every 10 seconds
else:
    refresh every 2 minutes

A. Jesse Jiryu Davis

unread,
Jun 24, 2013, 3:17:39 PM6/24/13
to python-...@googlegroups.com


--

aliane abdelouahab

unread,
Jun 24, 2013, 5:21:56 PM6/24/13
to python-...@googlegroups.com
the visiblity api is when the tab is not active (reduced or other tab), i think that if they will do this to refresh the page, then the user will be considered offline if he loses focus from page? 

Anmol Sarma

unread,
Jun 25, 2013, 6:39:10 AM6/25/13
to python-...@googlegroups.com
Thanks for the suggestions. I will definitely learn about websocket in the future, but for now I just added <meta http-equiv="refresh" content="1"> to the template.
Reply all
Reply to author
Forward
0 new messages