Asd Flask Tutorial
Asd Flask Tutorial
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
Model-View-Controller
Most of the modern web development frameworks follow the Model-View-Controller model (MVC model)
The model : representation of data. Usually, have a
Its a rather convenient way to design software projects involving user interfaces presenting and manipulating data.
Model-View-Controller
Model-View-Controller
the model
The HTML pages, showing the various screen of the
the controllers
Model-View-Controller
the model
The HTML pages, showing the various screen of the
controllers
Model-View-Controller
javascript
Some work on the model database, software
architecture
Some work on the controls rather low-level and/or
specialized code
Some work on writing unit tests for at least the model
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
applications
Awfully designed programming language very inconsistent libraries very little help for debugging many security issues many better alternatives
But you can use those all this with a script-like language : Grails and Groovy
Flask
Flask
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
Hello, world !
Hello, world !
You will see this
Hello, world !
Hello, world !
requested
hello() returns the text data for the web browser
Debugging
In debug mode, you can edit the code while the server runs : it will restart !
Debugging
The debug mode will also helps a lot to point where the problem is
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
One function return text data. It can be HTM, XML, JSON, etc.
u r l f o r ( name ) )
Especially convenient when you might have to change the URL naming scheme
:(
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
Text template system is a convenient and common way to separade the view code from the remaining code
HTML.
XSLT is a template system based on XML. Plateform
hello.html is processed to generate the HTML to send to a user. Here, we use the name variable, passed as a parameter of render template
Flask assumes that all your templates will be in a template directory, relative to your script
| t e m p l a t e s | | | | h e l l o . h t m l | | t e s t . py
Using ressources
If you wish to use other le ressources, like pictures or CSS les, you can put them in directory named static
| t e m p l a t e s | | | | h e l l o . h t m l | | s t a t i c | | | | s t y l e . c s s | | t e s t . py
Those resource are not dynamic, not generated on the y like the HTML code, hence the name static
Using ressources
Then, to use those ressources, you can again use url for
< ! d o c t y p e h t m l> <h t m l> <head> < t i t l e>The w e b s i t e t h a t s a y s H e l l o t o you</ t i t l e> < l i n k r e l =s t y l e s h e e t t y p e=t e x t / c s s h r e f= {{ u r l f o r ( s t a t i c , f i l e n a m e = s t y l e . c s s ) }} > </ head> <body> {% i f name %} <h1>H e l l o , {{ name }} !</ h1> {% e l s e %} <h1>H e l l o , t h i n g w i t h no name !</ h1> {% e n d i f %} </ body> </ h t m l>
Template inheritance
On a typical website, dierent views follow a similar design
Template inheritance
On a typical website, dierent views follow a similar design
Template inheritance
On a typical website, dierent views follow a similar design
Template inheritance
On a typical website, dierent views follow a similar design
Template inheritance
Jinja2 provides a simple way to share a common template and specialize it : template inheritance
{% e x t e n d s b a s e . h t m l %} {% b l o c k c o n t e n t %} {% i f name %} <h2>H e l l o , {{ name }} !</ h2> {% e l s e %} <h2>H e l l o , t h i n g w i t h no name !</ h2> {% e n d i f %} {% e n d b l o c k %}
Template inheritance
Jinja2 provides a simple way to share a common template and specialize it : template inheritance
{% e x t e n d s b a s e . h t m l %} {% b l o c k c o n t e n t %} {% i f name %} <h2>Goodbye , {{ name }} !</ h2> {% e l s e %} <h2>Goodbye , t h i n g w i t h no name !</ h2> {% e n d i f %} {% e n d b l o c k %}
Template inheritance
And base.html look like this
< !DOCTYPE HTML PUBLIC //W3C//DTD HTML 4 . 0 1 / /EN> <h t m l l a n g= en > <head> < t i t l e> S a l u t e . com , t h e w e b s i t e t h a t s a l u t e s you</ t i t l e> < l i n k r e l =s t y l e s h e e t t y p e=t e x t / c s s h r e f= {{ u r l f o r ( s t a t i c , f i l e n a m e = s t y l e . c s s </ head> <body> < d i v i d= c o n t a i n e r > < d i v i d= h e a d e r > <h1> S a l u t e . com </ h1> <p>The w e b s i t e t h a t s a l u t e s you</p> </ d i v> < d i v i d= c o n t e n t > {% b l o c k c o n t e n t %}{% e n d b l o c k %} </ d i v> </ d i v> < d i v i d= f o o t e r > <h2> S a l u t e . com </ h2> <p> S i t e d e s i g n & ; c o p y r i g h t &c o p y ; A l e x a n d r e D e v e r t</p> </ d i v> </ body> </ h t m l>
Template inheritance
On the Python side, hello.html and goodbye.html are just normal HTML pages, nothing special to do
@app . r o u t e ( / h e l l o / ) @app . r o u t e ( / h e l l o /<name> ) d e f h e l l o ( name = None ) : r e t u r n r e n d e r t e m p l a t e ( h e l l o . h t m l , name = name ) @app . r o u t e ( / g o o d b y e / ) @app . r o u t e ( / g o o d b y e/<name> ) d e f g o o d b y e ( name = None ) : r e t u r n r e n d e r t e m p l a t e ( g o o d b y e . h t m l , name = name )
Template inheritance
Template macros
On a website, the same user interface elements are often re-used
Template macros
On a website, the same user interface elements are often re-used
Template macros
We can dene reusable HTML bits of codes.
{% macro r e n d e r p a n e l ( t i t l e , s t y l e = l e f t ) %} < d i v c l a s s= p a n e l > </ h1> <h1 c l a s s= {{ s t y l e }} >{{ t i t l e }} < d i v c l a s s= p a n e l c o n t e n t > < d i v c l a s s= {{ s t y l e }} > {{ c a l l e r ( ) }} </ d i v> </ d i v> </ d i v> {% endmacro %}
This dene a box, containing whatever caller() will put in it, and with a title. We put this in ui.html
Template macros
Now, we can create lots of boxes.
{% e x t e n d s b a s e . h t m l %} {% i m p o r t u i . h t m l a s u i %} {% b l o c k c o n t e n t %} < d i v c l a s s= t h r e e columnsl a y o u t > < d i v c l a s s= l e f t column > {% c a l l u i . r e n d e r p a n e l ( Lorem ipsum , l e f t ) %} . . . blabla . . . {% e n d c a l l %} {% c a l l u i . r e n d e r p a n e l ( Lorem ipsum , l e f t ) %} . . . blabla . . . {% e n d c a l l %} </ d i v> < d i v c l a s s= r i g h t column > {% c a l l u i . r e n d e r p a n e l ( H i s t o r y , l e f t ) %} . . . blabla . . . {% e n d c a l l %} {% c a l l u i . r e n d e r p a n e l ( Now i s t h e t i m e f o r a l l good men , l e f t ) %} . . . blabla . . . {% e n d c a l l %} </ d i v> </ d i v> {% e n d b l o c k %}
Template macros
To use a macro, rst import the le that contains that macro
{% i m p o r t u i . h t m l a s u i %}
What is between call and endcall could be any valid HTML code. It will be placed in place of caller in the macro denition.
Template language
Template language
generation.
Its a page that might be served for many dierent
Template language
Template language
Template language
Template language
The Jinja for loop works like the Python one
{% f o r i t e m i n n a v i g a t i o n %} < l i> <a h r e f= {{ i t e m . h r e f }} >{{ i t e m . c a p t i o n }} </ a> </ l i > {% e n d f o r %}
Note that
navigation is a sequence, passed to the template item is one item of the sequence loop code is between {% for %} and {% endfor %}
Template language
Jinja provides a loop object that can be called inside a for loop
{% f o r i t e m i n n a v i g a t i o n %} < l i> <a h r e f= {{ i t e m . h r e f }} >{{ l o o p . i n d e x }} {{ i t e m . c a p t i o n }} </ a> </ l i > {% e n d f o r %}
Template language
This loop object provides some useful informations about the current item of the loop loop variable loop.index loop.index0 loop.revindex loop.revindex0 loop.last loop.rst meaning Current index (1-indexed) Current index (0-indexed) Current index, reversed order (1-indexed) Current index, reversed order (0-indexed) True if last item True if rst item
Template language
Template language
If the sequence you iterate turns out to be empty, you can catch this case with an else block
{% f o r u s e r i n u s e r l i s t < l i> {{ u s e r . name }} </ l i > {% e l s e %} No u s e r s f o u n d ! {% e n d f o r %} i f n o t u s e r . i s h i d d e n %}
Table of Contents
1 2 3 4 5
Model-View-Controller Flask First steps Routing Templates Basic template rendering Using ressources Template inheritance Template macros Template language Requests
Requests
We can send data (HTML, JSON, XML, any kind of text), but we also need to receive data
passwords checkboxes values ...
Requests
Requests
We can also handle POST requests, like this
from f l a s k i m p o r t r e q u e s t @app . r o u t e ( / l o g i n , methods = [ GET , POST ] ) def login ( ) : # GET r e q u e s t i f r e q u e s t . method == GET : r e t u r n r e n d e r t e m p l a t e ( l o g i n . html ) # POST REQUEST else : email = r e q u e s t . form [ e m a i l ] p a s s w o r d = r e q u e s t . form [ p a s s w o r d ] # Check e m a i l & p a s s w o r d # TODO r e t u r n r e n d e r t e m p l a t e ( welcome . h t m l )
Requests