I am implementing a GUI in Python/Flask. The way flask is designed, the local host along with the port number has to be "manually" opened.
Is there a way to automate it so that upon running the code, browser(local host) is automatically opened?
I tried using webbrowser package but it opens the webpage after the session is killed.
I also looked at the following posts but they are going over my head.
Shell script opening flask powered webpage opens two windows
python webbrowser.open(url)
Problem occurs when html pages are rendered based on user inputs.
Thanks in advance.
import webbrowser
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
webbrowser.open_new('http://127.0.0.1:2000/')
app.run(port=2000)
Using the web browser in Python Under most circumstances, simply calling the open() function from this module will open url using the default browser . You have to import the module and use open() function.
According to the documentation webbrowser. open_new(url) Should do that. Unfortunately in case Chrome is the default browser it only opens a new tab.
Use of web browser Python module for opening URL in the browser? This is useful for automation testing in web development. Using this code you can open the web development website URL in the browser and then you can test. Clicking on buttons, filling the form automatically, login website and there are so many test cases you can explore with it.
Actually, at the beginning, none of the browsers is registered and only the default one is called each time. Hence we had to register them manually. Now we are going to use these methods to see how we could open browsers with our passed URLs.
Opening URL in Default Browser 1 If new = 0, open URL in same browser window 2 If new = 1, opens URL in new browser window 3 If new = 2, open URL in same tab. More ...
This is useful for automation testing in web development. Using this code you can open the web development website URL in the browser and then you can test. Clicking on buttons, filling the form automatically, login website and there are so many test cases you can explore with it.
Use timer to start new thread to open web browser.
import webbrowser
from threading import Timer
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
def open_browser():
webbrowser.open_new('http://127.0.0.1:2000/')
if __name__ == "__main__":
Timer(1, open_browser).start();
app.run(port=2000)
I'd suggest the following improvement to allow for loading of the browser when in debug mode:
Inspired by this answer, will only load the browser on the first run...
def main():
# The reloader has not yet run - open the browser
if not os.environ.get("WERKZEUG_RUN_MAIN"):
webbrowser.open_new('http://127.0.0.1:2000/')
# Otherwise, continue as normal
app.run(host="127.0.0.1", port=2000)
if __name__ == '__main__':
main()
https://stackoverflow.com/a/9476701/10521959
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With