20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
</>CodeWithHarry
Start Learning Tutorials
Flask Cheatsheet
"Flask cheatsheet for beginners"
By CodeWithHarry Updated: 5 April 2025
Importing Flask
from flask import Flask
Most Used Import Functions
These are some of the most used import functions by Flask developers:
from flask import Flask, render_template, redirect, url_for, request
Boilerplate Code
https://www.codewithharry.com/blogpost/flask-cheatsheet 1/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
This is the basic template or barebone structure of a Flask app:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
app.run()
Creating a Route
This is to make different endpoints in our Flask app:
@app.route("/")
Setting Allowed Methods
Used to specify which methods are allowed for a request. Allowing GET and POST requests on
an endpoint:
methods = ['GET', 'POST']
Re-run While Coding
This is used to automatically rerun the program when the file is saved:
app.run(debug=True)
Change Host
https://www.codewithharry.com/blogpost/flask-cheatsheet 2/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
This is used to change the host:
app.run(host='0.0.0.0')
Change Port
This is used to change the port:
app.run(port=80)
Importing SQLAlchemy
from flask_sqlalchemy import SQLAlchemy
Database URI
This is the database's address:
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql://username:password@localhost/db_name'
or
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
Initialization
This is used to initialize SQLAlchemy:
db = SQLAlchemy(app)
https://www.codewithharry.com/blogpost/flask-cheatsheet 3/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
Creating a Model
Class used to get data from the database and to send data to the database:
class TableName(db.Model):
column_1 = db.Column(db.Integer, primary_key=True)
column_2 = db.Column(db.String(80), nullable=False)
column_3 = db.Column(db.String(12), nullable=False)
Get All Data - all() Method
This is used to get all the data from the database:
data = ClassName.query.filter_by().all()
Filtered Data - first() Method
This is used to get the first dataset from the list returned by the filter_by function. You can get
targeted data by this:
data = ClassName.query.filter_by().first()
Send/Add Data to Database
This is used to send/add data to the database:
data_to_send = ClassName(column_1=dataset1, column_2=dataset2,
column_3=dataset3)
db.session.add(data_to_send)
db.session.commit()
Delete Data from the Database
This is used to delete data from the database:
https://www.codewithharry.com/blogpost/flask-cheatsheet 4/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
data_to_send = ClassName(column_1=dataset1, column_2=dataset2,
column_3=dataset3)
db.session.delete(data_to_send)
db.session.commit()
Request Method
This is used to know what request is made (GET/POST):
request.method
Render Template
This is used to pass and render an HTML file directly:
render_template("file.html")
Solving FSADeprecationWarning
SQLALCHEMY_TRACK_MODIFICATIONS allows you to disable the modification tracking system using
this line:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Creating Database Files
This is used to create database files:
from yourapplicationname import db
db.create_all()
exit()
https://www.codewithharry.com/blogpost/flask-cheatsheet 5/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
Method to Return Database Items
This is used to return database items:
def __repr__(self) -> str:
return f"{self.item}"
Printing Returned Content from the Method
This is used to print returned database items:
data = ClassNameWithMethod.query.all()
print(data)
Flask Documentation
Visit the Flask documentation here.
Flask SQLAlchemy Documentation
Visit the Flask SQLAlchemy documentation here.
Download this Cheatsheet.
Tags
Share
flask cheatsheet
Main Learn
Home Courses
Contact Tutorials
Work With Us Notes
https://www.codewithharry.com/blogpost/flask-cheatsheet 6/7
20/05/2025, 14:36 Flask Cheatsheet | Blog | CodeWithHarry
My Gear
Legal Social
Terms GitHub
Privacy Twitter (X)
Refund YouTube
Facebook
Made with ❤️ and ☕ in India
https://www.codewithharry.com/blogpost/flask-cheatsheet 7/7