CS1520 Recitation: Flask 1 Jeongmin Lee Plan for Today Install - - PowerPoint PPT Presentation

cs1520 recitation flask 1
SMART_READER_LITE
LIVE PREVIEW

CS1520 Recitation: Flask 1 Jeongmin Lee Plan for Today Install - - PowerPoint PPT Presentation

CS1520 Recitation: Flask 1 Jeongmin Lee Plan for Today Install Flask and Run First Program Routing Variable Rules Installing Flask Start with Virtual ENV 1. Install Virtual Environment first pip install virtualenv Start with


slide-1
SLIDE 1

CS1520 Recitation: Flask 1

Jeongmin Lee

slide-2
SLIDE 2

Plan for Today

  • Install Flask and Run First Program
  • Routing
  • Variable Rules
slide-3
SLIDE 3

Installing Flask

slide-4
SLIDE 4

Start with Virtual ENV

  • 1. Install Virtual Environment first
  • pip install virtualenv
slide-5
SLIDE 5

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
slide-6
SLIDE 6

Start with Virtual ENV

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

slide-7
SLIDE 7

Start with Virtual ENV

  • 3. Get into the folder and activate environment
  • cd/cs1520_flask
  • source bin/activate
  • 4. Now, we are in the new environment
slide-8
SLIDE 8

Get Flask

  • 5. Install Flask
  • pip install Flask

Note that this installation is

  • nly on current environment.

Not your system’s Python.

slide-9
SLIDE 9

Run First Application

  • save it as first.py

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()

  • run >> python first.py
slide-10
SLIDE 10

Run First Application

  • save it as first.py

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()

  • run >> python first.py
slide-11
SLIDE 11

Routing

slide-12
SLIDE 12

Routing

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

slide-13
SLIDE 13

Routing

  • Important job of Flask is handling user’s request
slide-14
SLIDE 14

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

slide-15
SLIDE 15

First Application

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()

slide-16
SLIDE 16

First Application

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()

slide-17
SLIDE 17

First Application

from flask import Flask app = 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.

slide-18
SLIDE 18

Let’s have more routes!

from flask import Flask import datetime app = 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()

slide-19
SLIDE 19

Variable Rules

slide-20
SLIDE 20

Variable Rules

  • Key idea: Build a URL dynamically.
slide-21
SLIDE 21

Variable Rules

  • Key idea: Build a URL dynamically.
  • Use python’s variable to be the part of URL!
slide-22
SLIDE 22

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.

slide-23
SLIDE 23

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.

slide-24
SLIDE 24

Variable Rules

  • Variable Rules are passed

as a keyword argument to the function with which rule is associated.

from flask import Flask app = Flask(__name__) @app.route('/hello/<name>') def hello_name(name): return 'Hello %s!' % name if __name__ == '__main__': app.run(debug = True)

slide-25
SLIDE 25

Variable Rules

  • You can use other type of variable.
  • Integer and Floating Point numbers.
  • Path (string with slash ‘/’)
slide-26
SLIDE 26

Variable Rules

Integer and Floating Point

  • <int:postID> will cast

with coming string part of URL into type of integer.

  • Same for Float.

from flask import Flask app = 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()

slide-27
SLIDE 27

Variable Rules

Path:

  • In computer world these

are different: ○ /python/ and /python

  • But Flask will treat them

same for trailing slash (/)

from flask import Flask app = 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()

slide-28
SLIDE 28

Questions?