1

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.

LittleBobbyTables
  • 4,361
  • 9
  • 38
  • 67
  • I need this functionality as well. I was not able to use the answers below. None of them are accepted as well. Did you find a way to get this work? – skr Apr 22 '16 at 18:54

4 Answers4

2

use threads. See this for a relevant post.

python webbrowser.open(url)

Community
  • 1
  • 1
tourdownunder
  • 1,779
  • 4
  • 22
  • 34
0

put app.run() in a function and in '__main__' start this function as new thread. Then open webbrowser

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
Rashid Mv
  • 396
  • 3
  • 8
0

This 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 my app.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...

if __name__ == '__main__':
    os.system("open http://localhost:8080")
    app.run(
            '0.0.0.0',
            port=8080,
            debug=True,
            use_reloader=False, # <== use False and get one tab...but no debug output
Efrain
  • 66
  • 1
  • 2
0

I had the same problem also tried using a different thread (python webbrowser.open(url))

and using os.startfile(url)

got the same result

but when I changed the Debug to false, it opened only one tab as I wanted

app.run(debug=False)

Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21
baruchno
  • 11
  • 4