Flask Cheatsheet PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

#Flask Cheat Sheet and Quick Reference

#Barebones App #Access Request Data


from flask import Flask request.args['name'] #query string arguments
#Useful Plugins
request.form['name'] #form data
app = Flask(__name__) Flask-PyMongo - http://readthedocs.org/docs/flask-pymongo/
request.method #request type
request.cookies.get('cookie_name') #cookies
@app.route('/hello') Flask-SQLAlchemy - http://pypi.python.org/pypi/Flask-SQLAlchemy
request.files['name'] #files
def hello():
return 'Hello, World!' #Redirect Flask-WTF - http://pythonhosted.org/Flask-WTF/

if __name__ == '__main__': from flask import url_for, redirect Flask-Mail - http://pythonhosted.org/Flask-Mail/


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

@app.route('/') #Session Handling


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

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

import jsonify #set session


@app.route('/login_success')
@app.route('/returnstuff') def login_success():
m
co
def returnstuff():
.
num_list = [1,2,3,4,5] session['key_name'] = 'key_value' #stores a secure cookie in browser
d
te
num_dict = {'numbers' : num_list, 'name' : 'Numbers'}
n
return redirect(url_for('index'))
i
#returns {'output' : {'numbers' : [1,2,3,4,5], 'name' : 'Numbers'}}
pr
ty
return jsonify({'output' : num_dict}) #read session
t
re
@app.route('/')

//p
def index():

:
if 'key_name' in session: #session exists and has key
tp
session_var = session['key_value']
else: #session does not exist ht

You might also like