-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactors.py
59 lines (52 loc) · 1.98 KB
/
actors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# using python 3
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Required
from data import ACTORS
app = Flask(__name__)
# Flask-WTF requires an enryption key - the string can be anything
app.config['SECRET_KEY'] = 'some?bamboozle#string-foobar'
# Flask-Bootstrap requires this line
Bootstrap(app)
# this turns file-serving to static, using Bootstrap files installed in env
# instead of using a CDN
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
# with Flask-WTF, each web form is represented by a class
# "NameForm" can change; "(FlaskForm)" cannot
# see the route for "/" and "index.html" to see how this is used
class NameForm(FlaskForm):
name = StringField('Which actor is your favorite?', validators=[Required()])
submit = SubmitField('Submit')
# define functions to be used by the routes (just one here)
# retrieve all the names from the dataset and put them into a list
def get_names(source):
names = []
for row in source:
name = row["name"]
names.append(name)
return sorted(names)
# all Flask routes below
# two decorators using the same function
@app.route('/', methods=['GET', 'POST'])
@app.route('/index.html', methods=['GET', 'POST'])
def index():
names = get_names(ACTORS)
# you must tell the variable 'form' what you named the class, above
# 'form' is the variable name used in this template: index.html
form = NameForm()
message = ""
if form.validate_on_submit():
name = form.name.data
if name in names:
message = "Yay! " + name + "!"
# empty the form field
form.name.data = ""
else:
message = "That actor is not in our database."
# notice that we don't need to pass name or names to the template
return render_template('index.html', form=form, message=message)
# keep this as is
if __name__ == '__main__':
app.run(debug=True)