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?
Asked
Active
Viewed 1,627 times
-1
-
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 Answers
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)

George Verouchis
- 150
- 9
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.

SlasherZer0
- 76
- 2