Shell script opening flask powered webpage opens t

2019-08-09 17:58发布

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.

3条回答
孤傲高冷的网名
2楼-- · 2019-08-09 18:16

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

查看更多
一夜七次
3楼-- · 2019-08-09 18:25

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
查看更多
何必那么认真
4楼-- · 2019-08-09 18:38

use threads. See this for a relevant post.

python webbrowser.open(url)

查看更多
登录 后发表回答