CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... ·...
Transcript of CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... ·...
CS1520 Recitation:
Flask 1Jeongmin Lee
Plan for Today● Install Flask and Run First Program
● Routing
● Variable Rules
Installing Flask
Start with Virtual ENV1. Install Virtual Environment first
● pip install virtualenv
Start with Virtual ENV** For windows
1-1. Download get-pip.py file and run it
● https://bootstrap.pypa.io/get-pip.py
1-2. Setup pip and then virtual env
● pip install --upgrade pip setuptools
● pip install virtualenv
Start with Virtual ENV2. Create new environment with Python 3
● python -m virtualenv cs1520_flask -p
/Library/Frameworks/Python.framework/Versions/3.4/bin/pyt
ho
(example for my setting)
Start with Virtual ENV3. Get into the folder and activate environment
● cd/cs1520_flask
● source bin/activate
4. Now, we are in the new environment
Get Flask5. Install Flask
● pip install Flask
Note that this installation is
only on current environment.
Not your system’s Python.
Run First Application● save it as first.py
from flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
● run >> python first.py
Run First Application● save it as first.py
from flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
● run >> python first.py
Routing
RoutingRouting is about how URL and resources are linked!
● URL : Uniform Resource Locator
e.g., http://cs.pitt.edu/index.html
● Resource: can be anything. image, file, html,
even a piece of function in Python.
Routing● Important job of Flask is handling user’s request
Routing● Important job of Flask is handling user’s request
● Request is coming through URL
● Routing is a mechanism that handles URL to specific
function in your code
First Applicationfrom flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
First Applicationfrom flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
First Applicationfrom flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
● URL of ‘/’ i bound to the
hello_world() function.
● Hence, user requested ‘/’ URL,
the hello_world() function will
be run and its results will be
rendered in the browser.
Let’s have more routes!from flask import Flaskimport datetimeapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
@app.route('/todayis')def get_today_date(): return str(datetime.date.today())
if __name__ == '__main__': app.run()
Variable Rules
Variable Rules● Key idea: Build a URL dynamically.
Variable Rules● Key idea: Build a URL dynamically.
● Use python’s variable to be the part of URL!
Variable Rules● Key idea: Build a URL dynamically.
● Use python’s variable to be the part of URL!
● Variable Rules are passed as a keyword argument to the
function with which rule is associated.
Variable Rules● Key idea: Build a URL dynamically.
● Use python’s variable to be the part of URL!
● Variable Rules are passed as a keyword argument to the
function with which rule is associated.
Variable Rules● Variable Rules are passed
as a keyword argument to
the function with which
rule is associated.
from flask import Flaskapp = Flask(__name__)
@app.route('/hello/<name>')def hello_name(name): return 'Hello %s!' % name
if __name__ == '__main__': app.run(debug = True)
Variable Rules● You can use other type of variable.
● Integer and Floating Point numbers.
● Path (string with slash ‘/’)
Variable RulesInteger and Floating Point
● <int:postID> will cast
with coming string part of
URL into type of integer.
● Same for Float.
from flask import Flaskapp = Flask(__name__)
@app.route('/blog/<int:postID>')def show_blog(postID): return 'Blog Number %d' % postID
@app.route('/rev/<float:revNo>')def revision(revNo): return 'Revision Number %f' % revNo
if __name__ == '__main__': app.run()
Variable RulesPath:
● In computer world these
are different:
○ /python/ and /python
● But Flask will treat them
same for trailing slash (/)
from flask import Flaskapp = Flask(__name__)
@app.route('/flask')def hello_flask(): return 'Hello Flask'
@app.route('/python/')def hello_python(): return 'Hello Python'
if __name__ == '__main__': app.run()
Questions?