0% found this document useful (0 votes)
2 views8 pages

Flask Cheatsheet - CodeWithHarry

The Flask Cheatsheet provides essential code snippets and functions for Flask app development, including importing Flask, creating routes, and setting allowed methods. It covers database integration with SQLAlchemy, including initialization, creating models, and performing CRUD operations. Additionally, it offers tips on debugging and rendering templates, along with links to Flask documentation.

Uploaded by

chainedboy99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Flask Cheatsheet - CodeWithHarry

The Flask Cheatsheet provides essential code snippets and functions for Flask app development, including importing Flask, creating routes, and setting allowed methods. It covers database integration with SQLAlchemy, including initialization, creating models, and performing CRUD operations. Additionally, it offers tips on debugging and rendering templates, along with links to Flask documentation.

Uploaded by

chainedboy99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

3/31/25, 10:49 Flask Cheatsheet |

PM CodeWithHarry

Flask
Cheatsheet
Haris Ali Khan

July 1, 2022 2
Importing min read

Flask
ffrroomm flask iimmppoorrtt Flask

Most used import functions


These are some of the most used import functions by flask
developers

ffrroomm flask import Flask, render_template, redirect, url_for, r

Boilerplate code
This is the basic template or barebone structure of a Flask app

ffrroomm flask

iimmppoorrtt Flask app =

Flask( name

Creating a"//"")
) @app.route("

route
ddeeff hheelllloo
https://www.codewithharry.com/blogpost/flask-cheatsheet/ 1/8
3/31/25, 10:49 PM Flask Cheatsheet | CodeWithHarry

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 = [''GGEETT'', ''PPOOSSTT'']

Re-run while coding


This is used to automatically rerun the program when the file is
saved.

app.run(debug=TT rr uu ee )

Change host
This is used to change the host.

app.run(host=''00..00..00..00'')

Change port
This is used to change the port.

Importing
app.run(port=88 00 )
SQLAlchemy
ffrroomm flask_sqlalchemy iimmppoorrtt

SQLAlchemy

Database
URI
https://www.codewithharry.com/blogpost/flask-cheatsheet/ 2/8
3/31/25, 10:49 Flask Cheatsheet |
PM CodeWithHarry
This is the database's address.

app.config[''SSQQLLAALLCCHHEEMMYY DDAATTAABBAASSEE UURRII''] =

''mmyyssqqll::////uusseerrnnaammee::ppaassss

or

app.config[''SSQQLLAALLCCHHEEMMYY DDAATTAABBAASSEE UURRII''] =

''ssqqlliittee:://////tteesstt..ddbb''

Initialization
This is used to initialize SQLAlchemy.

db = SQLAlchemy(app)

Creating a Model
Class used to get data from the database and to send data to the
database.

ccllaassss TTaabblleeNNaammee(db.Model):

column_1 = db.Column(db.Integer, primary_key=TT rr uu ee )

column_2 = db.Column(db.String(8800), nullable=FFaallssee)

column_3 = db.Column(db.String(1122), nullable=FFaallssee)

Get all data - all() method


This is used to get all the data from the database.

data = ClassName.query.filter_by().aallll()

Filtered data - first() method


https://www.codewithharry.com/blogpost/flask-cheatsheet/ 3/8
This is used to get the first dataset from the list returned by the
3/31/25, 10:49 PM Flask Cheatsheet |
CodeWithHarry
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,


db.session.add(data_to_send)
db.session.commit()

Delete data from the database


This is used to delete data from the database.

data_to_send = ClassName(column_1=dataset1, column_2=dataset2,


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(""ffiillee..hhttmmll"")

Solving FSADeprecationWarning
SQLALCHEMY_TRACK_MODIFICATIONS allows you to disable the
modification tracking system using this line:

app.config[''SSQQLLAALLCCHHEEMMYY TTRRAACCKK

MMOODDIIFFIICCAATTIIOONNSS''] = FFaallssee

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 4/8
3/31/25, 10:49 Flask Cheatsheet |
PM CodeWithHarry
Creating Database
files
This is used to create database files

ffrroomm yourapplicationname

iimmppoorrtt db db.create_all()
exit()

Method to return
</> CodeWithHarry Logi
database items
Menu n

This is used to return database items.

def __repr__(self)
rreettuurrnn -> str:

ff""{self.item}""

Printing returned content from


the method
This is used to print returned database items. Copy

data = ClassNameWithMethod.query.aallll()

pp rr ii nn tt (data)

Flask Documentation
Visit the Flask documentation here

Flask SQLAlchemy
Documentation
Visit the Flask SQLAlchemy documentation
Download this
here
Cheatsheet

Add a new
comment
https://www.codewithharry.com/blogpost/flask-cheatsheet/ 5/8
3/31/25, 10:49 Flask Cheatsheet |
PM CodeWithHarry
Type Your Comment

Post
Comment

Comments
(18)
tejasayarekar7_gm 2025-02-26
same here problem not downloading from my end also if
anyone get please email( [email protected])

REPLY

zeeshanulhaq04 2025-02-15
this Cheatsheet is not downloading from my side kindly send
this code on my Gmail([email protected]), i'm
begginer and follow your course and youtube channel.

REPLY

sharmaaditya8463 2025-01-31
if anyone get this cheatsheet can you please share it on
this email ([email protected]) Thanks in
advance. :)

REPLY

hamoabbasi3256_gm 2024-12-25
Thanks Harry bhaiyaa

REPLY

sameer673.thakur673
2024-12-01

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 6/8
3/31/25, 10:49 Flask Cheatsheet | CodeWithHarry
PM
Hello Harry Bhaiya, please help me with my flask project , I
am in very big trouble, you are my last hope
VIEW ALL
REPLIES

REPLY

hamidtalisvi 2024-10-31
sir it is not downloading please send me flask cheatsheet at
this gmail [email protected]

REPLY

beersinghkaran036_gm 2024-07-25
Hi Sir I am Karan and the flask cheatsheets is not
downloading and please send notes on
[email protected] par send kar de na if you
time.

REPLY

princerajsinha863_gm 2024-07-23
It is not downloading

REPLY

ashishtripathipara_gm 2024-07-09
Bhai [email protected] pe cheatsheet send
kar dijiye mujhase download nahi ho raha hai please

REPLY

krish061521_gm 2024-06-16
#HarryBhai apki website me C language, C++, Java and
Flask ki cheatsheet download nahi ho rahi hai Maine cyber
cafe me jakar download kia waha par bhi nahi hua apki
website me kuchh technical issues hai isliye cheatsheet
download nahi ho rahi hai please aap jaldi se is problem ka
solution laaiye
https://www.codewithharry.com/blogpost/flask-cheatsheet/ 7/8
3/31/25, 10:49 Flask Cheatsheet |
PM CodeWithHarry
REPL
Y

CodeWithHarry
Managed by CWH Privacy Terms Shop Contact
Solutions Refund

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 8/8

You might also like