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

flask
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

requirements.txt Hello there! HTTP methods Templates Static files

Flask

Marcin Jenczmyk

Clearcode m.jenczmyk@clearcode.cc

27/05/2017

Marcin Jenczmyk Flask

slide-2
SLIDE 2

requirements.txt Hello there! HTTP methods Templates Static files

Overview

1

requirements.txt

2

Hello there!

3

HTTP methods

4

Templates

5

Static files

Marcin Jenczmyk Flask

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

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

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

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

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

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

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

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

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

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

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

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

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

slide-16
SLIDE 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.

  • ne can get login view URL by calling url_for(’login’).

Marcin Jenczmyk Flask

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

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

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

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

  • f

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

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

  • f

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

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

slide-23
SLIDE 23

requirements.txt Hello there! HTTP methods Templates Static files

Jinja2

<ul id=” n a v i g a t i o n ”> {% 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

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

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

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

slide-27
SLIDE 27

requirements.txt Hello there! HTTP methods Templates Static files

Static files

{% extends ” base . html” %} {% block css %} <l i n k r e l=” s t y l e s h e e t ” type=” t e x t / css ” href=”{{ u r l f o r ( ’ s t a t i c ’ , filename =’ s t y l e . css ’ ) }}”> {% endblock %}

Figure: Jinja template rendering static content (see hello v5/templates/index.html, continuation on next slide).

Marcin Jenczmyk Flask

slide-28
SLIDE 28

requirements.txt Hello there! HTTP methods Templates Static files

Static files

{% block body %} <img class=”meme” src=”{{ u r l f o r ( ’ s t a t i c ’ , filename =’ h e l l o . jpg ’ ) }}”> {% endblock %}

Figure: Jinja template rendering static content, continuation (see hello v5/templates/index.html).

Marcin Jenczmyk Flask

slide-29
SLIDE 29

requirements.txt Hello there! HTTP methods Templates Static files

References

Armin Ronacher (2017) http://flask.pocoo.org/ Armin Ronacher (2008) http://jinja.pocoo.org/docs/2.9/

Marcin Jenczmyk Flask