Created
July 6, 2018 00:45
-
-
Save yevrah/fb185239c9bca376d4a7329e8dff5161 to your computer and use it in GitHub Desktop.
Flask: Annotated Boilerplate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A very simple Flask Hello World app for you to get started with... | |
from flask import Flask | |
app = Flask(__name__) | |
@app.route('/') | |
def hello_world(): | |
return 'Hello from Flask!' | |
# Redirect to other uri | |
@app.route('/r/<path:uri>') | |
def redirect(uri): | |
return "Hello from Redirect: {0} ".format(uri) | |
if __name__ == '__main__': | |
app.run(debug=True) | |
""" Cheet Sheet | |
____ _ | |
| __ ) __ _ ___(_) ___ ___ | |
| _ \ / _` / __| |/ __/ __| | |
| |_) | (_| \__ \ | (__\__ \ | |
|____/ \__,_|___/_|\___|___/ | |
# URL Routing | |
-------------------------------------- | |
@app.route(‘/foo/<name>/<int:age>’) | |
def view(name, age): | |
return ‘%s is %d years old’ % ( name, age ) | |
# Advanced Routing | |
-------------------------------------- | |
@app.route('/login', methods=['GET', 'POST']) | |
# Template Rendering | |
-------------------------------------- | |
from flask import render_template | |
return render_template( | |
‘foo.html’, | |
var1=value1, var2=value2, ... | |
) | |
_ _ _ | |
/ \ ___| |_(_) ___ _ __ ___ | |
/ _ \ / __| __| |/ _ \| '_ \/ __| | |
/ ___ \ (__| |_| | (_) | | | \__ \ | |
/_/ \_\___|\__|_|\___/|_| |_|___/ | |
# Retrieve query params | |
-------------------------------------- | |
request.args | |
request.args[‘name’ | |
# Retrieve form params (“POST data”) | |
-------------------------------------- | |
request.form | |
request.form[‘name’] | |
# Checking request type | |
-------------------------------------- | |
request.method | |
# Retrieve cookie value | |
-------------------------------------- | |
request.cookies | |
# Set Cookies | |
-------------------------------------- | |
response.set_cookie(key, value, ...) | |
# Sessions | |
-------------------------------------- | |
session[‘foo’] | |
# Settings | |
-------------------------------------- | |
app.config | |
app.config[‘my_name’ | |
# Build Urls | |
-------------------------------------- | |
url_for(‘blog’, id=45, slug=‘hi’) | |
# Redirect | |
-------------------------------------- | |
return redirect(‘/hello’) | |
_ _ | |
| | __ _ _ _ ___ _ _| |_ ___ | |
| | / _` | | | |/ _ \| | | | __/ __| | |
| |__| (_| | |_| | (_) | |_| | |_\__ \ | |
|_____\__,_|\__, |\___/ \__,_|\__|___/ | |
|___/ | |
# Basic App Layouts | |
-------------------------------------- | |
my_app.py | |
static/ | |
logo.png | |
base.css | |
templates/ | |
blog_post.html | |
index.html | |
# Package Layouts | |
-------------------------------------- | |
my_app/ | |
__init__.py | |
application.py | |
models.py | |
static/ | |
logo.png | |
base.css | |
templates/ | |
blog_post.html | |
index.html | |
-------------------------------------- | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment