WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Flask – Templates


It is possible to restore the output of a function bound to a specific URL as HTML. For example, in the following script, hi() function will render 'Hi World' with

tag attached to it

.
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
   return '<html><body><h1>Hello World</h1></body></html>'

if __name__ == '__main__':
   app.run(debug = True)

However, producing HTML content from Python code is cumbersome, particularly when variable data and Python language elements like conditionals or loops should be put. This would require regular escaping away from HTML.

This is the place one can advantage Jinja2 template engine, on which Flask is based. Rather than returning hardcode HTML from the function, a HTML document can be rendered by the render_template() function.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
   return render_template(‘hello.html’)

if __name__ == '__main__':
   app.run(debug = True)

Flask will try to find the HTML document in the templates folder, in a similar folder where this script is present.

  • Application envelope
    • Hello.py
    • templates
      • hello.html

The term 'web templating system' refers to structuring a HTML script in which the variable information can be embedded dynamically. A web template framework contains a format motor, some sort of data source and a template processor.

Flask utilizes jinga2 template engine. A web template contains HTML syntax interspersed placeholders for variables and expressions (for these situation Python expressions) which are replaced values when the format is rendered.

The following code is saved as hello.html in the templates folder.

<!doctype html>
<html>
   <body>
   
      <h1>Hello {{ name }}!</h1>
      
   </body>
</html>

Next, run the following script from Python shell.

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/hello/<user>')
def hello_name(user):
   return render_template('hello.html', name = user)

if __name__ == '__main__':
   app.run(debug = True)

As the development server starts running, open the browser and enter URL as − http://localhost:5000/hello/mvl

The variable part of URL is inserted at {{ name }} place holder.

As the development server starts running, open the browser and enter URL as − http://localhost:5000/hello/mvl

The variable part of URL is inserted at {{ name }} place holder.

The Jinga2 template engine uses the following delimiters for escaping from HTML.

  • {% ... %} for Statements
  • {{ ... }} for Expressions to print to the template output
  • {# ... #} for Comments not included in the template output
  • # ... ## for Line Statements

In the following example, use of conditional statement in the format is demonstrated. The URL rule to the hello() function accepts the whole integer parameter. It is passed to the hello.html format. Inside it, the estimation of number got (marks) is analyzed (more noteworthy or under 50) and in like manner HTML is restrictively rendered.

The Python Script is as follows −

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/hello/<int:score>')
def hello_name(score):
   return render_template('hello.html', marks = score)

if __name__ == '__main__':
   app.run(debug = True)

HTML template script of hello.html is as follows −

<!doctype html>
<html>
   <body>
      {% if marks>50 %}
         <h1> Your result is pass!</h1>
      {% else %}
         <h1>Your result is fail</h1>
      {% endif %}
   </body>
</html>

Note that the conditional statements if-else and endif are enclosed in delimiter {%..%}.