4

I have a flask app

@app.route("/hello")
def generater():
     return "hello world"

if __name__ == '__main__':
    app.run()

My application runs fine, but i would like to know how I could make a request to http://127.0.0.1:5000/hello when I compile my code

rabiaasif
  • 305
  • 3
  • 6
  • 21

3 Answers3

8

You can use webbrowser to automatically open http://localhost:5000 in a web browser when running your flask app:

import webbrowser
...

if __name__ == '__main__':
    webbrowser.open('http://localhost:5000')
    app.run()
3

There are a lot of ways you could do this. You could just open up your browser to that location. You could try @jimtodd's answer and then cURL the endpoint from another terminal window.

To do it in the code, which I guess is what you want, Flask offers you some helper methods. For example there is: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_first_request

You can use it like this:

def foo():
    pass

app.before_first_request(foo)

In the case where you want to run a script strictly on run, not just before the first request, this solution is good: Run code after flask application has started -- I guess you would use this for cold-start problems as well.

Charles Landau
  • 4,187
  • 1
  • 8
  • 24
-1

You can do this from command prompt:

set FLASK_APP=hello.py
python -m flask run

The you will see.... Running on http://127.0.0.1:5000

Now you can check the output in your browser.

Jim Todd
  • 1,488
  • 1
  • 11
  • 15