4

I am trying to make my python code autoupdate. The problem is that the code pulls up four html files, so four new tabs in the browser, every time it runs.

I tried auto-updating using an answer from a similar question: What is the best way to repeatedly execute a function every x seconds in Python?

import time

starttime = time.time()

while True:
  # My code here
  output_file("first.html", title="First HTML Page")
  # More code
  output_file("second.html", title="Second HTML Page")
  # Even more code
  time.sleep(60.0 - ((time.time() - starttime) % 60.0))

The problem with this method is that every minute it pulls up four more tabs-- meaning after four minutes I already have 16 tabs!

Anyone know how to autoupdate using the same tabs? I'd even take deleting the original tabs and replacing them, even though I imagine that's less elegant.

EDIT: I am running Windows, currently using IE, but I am also open to moving to Firefox or Chrome.

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
jenryb
  • 2,017
  • 12
  • 35
  • 72
  • 1
    What kind of tabs? Web browser tabs? We need to see the `output_file` function – cdonts Jul 24 '15 at 23:03
  • @cdonts Web browser tabs in IE. I'm not sure what you mean by output_file function, why is that important? – jenryb Jul 27 '15 at 14:15
  • Does IE have a python plugin? – Mark Jul 27 '15 at 18:04
  • @jenryb It's important, to see how your code works. – cdonts Jul 27 '15 at 19:17
  • @Mark I don't know if it does. The code also works with Firefox. Basically it chooses the default browser and opens four tabs using output_file command, which comes from Bokeh. – jenryb Jul 27 '15 at 20:03
  • how are you opening the tabs? – Padraic Cunningham Jul 27 '15 at 21:36
  • @PadraicCunningham the Bokeh plotting module has output_file as a command. It pulls up the tabs in the default browser. – jenryb Jul 27 '15 at 21:42
  • @jenry, how are you calling show?I presume you have to to actually open the file? – Padraic Cunningham Jul 27 '15 at 21:57
  • Looking at bokeh, I don't think there is an easy way to do it using bokeh itself, would closing the browser itself work for you? also do you need a general solution or for a specific OS? – Padraic Cunningham Jul 27 '15 at 22:13
  • @PadraicCunningham, that would be okay. But I'd prefer to just close the window with the tabs for the graphs and not other open windows. – jenryb Jul 27 '15 at 22:14
  • @PadraicCunningham I call show at the end of each piece of code to show the figure I created. – jenryb Jul 27 '15 at 22:16
  • Just saw you are using windows, a simple way to close the browser would be a subprocess call, something like `subprocess.check_call(["Taskkill", "/IM","browser.exe" ,"/F"])`, it is possible on linux/unix to close tabs but not sure what the equivalent would be on windows – Padraic Cunningham Jul 27 '15 at 22:18
  • You can also specify the browser to use with show, `show(obj, browser="firefox.exe")`, you could then kill that instead of any browser you have open yourself – Padraic Cunningham Jul 27 '15 at 22:22
  • Looking at the source code I definitely see no way to do it with bokeh, the browser is opened using the python builtin lib webbrowser https://github.com/bokeh/bokeh/blob/master/bokeh/browserlib.py#L4 – Padraic Cunningham Jul 27 '15 at 22:30

7 Answers7

3

I guess the best thing for you to do is close the entire program and restart it. If you are using a Windows computer, add

from subprocess import call

At the beginning, put

call("start chrome.exe") #For Google Chrome

And at the end,

call("taskkill /f /im chrome.exe")

This will literally close out the entire browser and start it again :P

Just telling you, if you are using Internet Explorer, please, oh god please do not stop "explorer.exe". That's basically stopping......everything.

I suggest using some scheduling software to run your program so that you aren't always using RAM. Many have been said already

rassa45
  • 3,482
  • 1
  • 29
  • 43
  • 1
    Thanks for the heads up, as I was using Internet Explorer. I chose to use Firefox for the opening and reopening of windows so it's a separate browser than the other things I may be working on. This solution works, for anyone visiting the question later, using firefox the first call had to be call('firefox -savepng http://www.google.com', shell=True) – jenryb Jul 31 '15 at 14:57
  • I'm glad it worked, please accept answers if they worked for you (emphasis on please :P). However, also understand that this is not "technically" the ideal method because the purpose of tabs is to not use as much RAM by only working on the ones you need. You can do this operation through command line (probably by using an external software). – rassa45 Jul 31 '15 at 15:16
  • It may fix your problem of having to use two browsers – rassa45 Jul 31 '15 at 15:16
2

As @padraicCunningham says in the comments, there is no way to reload specific tabs in your browser through bokeh. You therefore have to handle this by controlling your browser more directly instead. There are various options, from killing your browser Before reloading (as suggested in the comments), through scripting your browser through shell interfaces to handling it inside the browser.

I personally err towards the last, which would involve a little JavaScript. The simplest solution is probably just to have a page that opens 4 other pages in tabs and then reloads them on a timer. You can find help on how to do that form here:

This would require your Python application to open the controlling page once at the start, which you could do using the webbrowser package.

Community
  • 1
  • 1
Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
1

one other way (discouraged by the W3C apart from for dynamically updating pages due to the way it can interfere with the back button) would be to add a refresh tag in the head section of the HTML:

<meta http-equiv="refresh" content="5">

where the content is the time in seconds to sleep before refreshing

James Kent
  • 5,763
  • 26
  • 50
0

I'm sorry, I'd like to comment on this, but I'm lacking reputation to do so.

So here is my suggestion: Close the file before the loop. I'm not that into python, so I can't give a code example but maybe this might help, too.

destroyWindow #or something like this. But I think that is Tkinter

close() #http://www.tutorialspoint.com/python/file_close.htm
TheEnclave
  • 25
  • 5
0

If you want to autoupdate by downloading files, you can directly perform a get request instead of opening it through the browser. You can alternatively create a socket connection and perform the update.These seem to be more reliable and elegant methods.

The browser method is prone to problems created by human intervention. However if you want to do it through your way , you can open your 4 tabs for the first time. And using the browser dom , just refresh the page. So it would be like:

For every x secs:
    if first time:
        open the relevant tabs
    else:
        refresh the tabs
    check if update available and do the needfull
-1
import keyboard

keyboard.press_and_release('F5')
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
-3

What is the best way to repeatedly execute a function every x seconds in Python?

I'm not very good, but I'd suggest somehow using a for loop, to do it.

def function(n):
    for x in n:
    ~~~~~~~~~~~  # Blah, Blah, Blah. Some other code that you want
Slass33
  • 66
  • 9
  • There are actually softwares to schedule running command line operations – rassa45 Jul 31 '15 at 08:43
  • @ytpillai But installing a software, just to do this, is quite troublesome – Slass33 Aug 01 '15 at 22:55
  • actually, sometimes you don't even need to install. Most operating systems have a scheduler which can be accessed through admin rights for anti viruses and other automated softwares. Since Python programs just need to be run with 'python ' command, adding it in should be easy – rassa45 Aug 02 '15 at 16:53