Revolutionizing Web Development: 
Unleashing Speed and Efficiency with Python's FastAPI

Revolutionizing Web Development: Unleashing Speed and Efficiency with Python's FastAPI

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:

  • Fast to code
  • Very high performance
  • easy to use and learn
  • Based on OpenAPI for API creation
  • Great editor support
  • Minimize code duplication.
  • Asynchronous Server Gateway Interface (ASGI) support
  • In built swagger documentation

?

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:

  • In the code snippet, we imported the FastAPI class from the fastapi module we just installed.
  • App is the instance created inside main module. To run the file, hit command

-?uvicorn main:app --reload        

-??????? (Where uvicorn is the server, main is filename and app is instance.)

  • The @app.get(“/”) is responsible for path operation, which is a decorator. It indicates that the below function is responsible for handling requests that is on “/” path using the get operation.
  • The get operation is one of the HTTP operations like Post, Put and Delete, etc.
  • The async function returns a dictionary.

?

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.


  • Once your application is running, open your web browser and navigate to https://127.0.0.1:8000. You should see the "Hello, World!" message.
  • Now go to https://127.0.0.1:8000/docs, you will see automatically generated API documentation provided by Swagger UI. It will look like this


?§? 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:

?

  • When we have to send data from a client to API, we send it as a request body.
  • A request body is data sent by the client to API
  • To send data, you should use one of: POST, GET, PUT, DELETE, or PATCH.
  • GET: This method is used to retrieve data from the server side. This is a read-only method.
  • POST: This method sends data to the server and creates a new resource.
  • PUT: This method is most often used to update an existing resource.
  • PATCH: This method is very similar to the PUT method because it also modifies an existing resource. The difference is that in put method, request body contains the complete new version, whereas in? PATCH method, the request body only needs to contain the specific changes to the resource.
  • DELETE: The DELETE method is used to delete a resource.

?

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.

www.metricsviews.com

要查看或添加评论,请登录

MetricsViews Private Limited的更多文章

社区洞察

其他会员也浏览了