- run code from book to see why the hell mine isnt working [specially ginja]
- watch tutorials of fast api
- Make API For Admin [get login page , post admin-info , get admin-info ] (admin panel)
- Frontend Admin with Ginja (admin panel)
- database connection with fast api (admin panel)
- authentication with fast api (admin panel)
- see how frontend data pass to backend
- backend to databse
- A super fast Python Web Framework
- Other framework : Django / Flask / other
- fast api is very very modern
- has a very nice feature of a synchronous programming which is still lacking in django framework
Best thing for the fast API
1. Swagger UI
- genrally we use the postman but thats not a good thing.
- So by default, fast api provide you the swagger ui where we can simply check what are the routes we have created or api endpoints we have created.
- We can also try them out check what is we are creating , how it is going to respond
2. ReDoc
- having a very nice and minimal design for the documentation
- Python 3.6 with type using Pydantic library
- No new syntax to learn. Just standard modern Python.
- JSON schema
- by default returns json which every modern api need to communicate with other things
- Open API
- is a linux foundation under linux foundation and it defined how we create our api
- vs code : autocomplete feature
- pycharm : autocomplete
- HTTP Basic
- OAuth2 (also with JWT tokens)
- API keys is
- Headers
- Query parameters
- Cookies, etc.
FastAPI is actually a sub-class of Starlette. So, if you already know or use Starlette, most of the functionality will work the same way.
- WebSocket support
- GraphQL support
- In-process background tasks
- Startup and shutdown events
- Test client built on requests
- CORS, GZip, Static Files, Streaming responses
- Session and Cookie support
- SQL databases
- NOSQL databases
- GraphQL
- Install and Setup
- Break it down, how it structured
- Path Prameters
- API Docs - swagger / redocs
- Query Pramaeters
- Request body
- Debugging FastAPI
- Pydantic Schemas
- SqlAlchemy database connection
- Models and table
- Store blog to database
- Get blogs from database
- Delete
- Update
- Handling Exception
- Return response
- Define response model
- Create User
- Hash User Password
- Show single user
- Define docs tags
- Define User to Blog Relationship
- Define Blog to user Relationship
- API Router
- API Router with parameters
JWT - Json Web Token
- Create Login route
- Login and verify password
- Return JWT access token
- Routes behind authentication
- using Deta.sh website to deploy
We can also see the same thing with redocs
https://github.com/tiangolo/fastapi
Python 3.6+ version
fastapi: The framework on which we’ll build our application. uvicorn: An Asynchronous Server Gateway Interface module to run our application.
- Install Python 3.6 or greater version
- Install fast api Overall install or for venv
pip install fastapi
or
pip3 install fastapi
- Install Uvicorn
pip install uvicorn
or
pip3 install uvicorn
check version
uvicorn --version
- Code
main.py
from typing import Union
from fastapi import FastAPI
# Creating new instance of FASTAPI
app = FastAPI()
@app.get("/")
def index():
return 'heyy'
Note :Instead of app = FastAPI()
we can also write app = APIRouter()
- Execute on terminal to run Server
uvicorn main:app --reload
We have see how we can run a little demo of app with fast api app. Now we will explain the steps in here.
-
Activate virtual environment
env/Scripts/activate
-
We will create a new instance of FastAPI as follows.By instantiating FastAPI in the app variable, we can proceed to create routes
In app.py or main.py add
from fastapi import FastAPI app = FastAPI()
-
A route is created by first defining a decorator to indicate the type of operation, followed by a function containing the operation to be carried out when this route is invoked.
In the following example, we’ll create a "/" route that only accepts GET requests and returns a welcome message when visited:
@app.get("/") async def welcome() -> dict: return { "message": "Hello World"}
-
The next step is to start our application using uvicorn. In your terminal, run the following command:
uvicorn main:app --reload
In the preceding command, uvicorn takes the following arguments:
- file:instance: The file containing the instance of FastAPI and the name variable holding the FastAPI instance.
- --port PORT: The port the application will be served on.
- --reload: An optional argument included to restart the application on every file change.
-
The response from the application logged in your console will be the following:
{"message":"Hello World"}
-
Routing is an essential part of building a web application. In fast api, it is flexible and hassle free.
-
Routing in fast API is flexible and hassle free
-
It is the process of handling HTTP requests sent from a client to the server
-
HTTP requests are sent to defined routes, which have defined handlers for processing the requests and responding. These handlers are called route handlers.
Note: An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.
By the end of this chapter, we will know how to create routes using the APIRouter instance and connect to the main FastAPI application.
Also will learn what path and query parameters are and how to use them in our FastAPI application
The knowledge of routing in FastAPI is essential in building small- and large-scale applications.
In this chapter , we will be covering :
- Routing in FastAPI
- The APIRouter class
- Validation using Pydantic models
- Path and query parameters
- Request body
- Building a simple CRUD app
- A route is defined to accept requests from an HTTP request method and optionally take parameters
- When a request is sent to a route, the application checks whether the route is defined before processing the request in the route handler.
- On the other hand, a route handler is a function that processes the request sent to the server
An example of a route handler is a function that retrieves records from a database when a request is sent to a router via a route.
- HTTP methods are identifiers for indicating the type of action to be carried out
- The standard methods include GET, POST, PUT, PATCH, and DELETE. You can learn more about HTTP methods below :
GET
- The GET method requests a representation of the specified resource.
- Requests using GET should only retrieve data.
HEAD
- The HEAD method asks for a response identical to a GET request, but without the response body.
POST
- The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.
PUT
- The PUT method replaces all current representations of the target resource with the request payload.
DELETE
- The DELETE method deletes the specified resource.
CONNECT
- The CONNECT method establishes a tunnel to the server identified by the target resource.
OPTIONS
- The OPTIONS method describes the communication options for the target resource.
TRACE
- The TRACE method performs a message loop-back test along the path to the target resource.
PATCH
- The PATCH method applies partial modifications to a resource.
- create todo.py
main.py
from typing import Union
from fastapi import FastAPI
from todo import todo_router
# Creating new instance of FASTAPI
app = FastAPI()
@app.get("/")
async def say_hello() -> dict:
return {"message": "Hello!"}
app.include_router(todo_router)
todo.py
from typing import Union
from fastapi import FastAPI, APIRouter
# Creating new instance of FASTAPI
todo_router=APIRouter()
todo_list = []
@todo_router.post("/todo")
async def add_todo(todo: dict) -> dict:
todo_list.append(todo)
return {"message": "Todo added successfully"}
@todo_router.get("/todo")
async def retrieve_todos() -> dict:
return {"todos": todo_list}
Then execute :
(venv) uvicorn main:app --reload
todo.py is same as before
main.py
from typing import Union
from fastapi import FastAPI
from todo import todo_router
# Creating new instance of FASTAPI
app = FastAPI()
@app.get("/")
async def say_hello() -> dict:
return {"message": "Hello!"}
app.include_router(todo_router)