FastAPI – The Simplicity

While studying in College and having multiple projects ideas in my mind. i always thinking why there is no such simple way to build the apis & now it seems i missed one of the option.

When it comes to building APIs, Python has always been a popular choice due to its ease of use, readability, and the large number of libraries available. One of the newer frameworks in the Python ecosystem is FastAPI. In this blog, we’ll take a look at what FastAPI is, why you might want to use it, and some of its key features.

What is FastAPI?

FastAPI is a modern, fast (hence the name), web framework for building APIs with Python 3.7+ based on standard Python type hints. It was created by Sebastián Ramírez and is an open-source project with a thriving community. FastAPI is built on top of Starlette for the web parts and Pydantic for the data parts.

Why use FastAPI?

One of the key benefits of using FastAPI is its speed. It is built on top of Starlette, which is an async web framework that is designed to be lightweight and fast. This means that FastAPI is able to handle a large number of requests quickly and efficiently, making it an excellent choice for building high-performance APIs.

FastAPI is also highly customizable, with a wide range of options for handling authentication, authorization, and other features. This means that it can be tailored to fit the needs of a wide range of projects and use cases.

Key Features of FastAPI

FastAPI has a number of key features that make it an attractive choice for building APIs:

  1. Fast
  2. Easy to use
  3. Automatic documentation generation
  4. Highly customizable
  5. WebSocket support
  6. Type checking

Let’s Build One Project Using FastApis –

Language Translation | English To Hindi

Below Piece of code will helps For Language Translation

from googletrans import Translator
translator = Translator()
translation = translator.translate("All The Best", dest='hi')
print(translation)
from googletrans import Translator
translator = Translator()

translation = translator.translate("All The Best", dest='hi')
print(translation)
from googletrans import Translator translator = Translator() translation = translator.translate("All The Best", dest='hi') print(translation)

Enter fullscreen mode Exit fullscreen mode

How to create Endpoints in FastApis –

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"}

Enter fullscreen mode Exit fullscreen mode

As we can use jinja templating in Fast Apis will create normal HTML Page Which will send Api Request & get the converted response from backend.

So out project folder structure will look like below –

How to send Api request to Backend –

<script>
const box = document.getElementById("box");
// const box1 = document.getElementById("value")
uploadform.onsubmit = async (e) => {
e.preventDefault();
let res = await fetch("/english_text", {
method: "POST",
body: new FormData(uploadform),
});
if (res.ok) {
let result = await res.text();
box.innerHTML = result;
} else {
box.innerHTML = `Response error: ${res.status}`;
};
};
</script>
    <script>
      const box = document.getElementById("box");
      // const box1 = document.getElementById("value")

      uploadform.onsubmit = async (e) => {
        e.preventDefault();

        let res = await fetch("/english_text", {
          method: "POST",
          body: new FormData(uploadform),
        });

        if (res.ok) {
          let result = await res.text();

          box.innerHTML = result;
        } else {
          box.innerHTML = `Response error: ${res.status}`;
        };
      };
    </script>
<script> const box = document.getElementById("box"); // const box1 = document.getElementById("value") uploadform.onsubmit = async (e) => { e.preventDefault(); let res = await fetch("/english_text", { method: "POST", body: new FormData(uploadform), }); if (res.ok) { let result = await res.text(); box.innerHTML = result; } else { box.innerHTML = `Response error: ${res.status}`; }; }; </script>

Enter fullscreen mode Exit fullscreen mode

Now We are all set to boot up the FastApi Server.

So let’s pull the code & follow below steps with python3.

git clone https://github.com/rahul188/Fast_API_Demo.git
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
uvicorn application:app --reload
git clone https://github.com/rahul188/Fast_API_Demo.git
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
uvicorn application:app --reload
git clone https://github.com/rahul188/Fast_API_Demo.git python3 -m venv env source env/bin/activate pip install -r requirements.txt uvicorn application:app --reload

Enter fullscreen mode Exit fullscreen mode

the most beautiful thing about FastApi is the documentation. as we can see in below it will generate Api document as soon as we write the code.

Conclusion

FastAPI is a modern, fast web framework for building APIs with Python 3.7+ that is built on top of Starlette and Pydantic. It is highly customizable, easy to use, and provides automatic API documentation generation. FastAPI’s speed and flexibility make it an excellent choice for building high-performance APIs that can be tailored to fit the needs of a wide range of projects and use cases.

原文链接:FastAPI – The Simplicity

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
Be happy for this moment, this moment is your life.
享受当下的快乐,因为这一刻正是你的人生
评论 抢沙发

请登录后发表评论

    暂无评论内容