flask Flashcards
Flask: To import the flask class, type
from flask import Flask
Flask: To create an instance of the Flask class, type
app = Flask(__name__)
Flask: To make an app run but automatically use changes to the server code, type
app.run(debug=True)
Flask: To create the route for an app, type
@app.route("/") def index(): return "This is my route"
Flask: To import the request library, type
form flask import request
@app.route("/") def index(name_var="Default Name"): name = request.args.get("name", name_var) return name note: .get returns either the value on query string and if none present, returns whatever is after the comma.
Yes e.g.
@app.route(“/route_one”)
@app.route(“/route_two”)
from flask import Flask
app = Flask(__name__)
@app.route("//") def my_function(name, age): return "Im {}, {}".format(name, age)
Flask: Values pulled from the url are automatically the type
string
name the parameter in the function the same as what you are naming the url slash index, e.g.
@app.route("/") def my_function(name): return "Im {}".format(age)
Flask: To run a flask app on the local host, type
from flask import Flask
app = Flask(__name__)
@app.route('/') def hello_world(): return 'Hello World!'
if __name__ == ‘__main__’:
app.run()
Then save it and run it in python from the command line.
Flask: Triple quotes allow you to
Use line breaks by pressing enter
Flask: Flask automatically looks for a directory in the same directory as the app called
Templates. And it will automatically serve whats in it when you reference templates in your view.
Flask: To import templates in the app, type
from flask import render_template
Flask: To make a view return a template and pass it the variables, type
@app.route("/") def my_function(): return render_template("name.html", var_1=name, var_2=age)
Flask: When making views, make sure to always
pass in all of the variables you declare in the view.
Flask: To make the html template pull variables from the view, type
{{ var_name }}
Flask: html templates can be altered in just a small portion, by changing a
block
Flask: To imbed blocks inside the html templates, type
{% block block_name %}{% endblock %}
Flask: To create an extends html block, type
{% extends “layout.html” %}
Flask: The extends block
Imports all of the html of the file you link to except what is contained in the blocks which you need to replace with your own block contents (using the same block names).
Flask: To use the contents of a block from the html file you are extending, type
{{ super() }}
Flask: Static files should be saved in
A directory called “static” in the same directory as the app.
Flask: An example of a static file that goes in the static directory is
styles.css, javascript, images
Flask: using __name__, passes in
the current namespace.
Flask: The request object holds an attribute called
request.args
Flask: args holds all of the
parameters in the request (url) in a dictionary
Flask: To retrieve an object from the url request and set it to a variable, type
my_var = request.args.get(“parameter value”, “default”)
Flask: to accept a url request with and take the value into the view function, type
@app.route("/-parameter_value>") def index(paramer_value="yo"): return "hello {}".format(parameter_value)
flask: To turn values coming in from a request into integers, make the route
app.route(“/-int:assigned_variable>”)
flask: flask view functions (in order to work properly) can only return a
string
flask: flask views can have multiple
routes which can accept request values in all of the arrangements they may come in.
app. route(“/-int:num1>/-float:num2>”
app. route(“/-float:num1>/-int:num2>”
Flask: for a basic flask app with two views, multiple routes, and character conversion, type
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route("/") def index(parameter_value="default"): parameter_value = request.args.get("sent in value", parameter_value) return "hello {}".format(parameter_value)
@app.route(“/add//”)
@app.route(“/add//”)
def add(num1, num2):
return “sum {}”.format(num1+num2)
app.run(debug=True)
flask: To send a dataframe to an layout in html format, type
app.route("/analysis") def analysis(): df = pandas.DataFrame() return render_template("analysis.html", data=df.to_html())
{% extends “layout.html” %}
{% block body %}
{{ data | safe }}
{% endblock %}
python: To find the files you just installed through pip
lib/python2.7/site-packages/module_name.egg/
python: It seems that if you have multiple files inside one module you can just
import a class straight from the module and do not have to go into the specific file in the module
flask: To use minimongo through flask, type
from flask import Flask, render_template, request
from minimongo import Model, configure
app = Flask(__name__)
configure(host=”ds029824.mongolab.com”, port=29824, username=”al”, password=”al”)
class collectionDocument(Model): class Meta: database = "databaseName" collection = "collectionName"
@app.route(‘/’)
def hello_world():
instance = collectionDocument()
instance.hello = request.args.get(“key”)
instance.save()
instance = collectionDocument({“x”: 1, “y”: 2}).save()
collectionDocument.collection.find_one()
instance = collectionDocument.collection.find_one({“x”: 1})
return ‘String!’
flask: To convert a request object into json format, type
request.get_json()
flask: To get a field from a post request, type
request.form.get(“field_name”)
flask: To turn an array of dicts into a json response, tyep
from flask import jsonify
…
return jsonify(data = [{“key”:”value”}, {“key2”:”value2”}])
note:key-name (e.g. data) is required
flask: To make static files serve from flask paths
from flask import Flask, request, send_from_directory
@app.route('/') def send_static(path): return send_from_directory('static', path)
flask: To allow post and get requests, type
@app.route(‘/’, methods=[‘GET’, ‘POST’])
def view_function():
if request.method == ‘POST’:
…
flask: To get a file from a post request, type
request.files.get(“file_name”)