FastAPI is a framework High-performance web API building in Python 3.6+. Let’s see how this framework works through exercise. To do this, let’s first define what an API is.
What is an API?
To build applications that are scalable and interactive, it is necessary that they be able to communicate with each other. Therefore, an API (short for Application Programming Interface) is a series of rules that facilitates communications between applications. These applications can be Python libraries or web servers, among others.
One of the main advantages of an API is that the applicant does not need to know the internal workings of the application or the language in which it is developed to be able to respond and vice versa. This allows different services using different technologies to communicate in a standard way.
FastAPI: application exercise
Now we are going to make an API, where we are going to make certain information available through it so that it can be consulted. We will do it using Python.
To do this, we will use one of the frameworks of Python called fastAPI, which is the framework par excellence that is used for productive topics, since it is the most complete.
We’re going to look at some simple examples of how it’s used and what we can see, and then we’ll delve into how to integrate it into it so that it has the ability to read a model.
As always, the first thing we will do is install dependencies:
#Install fastAPI dependencies! pip install fastapi [all] pyngrok streamlit
We reboot the kernel and continue.
How do we define our API? Basically we do a importthen we create the application, which will be what we are going to make available, and then we instantiate the fastAPI object.
Right now what we will do is create a first endpoint, that is, an entry point, which will help us consult. So the one we will implement is a get. The result of this route will simply be a «Hello World.»
#Basic complexity fastAPI exercise %%writefile main.py from fastapi import FastAPI app = FastAPI () @app.get (» / «) async def root (): return {«mesage»: «Hello World»} #fastAPI exercise of higher complexity %%writefile main.py from typing import Optional from fastapi import FastAPI from pydantic import BaseModel class Identity (BaseModel): name: str surname: Optional [str] = None app = FastAPI () @app.get (» / «) async def root (): return {«message»: «Helo World»} @app.post («/testing») async def testing (id: Identity ): if id.surname is None: message = f «Welcome to the API! My name is {id.name}» else: message = f «Welcome to the API! My name is {id.name} {id.surname }» return {«message»: message}
Now, to test it, we have the following cells, which are used to run a microservice. You can change the token that we have set if you log into ngrok with your username and password and click on the “Your Authtoken” option:
This is the code that you can modify. What this does is create a tunnelBasically, since if you want to enter collab from Google, you need a public IP. Therefore, what this cell does is allow you public access to a machine that is private.
So that, We can access the machine that is in Google from the internet.
We raise the code:
import nest_asyncio from pyngrok import ngrok, conf conf.get_default ().auth_token = «25TtT902JbR7LogkJKxk70jhCz6_4Z6JeyChZAau4gkr2th42″ ngrok_tunnel = ngrok.connect (8000) print (‘Public URL:’, ngrok_tunnel.public_ url) nest_asyncio.appy () To this URL we add a trailing slash (» / «).
We execute the following cell:
! uvicorn main:app –port 8000
What we will see is that, if we have set up the tunnel and put the URL that we received as a result in the browser, with the bar at the end, We access the starting route, which is the one we have generated, and it will return a message that says «Hello World».
Through this URL we can access the microservice.
We may see a message detecting some malicious element, but this is because the website does not have certificates:
In any case, we click on «visit this unsafe site» and the following message will appear, which is the one we have defaulted to:
Whats Next?
If you want to continue training in any of the numerous topics in the world of Big Data, at we offer you the guidance of great experts so that, in a few months, you become a great IT professional. Take a look at the Big Data, Artificial Intelligence & Machine Learning Full Stack Bootcamp syllabus and discover this intensive and comprehensive training that will catapult your career. Request more information now and take the leap that will transform your future!