admin管理员组

文章数量:1312662

My question is that are you able to run any code after the

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

in flask. I tried to print something after the above syntax to check and it only appeared after I exited the process. I was unable to find answer to this question.

If yes, please guide me on how to accomplish this. I need to run a block of code after the website has been launched. I am running on localhost if that helps.

PS: I am creating a website that downloads an image from the user's computer, runs a python script and predicts what the thing in the image is and tells whether the object is recyclable or not. This is for a school project. I tried to

print("check") 

and, as I use pycharm, stopped the process using the stop button and the following appeared:-

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
check

Process finished with exit code 0

My question is that are you able to run any code after the

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

in flask. I tried to print something after the above syntax to check and it only appeared after I exited the process. I was unable to find answer to this question.

If yes, please guide me on how to accomplish this. I need to run a block of code after the website has been launched. I am running on localhost if that helps.

PS: I am creating a website that downloads an image from the user's computer, runs a python script and predicts what the thing in the image is and tells whether the object is recyclable or not. This is for a school project. I tried to

print("check") 

and, as I use pycharm, stopped the process using the stop button and the following appeared:-

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
check

Process finished with exit code 0
Share Improve this question edited Jan 31 at 14:48 Shree_ML asked Jan 31 at 14:28 Shree_MLShree_ML 617 bronze badges 7
  • 3 Hi there. What do you need to run at that point? You can do setup at the top level higher up, or run other code in your routes. – Chris Commented Jan 31 at 14:31
  • 1 @Chris So basically, I need to accept an image an perform a function on it. That is after the website has been launched. I do not understand the setting up on higher levels. Could you please elaborate or provide me with some sources? Thanks! – Shree_ML Commented Jan 31 at 14:34
  • 1 What do you mean by "accept an image and perform a function on it"? Why it cannot be done before starting an app? Please edit your question to describe your needs better. – matszwecja Commented Jan 31 at 14:35
  • 1 Can you edit your question with the printing you added, and explain what exactly you did to exit the process that made the printing appear? – Mureinik Commented Jan 31 at 14:44
  • 2 You want to set up a route in flask that you post an image to and that route handler will process the image and return a response. – JonSG Commented Jan 31 at 14:57
 |  Show 2 more comments

3 Answers 3

Reset to default 4

app.run() is a blocking call. Until you hit CTRL+C (or use pycharm to break out of it), it won't progress to the next line, where your print statement is.

When you run:

app.run()

It should be the final piece of code, if you would like to confirm that the website works, then try to connect to it via HTTPS with "http://127.0.0.1:5000". If all works, the console should display that connected via HTTPS & Should show your IP address.

As said by @JonSG and @Chris, I needed to create a route method before the

app.run()

to make the code work. Thanks!

本文标签: pythonUnable to run any code after launching the app in FlaskStack Overflow