Skip to the content.

Open Session Week 3

f-strings and Comments

1. Using f-strings for Formatting

name = "Shiv"
place = "Chennai"
profession = "Data Analyst"

text = f"My name is {name}, I live in {place} and I am {profession}."
print(text)

2. Commented Code

# text = "Today is sunday"
# print(text)

Key Points:

Jinja2

1. Defining Variables

# use `pip install jinja2` to install the jinja 
name = "Shiv"
place = "Chennai"
profession = "Data Analyst"

2. Step 1: Create Template Text with Placeholders


temp = "My name is {{name}}, I live in {{place}} and I am a {{profession}}"

This step essentially creates the structure of the output you want.

3. Step 2: Convert Text to a Template

made_temp = Template(temp)

4. Step 3: Render the Template with Data

output = made_temp.render(name=name, place=place, profession=profession)

This will generate the following string:

My name is Shiv, I live in Chennai and I am a Data Analyst

5. Output the Result

print(output)

Summary:

This method allows you to dynamically generate text (in this case, HTML or simple strings) by inserting data at runtime.

Jinja2 and HTML

1. Defining Variables

name = "Divya"
place = "Delhi"

2. Step 1: Creating the HTML Template with Placeholders


temp = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
        </head>
        <body>
            <h2>My name is {{name}}</h2>
            <h2>I live in {{place}}</h2>
        </body>
        </html>
"""

This template forms a structure of a basic HTML page, with two headings displaying a person's name and place of residence.


3. Step 2: Converting Text to a Jinja2 Template

made_temp = Template(temp)

4. Step 3: Rendering the Template with Data

out = made_temp.render(name=name, place=place)

The final rendered HTML string will look like this:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h2>My name is Divya</h2>
    <h2>I live in Delhi</h2>
</body>
</html>

5. Printing the Output

print(out)

Summary:

  1. Template Creation: You define a template (in this case, an HTML structure) with placeholders for the variables.
  2. Converting to a Template: The template string is converted into a Jinja2 Template object.
  3. Rendering: The .render() method substitutes the placeholders ({{name}}, {{place}}) with the actual data values ("Divya" and "Delhi").
  4. Output: The final HTML with the substituted values is printed out.

This technique is useful when you want to dynamically generate HTML or text-based content using data provided at runtime.

Jinja2 and for loop

1. Defining a List

data = ["Programmer", "Analyst", "Scientist"]

2. Step 1: Creating the Template with a For Loop


temp = "My data is: {%for i in Data %} {{i}} {% endfor %}"

When rendered, this loop will iterate over the list Data and insert each value into the final output.


3. Step 2: Converting the String into a Jinja2 Template

made_temp = Template(temp)

4. Step 3: Rendering the Template with Data

out = made_temp.render(Data=data)

The for loop inside the template will iterate over this list and print each value.

When rendered, the result will be:

My data is: Programmer Analyst Scientist 

5. Printing the Output

print(out)
My data is: Programmer Analyst Scientist 

It displays each item from the list data on the same line, separated by spaces.


Summary:

  1. Template Creation: A template string is created with a for loop to iterate over the list Data.
  2. Converting to a Template: The template string is converted into a Jinja2 template object.
  3. Rendering: The .render() method substitutes the list data for Data and loops over it, inserting each element into the final string.
  4. Output: The final rendered string (with the list elements inserted) is printed.

This method allows you to dynamically generate strings by looping over data using Jinja2 templates.

Jinja2 and for loop with if condition

1. Defining the List

data = ["Programmer", "Analyst", "Scientist"]

2. Creating the Template


temp = """
      {% for i in data %}
         {% if "z" in i %}
            {{i}}
         {% endif %}
         
      {% endfor %}
      No data found   
      """

If no string in the list contains the letter "z", the template will not print any of the list items. After the loop, the string "No data found" will always be printed.


3. Converting to a Template Object

made_temp = Template(temp)

4. Rendering the Template

out = made_temp.render(data=data)

Therefore, no strings from the list will be printed.


5. Printing the Output

print(out)
No data found

Explanation Summary:

  1. Template Creation: You define a template that loops over a list and checks whether each string contains the letter "z". If it does, the string is printed.
  2. Converting to a Template Object: The template string is converted into a Jinja2 Template object.
  3. Rendering the Template: You render the template by passing the data list, but no strings match the condition (having a "z").
  4. Output: Since no list items meet the condition, the final output will be "No data found".

This template structure is useful when you want to filter data dynamically based on certain conditions.

Jinja2 and for loop with if , elif and else condition

1. Defining the Variable

subject = "MAD 1"

2. Creating the Template


temp = """
      {% if "2" in sub %}
        {{sub}}
      {% else %}
      Required subject not found
      {% endif %}
"""


3. Converting to a Template Object

made_temp = Template(temp)

4. Rendering the Template

out = made_temp.render(sub=subject)

5. Printing the Output

print(out)
Required subject not found

Summary:

  1. Template Creation: A Jinja2 template is defined that checks for the presence of the character "2" in the sub variable.
  2. Converting to a Template Object: The template string is converted into a Jinja2 Template object.
  3. Rendering: The template is rendered by passing the variable subject (with the value "MAD 1") as sub.
  4. Output: Since "2" is not found in "MAD 1", the output is "Required subject not found".

This example demonstrates how to use Jinja2 for conditional rendering based on the content of a variable.