Difference between revisions of "Python-Flask/C2/Introduction-to-Flask-Templates/English"
(First draft) |
(First edit to do some wiki formatting) |
||
Line 208: | Line 208: | ||
|- style="border:1pt solid #000000;padding:0.176cm;" | |- style="border:1pt solid #000000;padding:0.176cm;" | ||
|| | || | ||
− | [Editor: sample_template.html] | + | [Editor: sample_template.html] <br> |
Type | Type | ||
− | + | ||
<!DOCTYPE html> | <!DOCTYPE html> | ||
Line 221: | Line 221: | ||
</head> | </head> | ||
− | <body> | + | <body><br> |
+ | <nowiki> | ||
<h1>The firstname value is Spoken</h1> | <h1>The firstname value is Spoken</h1> | ||
+ | </nowiki><br> | ||
</body> | </body> | ||
+ | |||
</html> | </html> | ||
− | + | ||
+ | |||
|| <span style="background-color:transparent;color:#000000;">Let us write a simple </span><span style="background-color:transparent;color:#000000;">'''HTML'''</span><span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">code to print the first name in a new file </span><span style="background-color:transparent;color:#000000;">'''sample_template.html '''</span> | || <span style="background-color:transparent;color:#000000;">Let us write a simple </span><span style="background-color:transparent;color:#000000;">'''HTML'''</span><span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">code to print the first name in a new file </span><span style="background-color:transparent;color:#000000;">'''sample_template.html '''</span> | ||
Line 373: | Line 377: | ||
− | < | + | <nowiki><h1>The firstname value is {{ firstname }}</h1></nowiki> |
Line 458: | Line 462: | ||
|- style="border:1pt solid #000000;padding:0.176cm;" | |- style="border:1pt solid #000000;padding:0.176cm;" | ||
− | || <div style="color:#000000;">[Text Editor] Show | + | || <div style="color:#000000;">[Text Editor] Show form1.html</div> |
+ | <!DOCTYPE html> | ||
− | < | + | <html> |
+ | <head> | ||
+ | <br> | ||
+ | <nowiki> <title> Hello </title> </nowiki> | ||
+ | <br> | ||
+ | </head> | ||
+ | <body> | ||
+ | |||
+ | <nowiki> <h1>The firstname value is: {{ fname }}</h1> </nowiki> | ||
+ | <br> | ||
+ | <nowiki> <h1>The lastname value is: {{ lname }}</h1> </nowiki> | ||
+ | |||
+ | </body> | ||
+ | |||
+ | </html> | ||
|| <span style="background-color:transparent;color:#000000;">In </span><span style="background-color:transparent;color:#000000;">'''form1.html,'''</span><span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">add the following piece of code.</span> | || <span style="background-color:transparent;color:#000000;">In </span><span style="background-color:transparent;color:#000000;">'''form1.html,'''</span><span style="background-color:transparent;color:#000000;"> </span><span style="background-color:transparent;color:#000000;">add the following piece of code.</span> |
Revision as of 02:06, 12 August 2019
Title of script: Introduction to Flask Templates
Author: Siddhartha Sarkar
Keywords: Video tutorial, Python Flask, flask templates, render_ template, jinja2, variables in templates.
Visual Cue | Narration |
Slide 1: Introduction to Flask templates | Welcome to the Spoken Tutorial on Introduction to Flask templates. |
Slide 2: Learning Objectives | In this tutorial, we will learn to* Use templates in Flask.
|
Slides 3: System Requirements
|
To record this tutorial, I’m using * Ubuntu Linux 16.04 OS
However, you may use any other editor or browser of your choice. |
Slide 5:
Pre-requisites
|
To follow this tutorial, you should have working knowledge of * Linux commands
If not, then please go through the corresponding tutorials on this website.
|
Slide 6: MVC Architecture | Let us begin with the concept of MVC architecture.
|
Slide 7: MVC Architecture
(Model)
|
* Model is used to represent the application data.
|
Slide 8: MVC Architecture
(View)
|
* View is the presentation layer of MVC.
|
Slide 9: MVC Architecture
(Controller)
|
* Controller is responsible for directing the execution flow of the app.
|
Slide 11: MVC Architecture
|
* The templates are part of the View part of MVC.
|
Slide 6: Templates
|
* So far we have simply returned HTML code from the view functions.
|
Slide 7: Templates
|
* One HTML file can inherit HTML code from another file with the help of templates.
|
Slide 8: Templates
|
* So one can write a base HTML file template.
|
Open a Terminal window
|
Let us open the Terminal by pressing Ctrl, Alt and T keys simultaneously on the keyboard. |
[Terminal]
Type cd project_flask & Press Enter
|
Now go to the folder project underscore flask which we created earlier using cd command. |
[Terminal]
$ . flask_venv/bin/activate
[Enter]
|
And activate our virtualenv.
Type
dot space flask_venv slash bin slash activate
And press Enter. |
Only narration | Here onwards, please remember to press the Enter key after typing each command. |
[Terminal]
Type
$ atom hello_flask.py
|
Let’s open the hello_flask.py file which we created earlier in this series, in any text editor. |
[Text Editor] hello_flask.py
Highlight HTML part in my_form().
|
Observe that the my_form() function returns raw HTML.
|
[Terminal]
mkdir templates [Enter]
|
Switch to the terminal.
|
[Editor: sample_template.html] <!DOCTYPE html> <html> <head> <title>Hello</title> </head> <body> </html>
|
Let us write a simple HTML code to print the first name in a new file sample_template.html
Type the code as shown here.
|
Press Ctrl + S | And save the file. |
Only Narration | Now let us render this HTML file via python flask app.
|
Switch to hello_flask.py in the text editor. | Switch to the file hello_flask.py in the text editor. |
[Editor: hello_flask.py]
@app.route('/template')
def template_ex():
return render_template('sample_template.html') |
In the hello_flask.py file, before the last if condition, type the code as shown here.
|
Highlight: render_template('sample_template.html') | Inside this route we have used the function render_template provided by Flask.
|
[Text Editor] Add , render_template
from flask import Flask, request, render_template
|
In the 1st line, we will import the method render_template. |
Press Ctrl + S keys | And then save the file |
[Terminal] | Now switch to the terminal. |
[Terminal]
Type
$ export FLASK_APP=hello_flask.py
|
And type export <space> FLASK_APP <equal to > hello underscore flask dot py |
[Terminal]
Type $ python3 -m flask run [Enter]
|
Now run the server. |
[Firefox]
And press Enter.
|
Then, open a web browser and in the address bar, type
http://127.0.0.1:5000/template
|
[Firefox] Highlight the text. | We got the output of the HTML page.
|
Only narration | Now switch to the hello_flask.py file
Let us see how we can do this.
|
def template_ex():
fname = 'Tutorial'
return render_template('sample_template.html', firstname = fname)
|
Update the template_ex method as shown here. |
[Text Editor] highlight fname = ‘Tutorial’ | I have declared a variable called fname here and assigned the value Tutorial to it. |
[Text Editor] Highlight
|
Then I have passed this variable fname to the render_template method.
|
Press Ctrl + S keys | Let’s save the file. |
[Text Editor]
In sample_template.html
Highlight Spoken
|
And switch to sample_template.html file.
|
Slide 9: Jinja | * Jinja is a powerful templating language.
|
Modify the body as
|
Switch to sample_template.html file.
|
Press Ctrl + S | Save the file. |
[Terminal]
Type
$ python3 -m flask run
[Enter]
|
And switch to the terminal.
|
Type
http://127.0.0.1:5000/template And press Enter.
|
Then switch to the browser and refresh the page.
|
[Text Editor] hello_flask.py
def my_form():
if request.method == 'POST':
firstname = request.form.get('firstname')
lastname = request.form.get('lastname')
return render_template('form2.html')
|
Once again, switch to the hello_flask.py file and update the form route as shown.
|
Press Ctrl + S | Save this file. |
Only narration | Now we have to create two HTML files form1.html and form2.html. |
Terminal
Press Ctrl + C
|
Switch to the terminal and stop the server. |
[Terminal]
atom templates/form1.html templates/form2.html [Enter]
|
Now open form1.html and form2.html in a text editor.
|
[Text Editor] Show form1.html
<!DOCTYPE html> <html> <head>
<body> <h1>The firstname value is: {{ fname }}</h1>
</body> </html> |
In form1.html, add the following piece of code.
|
Press Ctrl + S | Save the file. |
[Text Editor] Show form2.html
|
In form2.html, add the following piece of code.
|
Press Ctrl + S | Again, save the file. |
[Terminal]
Type python3 -m flask run
|
Switch to the terminal.
|
[Browser]
Type
And press Enter.
|
Then switch to the browser.
|
[Browser]
Firstname : Spoken
Lastname: Tutorial [Submit]
|
Enter the first name as Spoken.
|
[Browser] Highlight the text on screen. | We got the output as desired. |
[Terminal]
|
Switch back to the terminal.
|
Only narration | This brings us to an end of this tutorial.
Let us summarise.
|
Slide 8: Summary | In this tutorial, we learnt about-
Templating in flask to write clean code.
|
Slide: About Spoken Tutorial project
|
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
|
Slide:
Spoken Tutorial workshops
|
The Spoken Tutorial Project team conducts workshops and gives certificates.
For more details, please write to us.
|
Slide: Forum for specific questions: | Please post your timed queries in this forum. |
Slide: Acknowledgement | Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link.
|
This is Siddhartha Sarkar signing off.
Thanks for watching.
|