Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open browser automatically when Python code is executed

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)
like image 706
Abhishek Kulkarni Avatar asked Jan 17 '19 11:01

Abhishek Kulkarni


People also ask

How do I open a browser URL from a Python script?

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.

How do I open a new browser window in Python?

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.

How to use web browser Python module for opening url in browser?

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.

Is it possible to open browsers with passed URLs?

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.

How do I open a URL in the default browser?

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 ...

How to do automation testing in web development?

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.


2 Answers

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)
like image 138
Sunil Goyal Avatar answered Sep 17 '22 00:09

Sunil Goyal


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

like image 35
Yaakov Bressler Avatar answered Sep 20 '22 00:09

Yaakov Bressler