cs1520 recitation: flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/cs1520... ·...
TRANSCRIPT
![Page 1: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/1.jpg)
CS1520 Recitation:
Flask 1Jeongmin Lee
![Page 2: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/2.jpg)
Plan for Today● Install Flask and Run First Program
● Routing
● Variable Rules
![Page 3: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/3.jpg)
Installing Flask
![Page 4: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/4.jpg)
Start with Virtual ENV1. Install Virtual Environment first
● pip install virtualenv
![Page 5: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/5.jpg)
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
![Page 6: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/6.jpg)
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)
![Page 7: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/7.jpg)
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
![Page 8: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/8.jpg)
Get Flask5. Install Flask
● pip install Flask
Note that this installation is
only on current environment.
Not your system’s Python.
![Page 9: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/9.jpg)
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
![Page 10: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/10.jpg)
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
![Page 11: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/11.jpg)
Routing
![Page 12: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/12.jpg)
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.
![Page 13: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/13.jpg)
Routing● Important job of Flask is handling user’s request
![Page 14: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/14.jpg)
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
![Page 15: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/15.jpg)
First Applicationfrom flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
![Page 16: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/16.jpg)
First Applicationfrom flask import Flaskapp = Flask(__name__)
@app.route('/')def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
![Page 17: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/17.jpg)
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.
![Page 18: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/18.jpg)
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()
![Page 19: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/19.jpg)
Variable Rules
![Page 20: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/20.jpg)
Variable Rules● Key idea: Build a URL dynamically.
![Page 21: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/21.jpg)
Variable Rules● Key idea: Build a URL dynamically.
● Use python’s variable to be the part of URL!
![Page 22: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/22.jpg)
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.
![Page 23: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/23.jpg)
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.
![Page 24: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/24.jpg)
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)
![Page 25: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/25.jpg)
Variable Rules● You can use other type of variable.
● Integer and Floating Point numbers.
● Path (string with slash ‘/’)
![Page 26: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/26.jpg)
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()
![Page 27: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/27.jpg)
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()
![Page 28: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming](https://reader033.vdocuments.us/reader033/viewer/2022060605/605ae3aba346db6cd9529395/html5/thumbnails/28.jpg)
Questions?