24
loading...
This website collects cookies to deliver better user experience
backend development
in python. In this post, you'll see a simple code demo of FastAPI.Python v3
$ python3 -v venv env
Windows
$ .\env\Scripts\activate
Linux or Mac
$ . env/bin/activate
(env) $ pip install fastapi "uvicorn[standard]"
main.py
fileImport FastAPI
from fastapi import FastAPI
Create FastAPI instance, app
app = FastAPI()
Write a route
, on which you will be hitting the server (Requesting for some Resource
).
@app.get("/")
Now, Write a function, which will be trigerred when you hit this API end-point
def index():
return {"msg": "Hello, World!"}
main.py
a this-from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def index():
return {"msg": "Hello, World!"}
(env) $ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.