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 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 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)
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
Get Flask 5. 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 Flask app = 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 Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run() ● run >> python first.py
Routing
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.
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 Application from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
First Application from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
First Application from flask import Flask app = Flask(__name__) URL of ‘/’ i bound to the ● @app.route('/') hello_world() function. def hello_world(): Hence, user requested ‘/’ URL, return 'Hello World!' ● the hello_world() function will be run and its results will be if __name__ == '__main__': rendered in the browser. app.run()
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()
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 from flask import Flask app = Flask(__name__) as a keyword argument to the function with which @app.route('/hello/<name>') def hello_name(name): rule is associated. 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 Rules Integer and Floating Point from flask import Flask app = Flask(__name__) ● <int:postID> will cast @app.route('/blog/<int:postID>') def show_blog(postID): with coming string part of return 'Blog Number %d' % postID URL into type of integer. @app.route('/rev/<float:revNo>') def revision(revNo): ● Same for Float. return 'Revision Number %f' % revNo if __name__ == '__main__': app.run()
Variable Rules Path: from flask import Flask app = Flask(__name__) ● In computer world these @app.route('/flask') def hello_flask(): are different: return 'Hello Flask' ○ /python/ and /python @app.route('/python/') def hello_python(): return 'Hello Python' ● But Flask will treat them if __name__ == '__main__': app.run() same for trailing slash (/)
Questions?
Recommend
More recommend