views:

899

answers:

9

I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).

I understand I can do this with javascript but is there a python only based solution? Even if there is, is javascript the way to go? I'm more than willing to learn javascript if needed but if not, I'd like to stay focused on python.

Sorry for the basic question but I'm still clueless when it comes to web programming.

Thx!

+1  A: 

JavaScript is the primary way to add this sort of interactivity to a website. You can make the back-end Python, but the client will have to use JavaScript AJAX calls to update the page. Python doesn't run in the browser, so you're out of luck if you want to use just Python.

(It's also possible to use Flash or Java applets, but that's a pretty heavyweight solution for what seems like a small problem.)

Daniel Lew
+1  A: 

You need Javascript in one way or another for your 30 second refresh. Alternatively, you could set a meta tag refresh for every 30 seconds to redirect to the current page, but the Javascript route will prevent page flicker.

Chris Ballance
+2  A: 

You could use Comet, but I strongly discourage you from doing so. I'd just write a short Javascript, using jQuery this is really straightforward.

Another possibility is the use of an iframe that reloads every 30 seconds, this would prevent the whole page from reloading.

Georg
+2  A: 

If you want to do it entirely in python you can use pyjamas.

It generates javascript directly from python code, so you avoid writing javascript yourself completely.

nosklo
A: 

Is this for a real webapp? Or is this a convenience thing for you to view output in the browser? If it's more so for convenience, you could consider using mod_python.

mod_python is an extension for the apache webserver that embeds a python interpreter in the web server (so the script runs server side). It would easily let you do this sort of thing locally or for your own convenience. Then you could just run the script with mod python and have the handler post your results. You could probably easily implement the refreshing too, but I would not know off the top of my head how to do this.

Hope this helps... check out mod_python. It's not too bad once you get everything configured.

Tom
+4  A: 

This question appears to have two things in it.

  1. Presentation on the web. This is easy to do in Python -- use Django or TurboGears or any Python-based web framework.

  2. Refresh of the web page to show new data. This can be done two ways.

    • Some fancy Javascript to refresh.

    • Some fancy HTML to refresh the page. The meta refresh tag is what you want. If you do this, you have an all-Python solution.

S.Lott
+1  A: 

Write your output to a log file, and load the log file to the browser thru web server. If you need auto refresh, create a template HTML file with tag to refresh every 15 seconds:

<META HTTP-EQUIV="refresh" CONTENT="15">

and use server side include to include the log file on the page.

Harriv
+1  A: 

If you want a dead simple way to print data from a Python script to a webpage and update automatically, you can just print from the script. For example, using Apache with the below Python CGI script:

#!/usr/bin/python 

import time
import sys
import random

def write(inline=''):
    sys.stdout.write(inline)
    sys.stdout.write('\r\n')
    sys.stdout.flush()

#prints out random digits between 1 and 1000 indefinitely
write("Content-type: text/html\r\n")
i = 0
while(True):
    i = i + 1
    time.sleep(1)
    write(str(i) + "<br />")

If I navigate to that in a browser (Firefox, don't know if other browsers might work differently with regards to buffering etc), it prints the digits continually. Mind you, it prints in sequential order so the newer data is at the bottom rather than that top, but it might work depending on what exactly you're looking to do.

If this isn't really what you're looking for, the only other way to do this is an automatic refreshing page (either in an iframe, or the whole page) or with javascript to do the data fetching.

You can use a meta refresh tag in your iframe or page HTML source, and your CGI can print the new data each time it's refreshed. Alternatively, you can use javascript with an XMLHTTPRequest to read the new data in without a visual page refresh.

Jay
This is called Comet, but it's not guaranteed to work with every browser. (And there may be timeout issues.)
Georg
All good points, it really just depends on the details of what the OP was looking for. My guess is a dash of javascript would be the better solution.
Jay
A: 

Perhaps "long polling" is what you're looking for?

Long polling could be described as "HTTP push", basically you have a (Python) script served via a web-server, which only outputs data when available.. Then you try and load this page asynchronously via Javascript, when it fails you retry, when it succeeds you do something with the data (display it, usually)

The examples in my answer are in PHP, but it it's only really 2 commands (sleep(rand(1, 10)) - the other few are to demonstrate the javascript's error handling)

Well, it's not quite that simple.. You can't just serve a CGI python script via Apache, because you will run out of worker-threads, and the web-server will not be able to accept any further connections.. So, you need to use a more specialised server..

  • The twisted Python framework is perfect for such servers - the following two servers are incidentally both written with it
  • cometd - the "most famous" long-polling server thing, although I never had much luck with the Python implementation
  • slosh - seems extremely simply to use.. Implemented in Python, although you can interact with it via HTTP requests
dbr