Skip to main content

Flask cheatsheet

 


Flask Cheatsheet

Loading...

Importing Flask

from flask import Flask

Most used import functions

These are some of the most used import functions

from flask import Flask, render_template, redirect, url_for, request

Boilerplate

This is the basic template or barebone structure of Flask.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

app.run()

route(endpoint)

This is to make different endpoints in our flask app.

@app.route("/")

Route method

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

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)

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)

Creating Model

Class to get data from 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())

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

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

Filtered data(.first())

This is used to get the first dataset from the list returned by the filter_by function. You can get targetted 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.

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 whole html file directly.

render_template("file.html")

FSADeprecationWarning

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True|False

Creating Database file

This is used to create a database file

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://flask.palletsprojects.com/en/latest/

Flask SQLAlchemy Documentation

https://flask-sqlalchemy.palletsprojects.com/en/2.x/

Comments

Popular posts from this blog

Advanced Java Solved manual 5th Sem (Information Technology)

By Harsh Tambade Contributor : Vishal Chavare  Advanced Java Programming Solved Manual 22517                    Advanced Java Programming Solved Manual 22517 Download Now👇👇 Practical No. 1  Practical No. 2  Practical No. 3  Practical No. 4  Practical No. 5  Practical No. 6  Practical No. 7  Practical No. 8  Practical No. 9  Practical No.9 Practical No. 12 Practical No. 16 Practical No. 18 Practical No.18 Practical No. 19 Practical No .19 Practical No. 20 Practical No. 21 Practical No. 22 Practical No. 23 Practical No. 24   Advanced Java Programming Solved Manual 22517 here's also a full solved manual but the clarity is not too good so please refer the below links: download full manual

HR interview

  HR Interview Questions | Common Interview Questions A list of frequently asked  HR interview questions  or  Common interview questions  or  Job interview questions  and answers are given below. 1) Tell me about yourself? This is the most famous question for an interviewer and also most difficult to answer this question for the candidate. This question puts all the pressure on the candidate, and the interviewer relax. You should alert enough to answer this question. You should start with an easy and confident tone and answer in a proper manner. It should not be scripted. Always remember, you are not giving the interview to a robot so your articulation, your pronunciation of each word should be clear and confident. A good way: Analyze your interviewer interests. Express your most important accomplishments first. Possible Answer 1 "Good morning/afternoon/evening" sir/mam. First of all, thank you for giving me this opportunity to introduce myself. My nam...

How to experience windows 11 online without installing it on your pc

Link to enjoy the all new windows 11 (online) :  https://win11.blueedge.me/ You can also try it on mobile but can give some crashes and bugs so please use it on pc.   Windows has always existed to be a stage for the world’s innovation. It’s been the backbone of global businesses and where scrappy startups became household names. The web was born and grew up on Windows. It’s the place where many of us wrote our first email, played our first PC game and wrote our first line of code. Windows is the place people go to create, to connect, to learn and to achieve – a platform over a billion people today rely on.