2

I have a webpage showing some data. I have a python script that continuously updates the data(fetches the data from database, and writes it to the html page).It takes about 5 minutes for the script to fetch the data. I have the html page set to refresh every 60 seconds using the meta tag. However, I want to change this and have the page refresh as soon as the python script updates it, so basically I need to add some code to my python script that refreshes the html page as soon as it's done writing to it.

Is this possible ?

4
  • What are you using to display the HTML page? Commented Jul 25, 2013 at 19:57
  • I'm not sure I get you.I have it running on apache, if that's what you meant. Commented Jul 25, 2013 at 20:07
  • what framework are you using, we need context, look at the top of the python file and tell us what the import statments are at least Commented Jul 25, 2013 at 20:42
  • I'm not using any framework. The python script does nothing more than read the entire html page, modify some parts and write it back. I know this is inefficient and I'm much better of using Django etc, but it's been running like this for sometime now and I don't want to spend a lot of time rebuilding it. Commented Jul 25, 2013 at 22:04

1 Answer 1

1

Without diving into complex modern things like WebSockets, there's no way for the server to 'push' a notice to a web browser. What you can do, however, it make the client check for updates in a way that is not visible to the user.

It will involve writing Javascript & writing an extra file. When writing your main webpage, add, inside Javascript, a timestamp (Unix timestamp will be easiest here). You also write that same timestamp to a file on the web server (let's call it updatetime.txt). Using an AJAX request on the page, you pull in updatetime.txt & see if the number in the file is bigger than the number stored when you generate the document, refresh the page if you see an updated time. You can alter how 'instantly' the changes get noticed but controlling how quickly you poll.

I won't go into too much detail on writing the code but I'd probably just use $.ajax() from JQuery (even though it's sort of overkill for one function) to make the calls. The trick to putting something on a time in JS is setinterval. You should be able to find plenty of documentation on using both of them already written.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.