Finally, it's time to create the "Hello World" with FastApi; that is, to implement the minimum necessary to be able to see something on the screen; so, we create a file inside the project (the tasks folder):
api.py
With the following code:
api.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def hello_world():
return {"hello": "world"}
In this first example, the first thing we do is load a class that provides access to the FastAPI framework:
from fastapi import FastAPI
With this class, we create an instance of FastApi:
app = FastAPI ()
Which gives us access to multiple features of the framework, such as creating the application routes.
We define a GET request for the root and this is done through a decorator like the one we can see below:
@app.get("/")
Of course, we can access other types of requests such as POST, PUT, PATCH or DELETE indicating the corresponding method which has its direct equivalent with the name of the request to use; that is, to send a GET request, we use the function get(), to send a POST request, we use the post() function.
As with other web frameworks, each request is processed by a function, in the previous example, the GET request for the root is processed by a function called hello_world() which all it does is return a dictionary indicating the "hello world" message:
@app.get("/")
def hello_world():
return {"hello": "world"}
With this, we have our first hello world example in FlastApi; but, in order to see this message on the screen, specifically any program that allows processing HTTP requests such as a browser, we have to open the server associated with the previous application and this is where we use the previously installed uvicorn server; to do this, from the terminal and root of the project, we use the following command:
$ uvicorn api: app --reload
With the previous command, we indicate the name of the file, which in this case is called api.py:
api:app
And that the server stays tuned for changes; that is, with the option of:
--reload
The server will be reloaded every time changes are made to the application.
When executing the previous command, we will see in the terminal:
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [15820] using StatReload
INFO: Started server process [4024]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:58209 - "GET / HTTP/1.1" 200 OK
INFO: 127.0.0.1:58209 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:58209 - "GET / HTTP/1.1" 200 OK
It tells us which is the route in which the application has been raised:
http://127.0.0.1:8000
And from this route, which we can execute in the browser, we will see the message of:
http://127.0.0.1:8000
{
"hello": "world"
}
If necessary, you can also customize the port using the port option and indicate the port you want to use, in this example, port 8001:
$ uvicorn api:app --port 8001
With the reload option:
$ uvicorn api:app --port 8001 --reload
And in the output you will now see that the server is loading the application on port 8001
Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
Finally, the previous function can also indicate the return data type:
@app.get("/")
def hello_world() -> dict:
return {"hello": "world"}
It is important to note that a GET type request is used, which is the typical type of request used to query data; and the only one that we can use directly from the browser without having to use a form.
- Andrés Cruz
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter
I agree to receive announcements of interest about this Blog.
!Courses from!
10$
On Udemy
There are 4d 15:53!
!Courses from!
4$
In Academy
View courses!Books from!
1$
See the books