Installing python
Please refer to the Python development environment setup to install Python and the IDE. Virtual Studio Code is recommended, and this article will use this IDE to demonstrate.
PIP acceleration
By default, PIP downloads software packages from foreign websites, but the download speed is too slow. We need to switch to domestic websites. The configuration mode is as follows:
# use the mirror stand to upgrade PIP PIP install -i https://mirrors.ustc.edu.cn/pypi/web/simple PIP -u PIP config set global. The index - a url https://mirrors.ustc.edu.cn/pypi/web/simpleCopy the code
Click on the configuration details
Installation of poetry
Poetry is the new Python dependency management tool (the equivalent of Node.js yarn/NPM and Ruby bundler) that makes it easier to manage dependencies in projects and is a few blocks ahead of PIP. Installation is also particularly simple:
PIP install poetry # verify the installed version of poetryCopy the code
Next we use poetry to create a new project hello-py
Poetry new hello-py CD hello-py poetry install #Copy the code
The above command creates a hello-py in the current directory. Open the directory and see the following:
Hello-py is a newly created Python module
Below the Tests directory is the unit test code
Using the Testing icon on the left, click Configure Python Tests > PyTest > Tests and you’ll see all the test cases. Click Run to execute this use case alone.
Pyptroject. toml is a configuration file for the project that records basic information about the project and required dependencies.
Check out Poetry’s official website for more information
Install fastAPI dependencies
In the previous section we installed dependencies using PIP; in this chapter we manage them using poetry. In Terminal, switch to the project directory and execute:
Poetry add fastapi uvicorn[standard] # ZSH please use poetry add fastapi "uvicorn[standard]Copy the code
When poetry installs dependencies, it logs them in the PyProject.toml file.
Coding!
Create a new main.py file and type the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def hello():
return {"message": "Hello World"}
Copy the code
Press F5 in the IDE and select FastAPI from the pop-up box to run
You normally see the following results
Enter http://127.0.0.1:8000 to view the result
Browser, type http://127.0.0.1:8000/docs, you can see swagger document, simple and happy.
Browser, type http://127.0.0.1:8000/redoc, you can see redoc format documents.
File modification automatically restarts the service
From the Run menu, select Add Configuration… If there is a pop-up window, select Python > FastAPI), which generates a launcher.json file,
Add a line –reload to args, and the final configuration file looks like this. This will automatically restart the file when it is modified.
{ // 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", "module": "uvicorn", "args": Main: [" app ", "reload", / / add the line], "jinja" : true}}]Copy the code
If you are PyCharm, add uvicorn.run() to main.py as follows:
from fastapi import FastAPI app = FastAPI() @app.get('/') def hello(): return {"message": "Hello World"} if __name__ == '__main__': import uvicorn uvicorn.run('main:app', reload=True) #reload will automatically restart the service when the file is modifiedCopy the code
Then execute the file