I'm writing a small locally-running flask powered web app for some coworkers. They aren't super technologically savvy so I'd like them to be able to simply double click a link to run a shell script, which will start the flask server and then pop open the localhost page on their macs.
I've created a simple file start
:
python server.py
open http://localhost:5000
But it pops open two webpages - first the localhost (but with an "unable to connect" message) and then the localhost again with it working. So odd!
I tried using the webbrowser
module in python, but this has the same effect:
if __name__ == '__main__':
app.debug = True
webbrowser.open("http://localhost:5000/")
app.run()
and putting it afterwards:
if __name__ == '__main__':
app.debug = True
app.run()
webbrowser.open("http://localhost:5000/")
only opens the page after the server is shutdown.
Any ideas? I guess they could just close the non-working page, but it isn't very smooth functionality.
put
app.run()
in a function and in'__main__'
start this function as new thread. Then open webbrowserThis may be too late, but I needed the same functionality and wound up at this page also...
I thought about this a little bit and by process of elimination I tried adding the
use_reloader=False flag
to myapp.run()
.The problem is now gone, but I can see by the console trace it has turned off the debugger. If you don't care about that, then this may work...
This is what worked for me...
use threads. See this for a relevant post.
python webbrowser.open(url)