0% found this document useful (0 votes)
6 views

Flask Cheatsheet _ CodeWithHarry

flask cheatsheat

Uploaded by

fahim0gamer2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Flask Cheatsheet _ CodeWithHarry

flask cheatsheat

Uploaded by

fahim0gamer2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry

</> CodeWithHarry Menu Login

Flask Cheatsheet
Haris Ali Khan

July 1, 2022 2 min read

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
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/ 1/6
10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry
This is used to change the host.

app.
app.run
run(
(host
host=
='0.0.0.0'
'0.0.0.0')
) Free ChatGPT Extension Open

Change port
This is used to change the port.

app.
app.run
run(
(port
port=
=80
80)
)

Importing SQLAlchemy

from flask_sqlalchemy import SQLAlchemy

Database URI
This is the database's address.

app.
app.config
config[
['SQLALCHEMY_DATABASE_URI'
'SQLALCHEMY_DATABASE_URI']
] = 'mysql://username:password@localhost/db_name'

or

app.
app.config[
config['SQLALCHEMY_DATABASE_URI']
'SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'

Initialization
This is used to initialize SQLAlchemy.

SQLAlchemy(
db = SQLAlchemy (app
app))

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

TableName(
class TableName (db
db.
.Model
Model)
):
db.
column_1 = db .Column
Column((db
db.
.Integer
Integer,, primary_key
primary_key=
=True
True)
)
db.
column_2 = db .Column
Column((db
db.
.String
String(
(80
80)
), nullable
nullable=
=False
False)
)
db.
column_3 = db .Column
Column(
(db
db.
.String
String(
(12
12)
), nullable
nullable=
=False
False)
)

Get all data - all() method


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

ClassName.
data = ClassName.query
query.
.filter_by
filter_by(
().all
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 targetted data by this.

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

Send/add data to database

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 2/6
10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry
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.

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()

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
https://www.codewithharry.com/blogpost/flask-cheatsheet/ 3/6
10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry
Visit the Flask documentation here

Flask SQLAlchemy Documentation


Visit the Flask SQLAlchemy documentation here

Download this Cheatsheet

Add a new comment

Type Your Comment

Post Comment

Comments (12)

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

REPLY

yasharthmishra2000_gm 2023-06-29

Bhai flask alchemy chal nhi raha hai pip install karne ke baad bhi no
such module dikha raha hai kya karu plz help...

VIEW ALL REPLIES

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 4/6
10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry

REPLY

sahilchanna14_gm 2023-02-07

bhai cheatsheet acchi hai but numpy aur pandas ki bhi nikaldo naa

VIEW ALL REPLIES

REPLY

ishantgarg.2020_gm 2022-12-17

Hi.. In your YT video of flask you used heroku for deployment flask app.
But there free version is no more. Can you make another one video
regarding deployment of flask app free. Thanks

VIEW ALL REPLIES

REPLY

forvideoderapp_gm 2022-12-16

nice

REPLY

anuragintensenagar1999 2022-07-19

thanks bhai

REPLY

alihaider00760 2022-07-15

The SuperMan of Programming. Harry bhia Zindaaaabaaaad

VIEW ALL REPLIES

REPLY

vt7588976_gm 2022-07-11

This is very helpful for us. Thank you Harry Bhai.

REPLY

himanshukumark74_gm 2022-07-09

Nice sir thanku

REPLY

This site uses Google AdSense ad intent links. AdSense automatically generates these links and they may help creators earn money.

CodeWithHarry Copyright © 2024 CodeWithHarry.com

https://www.codewithharry.com/blogpost/flask-cheatsheet/ 5/6
10/13/24, 6:12 PM Flask Cheatsheet | CodeWithHarry

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

You might also like