flask Flashcards

1
Q

Flask: To import the flask class, type

A

from flask import Flask

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

Flask: To create an instance of the Flask class, type

A

app = Flask(__name__)

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

Flask: To make an app run but automatically use changes to the server code, type

A

app.run(debug=True)

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

Flask: To create the route for an app, type

A
@app.route("/")
def index():
  return "This is my route"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Flask: To import the request library, type

A

form flask import request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
@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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A

Yes e.g.

@app.route(“/route_one”)
@app.route(“/route_two”)

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

from flask import Flask

app = Flask(__name__)

@app.route("//")
def my_function(name, age):
    return "Im {}, {}".format(name, age)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Flask: Values pulled from the url are automatically the type

A

string

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

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Flask: To run a flask app on the local host, type

A

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.

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

Flask: Triple quotes allow you to

A

Use line breaks by pressing enter

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

Flask: Flask automatically looks for a directory in the same directory as the app called

A

Templates. And it will automatically serve whats in it when you reference templates in your view.

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

Flask: To import templates in the app, type

A

from flask import render_template

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

Flask: To make a view return a template and pass it the variables, type

A
@app.route("/")
def my_function():
    return render_template("name.html", var_1=name, var_2=age)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Flask: When making views, make sure to always

A

pass in all of the variables you declare in the view.

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

Flask: To make the html template pull variables from the view, type

A

{{ var_name }}

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

Flask: html templates can be altered in just a small portion, by changing a

A

block

19
Q

Flask: To imbed blocks inside the html templates, type

A

{% block block_name %}{% endblock %}

20
Q

Flask: To create an extends html block, type

A

{% extends “layout.html” %}

21
Q

Flask: The extends block

A

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).

22
Q

Flask: To use the contents of a block from the html file you are extending, type

A

{{ super() }}

23
Q

Flask: Static files should be saved in

A

A directory called “static” in the same directory as the app.

24
Q

Flask: An example of a static file that goes in the static directory is

A

styles.css, javascript, images

25
Q

Flask: using __name__, passes in

A

the current namespace.

26
Q

Flask: The request object holds an attribute called

A

request.args

27
Q

Flask: args holds all of the

A

parameters in the request (url) in a dictionary

28
Q

Flask: To retrieve an object from the url request and set it to a variable, type

A

my_var = request.args.get(“parameter value”, “default”)

29
Q

Flask: to accept a url request with and take the value into the view function, type

A
@app.route("/-parameter_value>")
def index(paramer_value="yo"):
    return "hello {}".format(parameter_value)
30
Q

flask: To turn values coming in from a request into integers, make the route

A

app.route(“/-int:assigned_variable>”)

31
Q

flask: flask view functions (in order to work properly) can only return a

A

string

32
Q

flask: flask views can have multiple

A

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>”

33
Q

Flask: for a basic flask app with two views, multiple routes, and character conversion, type

A

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)

34
Q

flask: To send a dataframe to an layout in html format, type

A
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 %}

35
Q

python: To find the files you just installed through pip

A

lib/python2.7/site-packages/module_name.egg/

36
Q

python: It seems that if you have multiple files inside one module you can just

A

import a class straight from the module and do not have to go into the specific file in the module

37
Q

flask: To use minimongo through flask, type

A

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!’

38
Q

flask: To convert a request object into json format, type

A

request.get_json()

39
Q

flask: To get a field from a post request, type

A

request.form.get(“field_name”)

40
Q

flask: To turn an array of dicts into a json response, tyep

A

from flask import jsonify


return jsonify(data = [{“key”:”value”}, {“key2”:”value2”}])
note:key-name (e.g. data) is required

41
Q

flask: To make static files serve from flask paths

A

from flask import Flask, request, send_from_directory

@app.route('/')
def send_static(path):
    return send_from_directory('static', path)
42
Q

flask: To allow post and get requests, type

A

@app.route(‘/’, methods=[‘GET’, ‘POST’])
def view_function():
if request.method == ‘POST’:

43
Q

flask: To get a file from a post request, type

A

request.files.get(“file_name”)