Revolutionizing Web Development: Unleashing Speed and Efficiency with Python's FastAPI
MetricsViews Private Limited
Building A Solid DevOps Strategy With Cloud Native.
Quick Summary:
FastAPI is a modern web framework of Python like flask, Django. It is mostly used for building RESTful APIs in Python. FastAPI is built on top of Starlette and Pydantic. It is one of the fastest web frameworks available for Python. It stands out due to its speed, automatic data validation, built-in support for asynchronous programming and the amazing part is, it has in-built swagger documentation for testing API's.
?
Let’s see what API is.
?
API stands for Application Programming Interface.
Here is a realistic example of APIs: A customer went to a restaurant and orders a food, and the waiter goes to the chef and gives the order details. In technical terms, the client sends a login request or any request to the server through API's. API acts as a mediator between two software components to communicate with each other. It acts as a bridge between request and response.
History: FastAPI was created by Mexican guy Sebastian Ramirez and was first released in 2018. Because of fast execution of API operations, he named it FastAPI.
?
Features of FastAPI:
?
Installation:
?
1.???? Create a new project in vs code. Open terminal and give following commands to install FastAPI. As we know, pip is a package installer for Python. It is a tool that allows us to install and manage Python packages.
- pip install fastapi
?? 2.? We have to install ASGI server, that is uvicorn. So, whenever we make any changes in code, we don’t need reload uvicorn server again and again. Command to install uvicorn
- pip install "uvicorn[standard]"
?
Let’s create the First Program:
-?uvicorn main:app --reload
-??????? (Where uvicorn is the server, main is filename and app is instance.)
?
Note: The difference between the async/await function and in a normal function is that, async function pauses while awaiting its results and let other such functions run in the meantime.
领英推荐
?§? Alternative API docs: go to https://127.0.0.1:8000/redoc You will see the alternative automatic documentation provided by ReDoc.
?
Path Parameters:
?
A path parameter is variable part of a URL path. It points to a specific resource location. Path parameters allow us to include values directly in the path of our URL. In the above code snippet -( @app.get("/")) is the path parameter.
?
Query Parameters:
?
Any parameters other than the path parameters are known query parameters. These are the common types of parameters. The query is set of key-value pairs that go after the '?' in a that URL, and it is separated by & characters.
?
Request Body:
?
?
Conclusion:
FastAPI is a newly added to Python web frameworks. Despite being new to the market, it is already gaining a grip in the developer’s group. The execution speed of async programming with Python’s easiness makes it different from other frameworks. Throughout the article, we touched on several topics essential to get you started with Fast API.
?
Credit – Priyanka Divekar
?
MetricsViews Pvt. Ltd.
MetricsViews specializes in building a solid DevOps strategy with cloud-native including AWS, GCP, Azure, Salesforce, and many more.? We excel in microservice adoption, CI/CD, Orchestration, and Provisioning of Infrastructure - with Smart DevOps tools like Terraform, and CloudFormation on the cloud.