Welcome back folks!

In today's blog we will be covering the topic of jinja2!

What is jinja2?

jinja2 is a text-based templating langauge created for Python developers to generate a variety of different markup formats with one of the most popular one being HTML.

In this blog post we are going to take a look at how to use jinja2 to generate templates for code which can be deployed online using Flask.

First off if you do not already have jinja2 installed you will need to go ahead and do that either in jupyter notebook or in your terminal:

! pip install jinja2

Let's start by becoming famillar with the syntax of jinja2 also know as 'Delimiters':

Statements:

Statements are expressed as: {% ... %}

This is used for writing out the structure(control flow) of the program. Statements can house conditionals such as 'if, elif, and else' statements and for-loops

Expressions:

Expressions are expressed as: {{ ... }}

Statements are used for writing out objects such as strings, numbers, lists, tuples and dictionaries.

Comments:

Comments are expressed as: {# ... #}

Comments are used to write in information outside of the actual code. For example, explaining the code to others or sharing other pieces of information.

Line Statements:

Line Statements are expressed as: # ... ##

Line Statements are enabled by the application and are equivalent of using {% ... %} for writing statements.

Now let's get try out some of the basics of jinja2:

from jinja2 import Template
t = Template('Welcome to {{something}}')
t.render (something = 'jinja2')
'Welcome to jinja2'

Here you can see we have imported Template from jinja2 and using this feature we have created a very basic peice of code.

First we stored the Template feature inside 't' along with the string: *'Welcome to {{something}}

Then using t.render we specified what the 'something' variable (written inside an Expression) in our sentence will be. In this case we specified it to be 'jinja2' so we received the output of

'Welcome to jinja2'.

We can go ahead and add multiple variables within the sentence and define what they will output back to us.

t = Template('My name is {{name}} and I am a {{occupation}}')
t.render (name = 'Vishal', occupation = 'student')
'My name is Vishal and I am a student'
t = Template('My name is {{name}} and I am a {{occupation}} at {{institute}} studying {{subject}}')
vishal = t.render(name = 'Vishal', occupation = 'student', institute = 'General Assembly', subject = 'Data Science')
vishal
'My name is Vishal and I am a student at General Assembly studying Data Science'

(I am saving our input in a variable called 'vishal' to use later on in the blog)

Using this information we can now create a quick HTML markdown:

First we need to import the packages that will display the text in HTML for us and load in our template files

from IPython.core.display import HTML
from jinja2 import Environment, FileSystemLoader
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)

We are first going to create a template in our folder. We are naming it test.html

%%writefile templates/test.html

<html>
<head>
 <title>{{title}}</title>
</head
<body>
 <h1>{{header}}</h1>
 <p>{{body}}</p>
</body>
</html>
Overwriting templates/test.html

In this step we are populating our 'test.html' file with the information we want returned back to us in HTML form.

(I am going to use the 'vishal' variable I saved earlier and have this block of code output it back to me in html)

test = env.get_template('test.html')
output = test.render(
    title='Hello', 
    header=vishal,
    body='What about you?')

HTML(output)

Hello

My name is Vishal and I am a student at General Assembly studying Data Science

What about you?