views:

112

answers:

4

Hello:

I have a simple workflow [Step 0]->[1]->[2]->...->[Step N]. The master program knows the step (state) it is currently at.

I want to stream this in real time to a website (in the local area network) so that when my colleagues open, say, http://thecomputer:8000, they can see a real time rendering of the current state of our workflow with any relevant details.

I've tought about writing the state of the script to an StringIO object (streaming to it) and use Javascript to refresh the browser, but I honestly have no idea how to actually do this.

Any advice?

+1  A: 

You could have the python script write an xml file that you get with an ajax request in your web page, and get the status info from that.

Daenyth
But how can you update an XML file without rewriting it every time? Is there a way to "update" an XML file (i.e. add a `<node></node>` somewhere in the middle). As you will correctly judge by my question, I'm a rookie here.
Arrieta
Just overwrite it and have the frontend poll for it.
Daenyth
This is a nice way to do it! And if you really want to keep the whole history of the execution of the script, just keep appending nodes to the XML, a nice library like lxml will make your life so much easier for this xml creation task.
Federico Cáceres
This combined with the JS bits from @Omar seems like a great solution.
Michael Mior
A: 

If you just what a quick solution rather than a full web page, check out SimpleHTTPServer You can write your state information in the form of a file to a directory and it and it is updated in for others to see.

Not beautiful, but very quick and very easy.

For a demo, just type python -m SimpleHTTPServer This will create a web page with the address of http://[your computer's name or IP]:8000 with the contents of the current working directory it is in when started. The files are hot linked. If you click on a file, it is displayed in the browser; if you click on a directory, you change to that directory and its contents are listed.

For your use, you could start with an empty directory and create files that match the steps you want to report. You could have files named your steps, "step 1.txt" for example, and more detailed info inside that file. Step 1 could be a directory that your script creates when step 1 is complete, etc.

Alternatively, check out Twisted, which will allow you to serve HTTP requests from your Python script directly.

If / when you want to get fancier, you can use essentially the same method. From your python script you write the output into files in an empty directory. From a web server with PHP, write a trivial script that checks that directory and displays what was found. Set the refresh interval on the page to some value and it will self update in the viewer's browsers.

drewk
A: 

I don't have an idea about how Python works, but if it were PHP this would be easy (if you know what you should do).

Let say my application is myapp.py and it's working from step 1 to step N. I would make it return (simple Text), a number. This number is the its' step number. For example if I call http://localhost/myapp.py, it returns 5. After 30 minutes, I reload the page, it returns 15.

Now to the JavaScript thing. I will recommend using jQuery. It makes things a lot easier and faster. All I would do is

$('#streaming').load('http://localhost/myapp.py');

This will load the content of the page to the DIV with ID = streaming. But that will happen only when you load the page. To make that happen every x seconds, use JavaScript settimeout function.

var t=setTimeout("javascript statement",milliseconds);

It would be better to store the part that load the step in a function and call it when the page load and with setTimeout. It would help also to stop setTimeout once your script stops working (with a special number/message).

Omar Abid
A: 

Using plain JavaScript, your choices are Comet, WebSockets, and Ajax. I don't really know much about any of those, however. Check out some of these websites:

http://en.wikipedia.org/wiki/Comet_(programming)

http://www.websockets.org/

From what I know, Comet simply involves making a long-lived Ajax request to the web server, where a script can push data as necessary. WebSockets are only supported in the very latest versions of some browsers. The simplest solution would be what Omar Abid suggested: poll the web server every once in a while via Ajax.

l33tnerd