chore [examples]: move non-app.run() locs out of if __name__ == "__main__" guard - #2828
chore [examples]: move non-app.run() locs out of if __name__ == "__main__" guard#2828samialfattani wants to merge 7 commits into
Conversation
|
Shouldn't the test container LOCs be under the guard? You would not want to run those. |
test container and build db logic should be under the guard
hmmm, actaully i want to run all LOCs including test containers and db, that because the user should have Docker installed in the local machine. however, to the best of my knowledge, the gurad is for only it would be better if you give a certain case that we need to run those LOCs under the gurad. |
|
The guard is for anything you don't want to be run when you import the file. I think testcontainers set up is part of that |
I still don't get it, when we import the file definetly i want to run the testcontainers LOCs so that the container can be run on Docker !! |
no, because maybe you want to run the main.py with a production grade server and you don't need to run docker since you have the right environment. With your PR, running docker is now a side effect when you import the file. |
Now I read better your requirements. Would the python debugger work with testcontainers? If yes, I see your point in moving testcontainers out of the guard. |
|
yes it works fine , in that since i think even database creation should be out of th guard |
|
if using the python debugger is your objective, ok. But FYI the best practice would be different |
|
OK , you can review now |
|
now it is ready to review |
| ) as postgres: | ||
| app = Flask(__name__) | ||
| app.config["SECRET_KEY"] = "secret" | ||
| print(f"Using database URL: {app.config['SQLALCHEMY_DATABASE_URI']}") |
There was a problem hiding this comment.
this looks like it would raise an error: the config for this key is defined in the next line
|
Many examples now perform db destruction and initialization or depend on runtime resources during module import (datetime_timezone, peewee_simple, geo_alchemy, mongoengine). There are also unrelated formatting/logging changes and now examples/sqla/main.py does not have StrictUndefined anymore, which is fine but outside this PR scope. Could we limit this PR to the guard relocation and revisit the examples that require testcontainers or destructive setup? If you need the db initalization to happen when you run the example with the python debugger using VScode or other tool and therefore need it to happen outside the main guard, I would suggest something like For examples using testcontainers, |
This PR just to relocate the running gurard
if __name__ == "__main__":into the right place.Reason of this PR: the running guard has to be only for
app.run()so that the server won't be executed twice while the other code should be out of theifstatement. It is useful to run the example with the python debugger using VScode or other tool, however in such cases, the server will be run implicitly (usingflask runcommand) without the line ofapp.run()