Top Banner
CS1520 Recitation: Flask 1 Jeongmin Lee
28

CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point will cast with coming

Oct 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

Installing Flask

Page 4: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

Routing

Page 12: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

Variable Rules

Page 20: CS1520 Recitation: Flask 1people.cs.pitt.edu/~jlee/teaching/cs1520_fall2017/slides/CS1520... · Variable Rules Integer and Floating Point <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

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 <int:postID> will cast with coming

Questions?