Python has plenty of tools to help make developers' lives easier. In this tutorial we'll take a look at how we can use a few of these tools to create applications suited to achieve a variety of different use cases.
Flask
Flask is a lightweight framework that is suitable for a variety of different use cases. With minimal overhead, it is capable of quickly standing up working applications for APIs, rendering templates, custom logic, and a lot more.
For speedy APIs and quickly creating projects for small teams, flask is a great pick. It only takes a few lines of code to get flask working:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
FastAPI
FastAPI is a great tool for doing exactly as you would expect, creating very performant, fast API applications.
There is a bit more to understand with fastAPI which is fully covered in their documentation. However, in a few sentences it is very performant because of the way payloads are received. Instead of inferring types, FastAPI makes you declare the type of the variable you want to receive from the payload - This forces that type. If the type cannot be forced, an error is sent to the sender of the post request.
Here's a few lines of fastAPI code to get you started:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name : str
description : Union[str, None] = None
price : float
tax : Union[str, None] = None
app = FastAPI()
@app.get("/")
def home():
return {'data': 'Hello world'}
@app.get("/search/{item}")
def search(item: str):
return {'data' : item}
@app.post("/items/")
def create_item(item : Item):
return item
Django
Django is a great framework for creating a full scale project. It has been highly worked with and made additional tools have been developed for it. Because of all of the out of the box tools django comes with, it can quickly set up complex applications. With greater power comes greater responsibility, so you will need to learn what each of the components of a django application does.
Along with being highly tooled, django has over the years developed best practices for using its framework. This allows new members on django projects to more or less know what's going on within the project and more easily contribute compared to a flask application. With more custom application logic, such as with flask applications, the harder it is to bring in new developers for the project.
Here's some sample django code featured in the final video: