-1

I want to start my Flask application directly in the browser when I run my Python file. I now just copied the localhost address into my browser and started it that way. Is it possible to do it without copying the link every time?

davidism
  • 121,510
  • 29
  • 395
  • 339
  • Which operating system? On Windows, you can do `os.system("start http://localhost:8080")` after you set up your socket. – Tim Roberts May 07 '21 at 17:25

2 Answers2

0

Try this:

import webbrowser

from flask import Flask
app = Flask(__name__)

#your staff
@app.route("/")
def hello():
    return("Hello World!")

def open_browser():
    webbrowser.open_new('http://127.0.0.1:5000/')

if __name__ == "__main__":
    # the command you want
    open_browser()
    app.run(port=5000)
0

Use the webbrowser module:

import webbrowser
webbrowser.get("google-chrome").open("https://www.bing.com")

You can replace 'google-chrome' with another browser you would like.