cs1520 recitation flask 1
play

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


  1. CS1520 Recitation: Flask 1 Jeongmin Lee

  2. Plan for Today ● Install Flask and Run First Program ● Routing ● Variable Rules

  3. Installing Flask

  4. Start with Virtual ENV 1. Install Virtual Environment first ● pip install virtualenv

  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

  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)

  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

  8. Get Flask 5. Install Flask ● pip install Flask Note that this installation is only on current environment. Not your system’s Python.

  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

  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

  11. Routing

  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.

  13. Routing ● Important job of Flask is handling user’s request

  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

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

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

  17. 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()

  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()

  19. Variable Rules

  20. Variable Rules ● Key idea: Build a URL dynamically.

  21. Variable Rules ● Key idea: Build a URL dynamically. ● Use python’s variable to be the part of URL!

  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.

  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.

  24. 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)

  25. Variable Rules ● You can use other type of variable. ● Integer and Floating Point numbers. ● Path (string with slash ‘/’)

  26. 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()

  27. 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 (/)

  28. Questions?

Recommend


More recommend