Hi Guys!
We are all know about API and of course some of us have used it already and some of us are building it already in different languages. Here we lets see about Fast Api(one of the popular api module in python),why its so popular?,Its Pros and How to build it!
Let's HiTπ₯π₯
At First What is Fast Api?, why Fast Api? ,we have well established frameworks in python like Django, Flask why we need to use Fast Api?
What is Fast Api?
FastAPI is an ASGI web framework
Why Fast Api?
Here some of reason to use Fast Api stated by the creator
Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
Fast to code: Increase the speed to develop an api
Fewer bugs: Reduce about 40% of human (developer) induced errors
Intuitive: Great editor support. Completion everywhere. Less time debugging.
Easy: Designed to be easy to use and learn. Less time reading docs.
Robust: Get production-ready code. With automatic interactive documentation
The feature i really like about Fast Api is documentation, offered by (Redoc and Swagger UI) its very Easy to debug and Test the Api you build!! Alright. Story time ends Here!! its time to build One
Its Hero timeπ !
1.initially lets create an virtual environment
syntex:python3 -m venv <path you wish to create>
Eg:python3 -m venv /Users/d.r/carrotpy/
2.Now a Folder is created at the path you specify, open the folder in any of the IDE (says VS-Code since its Famous)
In order to use venv we need to activate it
navigate into the folder and give the command
syntex: source bin/activate
3.Install the Fast Api and uvicorn into the venv
pip install fastapi
pip install uvicorn
4.Here the code to get the vegetable name and count from the user and return it
from typing import Optional
# if we need some parameter to be optional we use typing
import uvicorn
#it is ASGI
from fastapi import FastAPI
#importing Fast Api
app = FastAPI()
#declaring Fast Api
#start of Decorators
#When you hit the address http://127.0.0.1:8000/ This will return {"Hello": "World"} as dict
@app.get("/")
def carrotgreetings():
return "Hello Rabbit"
# this will retun the vegtable name and its count
@app.get("/veglist/{vegtable}/")
def read_item(vegtable: str, count: Optional[str] = None):
return {"vegtable": vegtable, "count": count}
if __name__ == "__main__":
uvicorn.run("fastapi_demo:app", host='127.0.0.1', port=8000, reload=True)
5.Run the Code by the comment
Syntex:`python3 <filename>`
Eg: python3 fastapi_demo.py
now hit the api by clicking http://127.0.0.1:8000/
You can see the docs by http://127.0.0.1:8000/docs
You can pass the Vegtable names and count by
http://127.0.0.1:8000/veglist/carrot/?count=2
wow! we made the api....
Top comments (1)
Fast Api is pretty good I used it in a previous project.