flask
play

Flask Marcin Jenczmyk Clearcode m.jenczmyk@clearcode.cc - PowerPoint PPT Presentation

requirements.txt Hello there! HTTP methods Templates Static files Flask Marcin Jenczmyk Clearcode m.jenczmyk@clearcode.cc 27/05/2017 Marcin Jenczmyk Flask requirements.txt Hello there! HTTP methods Templates Static files Overview


  1. requirements.txt Hello there! HTTP methods Templates Static files Flask Marcin Jenczmyk Clearcode m.jenczmyk@clearcode.cc 27/05/2017 Marcin Jenczmyk Flask

  2. requirements.txt Hello there! HTTP methods Templates Static files Overview requirements.txt 1 Hello there! 2 HTTP methods 3 Templates 4 Static files 5 Marcin Jenczmyk Flask

  3. requirements.txt Hello there! HTTP methods Templates Static files requirements.txt Requirements file is a plaintext file listing Python pip dependencies for a project. Marcin Jenczmyk Flask

  4. requirements.txt Hello there! HTTP methods Templates Static files requirements.txt Requirements file is a plaintext file listing Python pip dependencies for a project. To install dependencies from requirements.txt into current Python envrinoment run doctor@TARDIS: ∼ $ pip install -r requirements.txt Marcin Jenczmyk Flask

  5. requirements.txt Hello there! HTTP methods Templates Static files requirements.txt Requirements file is a plaintext file listing Python pip dependencies for a project. To install dependencies from requirements.txt into current Python envrinoment run doctor@TARDIS: ∼ $ pip install -r requirements.txt To save list of Python packages installled in current Python envrinoment into requirements.txt file run doctor@TARDIS: ∼ $ pip freeze > requirements.txt Marcin Jenczmyk Flask

  6. requirements.txt Hello there! HTTP methods Templates Static files requirements.txt appdirs==1.4.3 click==6.7 Flask==0.12.2 itsdangerous==0.24 Jinja2==2.9.6 MarkupSafe==1.0 packaging==16.8 pyparsing==2.2.0 six==1.10.0 Werkzeug==0.12.2 Figure: A sample requirements file. Marcin Jenczmyk Flask

  7. requirements.txt Hello there! HTTP methods Templates Static files Hello there! Flask is a microframework for Python for a web development. Marcin Jenczmyk Flask

  8. requirements.txt Hello there! HTTP methods Templates Static files Hello there! Flask is a microframework for Python for a web development. Some useful resources: http://flask.pocoo.org/ http://flask.pocoo.org/docs/latest/quickstart/ http://flask.pocoo.org/extensions/ https://blog.miguelgrinberg.com/post/ the-flask-mega-tutorial-part-i-hello-world Marcin Jenczmyk Flask

  9. requirements.txt Hello there! HTTP methods Templates Static files Hello there! Flask is a microframework for Python for a web development. Some useful resources: http://flask.pocoo.org/ http://flask.pocoo.org/docs/latest/quickstart/ http://flask.pocoo.org/extensions/ https://blog.miguelgrinberg.com/post/ the-flask-mega-tutorial-part-i-hello-world doctor@TARDIS: ∼ $ pip install Flask Marcin Jenczmyk Flask

  10. requirements.txt Hello there! HTTP methods Templates Static files Hello there! from flask import Flask app = Flask(__name__) @app.route("/") def hello (): return "Hello there!" if __name__ == "__main__": app.run() Figure: A simple Flask app (see hello v1/hello.py ). Marcin Jenczmyk Flask

  11. requirements.txt Hello there! HTTP methods Templates Static files Hello there! To run a Flask application run doctor@TARDIS: ∼ $ python hello.py Marcin Jenczmyk Flask

  12. requirements.txt Hello there! HTTP methods Templates Static files Hello there! To run a Flask application run doctor@TARDIS: ∼ $ python hello.py Debug mode It’s easier to debug application behaviour in debug mode - to do this add app.debug = True in your Python code or export FLASK DEBUG envrinoment variable doctor@TARDIS: ∼ $ export FLASK DEBUG=1 Marcin Jenczmyk Flask

  13. requirements.txt Hello there! HTTP methods Templates Static files Hello there! To run a Flask application run doctor@TARDIS: ∼ $ python hello.py Debug mode It’s easier to debug application behaviour in debug mode - to do this add app.debug = True in your Python code or export FLASK DEBUG envrinoment variable doctor@TARDIS: ∼ $ export FLASK DEBUG=1 Warning Debug mode should be never used on a production! Marcin Jenczmyk Flask

  14. requirements.txt Hello there! HTTP methods Templates Static files HTTP methods To use a GET HTTP method put a < type:arg> in a view URL, where type can be either string , int , float , path , any (any of listed before) or uuid . @app.route(’/’) @app.route(’/hello/<string:name >/’) def hello(name=None): if name is None: return ’Hello there!’ elif name == ’there ’: return ’General Kenobi!’ else: return ’Hello {}!’.format(name) Figure: Sample view with GET parameter (see hello v2/hello.py ). Marcin Jenczmyk Flask

  15. requirements.txt Hello there! HTTP methods Templates Static files HTTP methods To use POST HTTP method one has to enable it in a view decorator. @app.route(’/login/’, methods=[’GET’, ’POST ’]) def login (): if request.method == ’POST ’: # Do some stuff to log a user using POST data. pass else: # Render login form allowing to log in. pass return ’Login ’ Figure: Sample view handling POST parameter (see hello v2/hello.py ). Marcin Jenczmyk Flask

  16. requirements.txt Hello there! HTTP methods Templates Static files HTTP methods Remark One can get URL to view by its name using url_for function, ex. one can get login view URL by calling url_for(’login’) . Marcin Jenczmyk Flask

  17. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 Rendering entire HTML markup for a webpage by writing strings to be returned by views would be tiresome - there is a Jinja2 engine built in Flask to enable rendering HTML templates. Marcin Jenczmyk Flask

  18. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 Rendering entire HTML markup for a webpage by writing strings to be returned by views would be tiresome - there is a Jinja2 engine built in Flask to enable rendering HTML templates. One can render template using render_template function, Flask will be looking for templates in the templates directory, located at the same path as Python application file! / hello.py templates Marcin Jenczmyk Flask

  19. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 @app.route(’/hello/<string:name >’) def hello(name=None): from flask import render_template if name == ’there ’: greetings = ’General Kenobi!’ else: greetings = ’Hello {}!’.format(name or ’there ’ ) return render_template ( ’index.html ’, greetings=greetings ) Figure: Sample view rendering Jinja template (see hello v3/hello.py ). Marcin Jenczmyk Flask

  20. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 < ! DOCTYPE html > < html > < head > < meta charset =”UTF − 8” > < t i t l e > T i t l e of the document < / t i t l e > < / head > < body > < h1 > {{ g r e e t i n g s }} < / h1 > < / body > < / html > Figure: Jinja template for hello 3 example (see hello v3/templates/index.html ). Marcin Jenczmyk Flask

  21. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 < ! DOCTYPE html > < html > < head > < meta charset =”UTF − 8” > < t i t l e > T i t l e of the document < / t i t l e > { % block css % }{ % endblock % } < / head > < body > { % block body % }{ % endblock % } < / body > < / html > Figure: Jinja templates can be extended (see index.html and base.html - on next slide - in hello v4/templates/ ). Marcin Jenczmyk Flask

  22. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 { % extends ” base . html” % } { % block body % } < h1 > {{ g r e e t i n g s }} < / h1 > { % endblock % } Marcin Jenczmyk Flask

  23. requirements.txt Hello there! HTTP methods Templates Static files Jinja2 id =” n a v i g a t i o n ” > < ul { % f o r item in n a v i g a t i o n % } < l i > < a href =” {{ item . h r e f }} ” > {{ item . caption }} < / a > < / l i > { % endfor % } < / ul > { % i f u r l % } < a href =” {{ u r l }} ” > Mysterious URL < / a > { % e n d i f % } Figure: Jinja templates allow for using for loops and if commands. Marcin Jenczmyk Flask

  24. requirements.txt Hello there! HTTP methods Templates Static files Static files Dynamic web applications also need static files. They are going to be searched for in static directory (but on production envrinoment server should handle them); to generate URLs for static files, use the special static endpoint name. Marcin Jenczmyk Flask

  25. requirements.txt Hello there! HTTP methods Templates Static files Static files Dynamic web applications also need static files. They are going to be searched for in static directory (but on production envrinoment server should handle them); to generate URLs for static files, use the special static endpoint name. To generate URL for a static file use url_for function, ex. url_for(’static’, filename=’style.css’) . / hello.py templates static Marcin Jenczmyk Flask

  26. requirements.txt Hello there! HTTP methods Templates Static files Static files from flask import Flask , render_template app = Flask(__name__) @app.route(’/’) def hello (): return render_template (’index.html ’) if __name__ == "__main__": app.run() Figure: Python code for hello v5 example app. Marcin Jenczmyk Flask

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend