Started with fastapi python.
this is how I am string my server
class Server:
def __init__(self):
self.app = FastAPI()
def runServer(self, host: str, port: int,is_dev:bool):
uvicorn.run(self.app, host=host, port=port,debug=is_dev)
if __name__ == "__main__":
server = Server()
# read the environment variables
host: str = os.environ['host']
port: int = int(os.environ['port'])
is_dev: bool = bool(os.environ['dev'])
server.runServer(host, port, is_dev)
this ups the server but not running in auto reload mode if i make any changes.
even I tried
uvicorn.run(self.app, host=host, port=port, reload=is_dev)
reload is i guess not an option, thus causing the server to break.
i tried passing --reload
args in launch.json even but still not working
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: FastAPI",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py",
"jinja": true,
"args": ["--reload"],
"env": {
"host": "127.0.0.1",
"port": "5555",
"dev": "true"
}
}
]
}
any idea? am i missing something?
Hit Ctrl + Shift + D to open the Run and Debug window. Find the dropdown and choose the value that has value Python: FastAPI we provided for the key name in our debug configuration in launch. json file. Select Python: FastAPI and click on the green button.
To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.
If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.
uvicorn
will start in the reload mode only if the app
argument is a string in the format <module>:<app_instance>
and reload
or debug
argument are true. Like this:
if __name__ == "__main__":
uvicorn.run("example:app", host="127.0.0.1", port=5000, reload=True)
Excerpt from the documentation:
Note that the application instance itself can be passed instead of the app import string.
uvicorn.run(app, host="127.0.0.1", port=5000, log_level="info")
However, this style only works if you are not using multiprocessing (
workers=NUM
) or reloading (reload=True
), so we recommend using the import string style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With