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!
But this article is aimed more at all you techy lot and centres around how to run python scripts on user input and produce an output — without saving the input anywhere. This is super useful as it's completely automated, as long as the servers don’t crash (they might — I have no idea) then this should just run itself. No maintenance whatsoever.
Depending on the Python implementation you use, the interpreter can be: Whatever form the interpreter takes, the code you write will always be run by this program. Therefore, the first condition to be able to run Python scripts is to have the interpreter correctly installed on your system.
A lot of Python scripts don’t request the user to enter data a line at a time; they take a file as their input, process it, and produce a file as the output. Here’s a simple script that asks for an input filename and an output filename. It expects the input file to contain a number of lines, each with a comma-separated list of numbers on it.
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:
This question appears to have two things in it.
Presentation on the web. This is easy to do in Python -- use Django or TurboGears or any Python-based web framework.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With