0% found this document useful (0 votes)
3K views1 page

#Barebones App #Access Request Data #Useful Plugins: Rin Ted .Co M

The document provides a cheat sheet and quick reference for Flask, a Python-based web application framework. It summarizes key Flask concepts like creating a basic app, routing, accessing request data, redirects, templates, JSON responses, configuration, plugins, and session handling. Useful links are also included for additional Flask resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views1 page

#Barebones App #Access Request Data #Useful Plugins: Rin Ted .Co M

The document provides a cheat sheet and quick reference for Flask, a Python-based web application framework. It summarizes key Flask concepts like creating a basic app, routing, accessing request data, redirects, templates, JSON responses, configuration, plugins, and session handling. Useful links are also included for additional Flask resources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#Flask Cheat Sheet and Quick Reference

#Barebones App #Access Request Data


from flask import Flask [Link]['name'] #query string arguments
#Useful Plugins
[Link]['name'] #form data
app = Flask(__name__) Flask-PyMongo - [Link]
[Link] #request type
[Link]('cookie_name') #cookies
@[Link]('/hello') Flask-SQLAlchemy - [Link]
[Link]['name'] #files
def hello():
return 'Hello, World!' #Redirect Flask-WTF - [Link]

if __name__ == '__main__': from flask import url_for, redirect Flask-Mail - [Link]


[Link](debug=True)
@[Link]('/home') FLask-RESTFul - [Link]
#Routing def home():
Flask-Uploads - [Link]
return render_template('[Link]')
@[Link]('/hello/<string:name>') # [Link]/hello/Anthony
@[Link]('/redirect') Flask-User - [Link]
def hello(name):
return 'Hello ' + name + '!' # returns hello Anthony! def redirect_example():
return redirect(url_for('index')) #sends user to /home FLask-Login - [Link]
#Allowed Request Methods #Abort #Useful Links
@[Link]('/test') #default. only allows GET requests from flask import abort()
@[Link]('/test', methods=['GET', 'POST']) #allows only GET and POST. Flask Website - [Link]
@[Link]('/test', methods=['PUT']) #allows only PUT @[Link]('/')
def index(): Pretty Printed Website - [Link]
#Configuration abort(404) #returns 404 error
Pretty Pretty YouTube Channel -
render_template('[Link]') #this never gets executed
[Link]
#direct access to config
[Link]['CONFIG_NAME'] = 'config value'
#Set Cookie
from flask import make_response
#import from an exported environment variable with a path to a config file
[Link].from_envvar('ENV_VAR_NAME') @[Link]('/')
def index():
#Templates resp = make_response(render_template('[Link]'))
resp.set_cookie('cookie_name', 'cookie_value')
from flask import render_template return resp

@[Link]('/') #Session Handling


def index():
return render_template('template_file.html', var1=value1, ...) import session

#JSON Responses [Link]['SECRET_KEY'] = 'any random string' #must be set to use sessions

import jsonify #set session


@[Link]('/login_success')
@[Link]('/returnstuff') def login_success():
m
co
def returnstuff():
session['key_name'] = 'key_value' #stores a secure cookie in browser
num_list = [1,2,3,4,5]
d.
te
num_dict = {'numbers' : num_list, 'name' : 'Numbers'}
return redirect(url_for('index'))
i n
#returns {'output' : {'numbers' : [1,2,3,4,5], 'name' : 'Numbers'}}
#read session
y pr
return jsonify({'output' : num_dict})
t
et
@[Link]('/')
def index(): r
://p
if 'key_name' in session: #session exists and has key
ttp
session_var = session['key_value']
else: #session does not exist h

You might also like