Flask Flashcards

(18 cards)

1
Q

import flask; create app; run app

A

from flask import Flask
app = Flask(__name__)
app.run(debug=True, host=”0.0.0.0”, port=5000)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Populate and format a jinja template w/data

A

from flask import render_template
params = {}
render_template(“template.html”, **params)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Get cookies

A

from flask import request
request.cookies.get(“cookie”, “default”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Get headers

A

from flask import request
request.headers.get(“cookie”, “default”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Variable route rule

A

@app.route(“/path/<type:name>
def fun(name):
pass</type:name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Variable route types

A

string, int, float, path, uuid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Manually escape html

A

from markupsafe import escape

def route():
escape(sus_content)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Construct/reverse a URL

A

from flask import url_for

@app.route(‘/user/<username>')
def profile(username):
return f'{username}\'s profile'</username>

with app.test_request_context():
print(url_for(‘profile’, username=’John Doe’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

TODO: Markup

A

from markupsafe import Markup

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Use session (cookie) state

A

from flask import session
app.secret_key = b’some_random_bytes’

def route():
session[“varname”] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Store data (database connection) on the app context

A

from flask import g

def get_db():
if ‘db’ not in g:
g.db = connect_to_database()

return g.db

@app.teardown_appcontext
def teardown_db(exception):
db = g.pop(‘db’, None)

if db is not None:
    db.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Return a json response from a route

A

Return a dict or list object from the route handler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to receive a file in a route handler

A

Set enctype=”multipart/form-data” on POSTing form

@app.route(‘/upload’, methods=[‘GET’, ‘POST’])
def upload_file():
if request.method == ‘POST’:
f = request.files[‘the_file’]
f.save(‘/var/www/uploads/uploaded_file.txt’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to receive a JSON payload

A

Client must set Content-Type: application/json
# Use POST/PUT/PATCH

@app.route(‘/api/data’, methods=[‘POST’])
def receive_json():
json_data = request.get_json()
#json_data = request.json #equivalent direct property access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to perform a redirect

A

from flask import redirect, url_for

@app.route(‘/’)
def index():
return redirect(url_for(‘login’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to return an error from a route

A

from flask import abort

@app.route(‘/login’)
def login():
abort(404)

Optional - to provide a custom error page
@app.errorhandler(404)
def page_not_found(error):
return render_template(‘page_not_found.html’), 404

17
Q

How to send messages to the built in logger

A

app.logger.debug(‘A value for debugging’)
app.logger.warning(‘A warning occurred (%d apples)’, 42)
app.logger.error(‘An error occurred’)