Python-Flask/C2/Introduction-to-Flask-Templates/English
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
|
Slides 3: System Requirements | To record this tutorial, I’m using
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
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.
MVC paradigm separates a web app into three interlinked components.
|
Slide 7: MVC Architecture (Model) |
|
Slide 8: MVC Architecture (View) |
|
Slide 9: MVC Architecture (Controller) |
|
Slide 11: MVC Architecture |
|
Slide 6: Templates |
|
Slide 7: Templates |
|
Slide 8: Templates |
|
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.
Now we will demonstrate how we can avoid returning raw HTML code. For that we have to create our HTML template files in a separate folder called templates. |
[Terminal]
mkdir templates [Enter] |
Switch to the terminal.
And let’s create a folder named templates. Type mkdir <space> templates. |
[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.
For this demonstration, let us define a new route in our hello_flask file. |
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.
This function is used to render an HTML template file. Here we pass sample_template.html as an argument to the render_template function. So, Flask will look for the file sample_template dot html inside the templates folder. |
[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]
Type http://127.0.0.1:5000/template And press Enter. |
Then, open a web browser and in the address bar, type
http://127.0.0.1:5000/template And press Enter. |
[Firefox] Highlight the text. | We got the output of the HTML page.
So we have rendered the HTML code without hardcoding it inside the python file. |
Only narration | Now switch to the hello_flask.py file
We can pass the variables as a parameter to the render_template method. 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
return render_template('sample_template.html', firstname = fname) |
Then I have passed this variable fname to the render_template method.
Now the value of the variable fname can be accessed via the variable firstname in the HTML file. |
Press Ctrl + S keys | Let’s save the file. |
[Text Editor]
In sample_template.html Highlight Spoken |
And switch to sample_template.html file.
Here, we have explicitly mentioned Spoken as firstname. We will use jinja to interact the HTML file with the python file. |
Slide 9: Jinja |
|
Modify the body as
<h1>The firstname value is {{ firstname }}</h1> (within double curly braces firstname) |
Switch to sample_template.html file.
To access a variable in Jinja, we have to write it within double curly braces. So replace the word Spoken with within-double curly braces, firstname. |
Press Ctrl + S | Save the file. |
[Terminal]
Type $ python3 -m flask run [Enter] |
And switch to the terminal.
Stop the running server and start the server once again, by typing this command. |
Type http://127.0.0.1:5000
And press Enter. Highlight Tutorial. |
Then switch to the browser and refresh the page.
Observe that, we get Tutorial. That means we have successfully passed the variable from python to HTML. |
[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('form1.html', fname=firstname, lname=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.
This code will simply display the firstname and lastname. |
Press Ctrl + S | Save the file. |
[Text Editor] Show form2.html
<!DOCTYPE html> <html> <head>
<body> <form method="POST"> Firstname: <input type="text" name="firstname"><br> Lastname: <input type="text" name="lastname"><br> <input type="submit" value="Submit"><br> </form> </body> </html> |
In form2.html, add the following piece of code.
This code will display a form to receive firstname and lastname. |
Press Ctrl + S | Save the file. |
[Terminal]
Type python3 -m flask run |
Switch to the terminal.
And run the server again. |
[Browser]
Type http://127.0.0.1:5000/form And press Enter. |
Then switch to the browser.
In the address bar, replace template with form and press Enter. |
[Browser]
Firstname : Spoken Lastname: Tutorial [Submit] |
Enter the first name as Spoken.
And last name as Tutorial. Then click on the Submit button. |
[Browser] Highlight the text on screen. | We got the output as desired. |
[Terminal]
Press Ctrl + C Type deactivate and press Enter |
Switch back to the terminal.
Let’s stop the server. And deactivate the virtual environment. |
Only narration | This brings us to an end of this tutorial.
Let us summarise. |
Slide 8: Summary | In this tutorial, we learnt about-
|
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. |