Skip to main content

Creating a Description

This tutorial will show you how to create a customized description from dataframe columns with a jinja template. We will start by using a simple, hard coded, jinja template then show how to create and use a dynamic template.

We will largely be working with the create.jinja wrangle if you are unfamiliar with it.

Hard Coded Template​

The easiest way to create a new description is to hard code a jinja template that can be used across the entire dataset. We will use the table below as a example.

BrandProduct TypeFeatures
BoschAngle Grinder4.5", 120v
MakitaImpact Driver
DeWaltReciprocating Saw20v
MilwaukeeOrbital SanderWith dust extraction
wrangles:
- rename:
Product Type: Product_Type

- create.jinja:
output: Description
template:
string: {{ Brand }} {{ Product_Type }} ({{ Features }})

- rename:
Product_Type: Product Type

In the recipe above, we first renamed the Product Type column to Product_Type because jinja does not allow variables to contain spaces. The jinja templated uses variables (wrapped in double squigly brackets) which correspond to column names. Notice that anything not wrapped in double squigly brackets is passed through to the output, in this case parenthesis.

BrandProduct TypeFeaturesDescription
BoschAngle Grinder4.5", 120vBosch Angle Grinder (4.5", 120v)
MakitaImpact DriverMakita Impact Driver ()
DeWaltReciprocating Saw20vDeWalt Reciprocating Saw (20v)
MilwaukeeOrbital SanderWith dust extractionMilwaukee Orbital Sander (With dust extraction)

As you can see in the second row, the Features column is empty which resulted in an empty set of parenthesis. Let's add an if statement to our template to deal with empty Features.

wrangles:
- rename:
Product Type: Product_Type

- create.jinja:
output: Description
template:
string: {{ Brand }} {{ Product_Type }} {% if Features %} ({{ Features }}) {% endif %}

- rename:
Product_Type: Product Type

In the new template, we used a conditional control structure (ie if statement) to keep from outputting empty parenthesis. In jinja, control structures are wrapped in "{% %}".

BrandProduct TypeFeaturesDescription
BoschAngle Grinder4.5", 120vBosch Angle Grinder (4.5", 120v)
MakitaImpact DriverMakita Impact Driver
DeWaltReciprocating Saw20vDeWalt Reciprocating Saw (20v)
MilwaukeeOrbital SanderWith dust extractionMilwaukee Orbital Sander (With dust extraction)

Dynamic Template​

By emplementing yaml aliases, along with a custom python function, we can utilize different templates for different categories of data. In this example, we will use Product Category to decide what sort of template we want to emplement.

The table below illustrates our example data:

BrandProduct TypeProduct CategoryVoltageHandle LengthTPI
MakitaImpact DriverPower Tools18v
KleinDiagonal Cutting PliersHand Tools4.5"
DiabloReciprocating Saw BladePower Tool Accessories11
alias:
- &Templates {
Power Tools: '{{ Brand }} {{ Product_Type }} ({{ Voltage }})',
Hand Tools: '{{ Brand }} {{ Product_Type }} ({{ Handle_Length }})',
Power Tool Accessories: '{{ Brand }} {{ Product_Type }} ({{ TPI }}tpi)'
}

wrangles:
- rename:
Product Type: Product_Type
Product Category: Product_Category
Handle Length: Handle_Length

- custom.template_builder:
category: Product_Category
alias: *Templates
output: Template

- create.jinja:
output: Description
template:
column: Template

- rename:
Product_Type: Product Type
Product_Category: Product Category
Handle_Length: Handle Length

write:
- dataframe:
not_columns:
- Template
def template_builder(df, category, alias, output):
temp_list = []
for i in range(len(df)):
temp_list.append(alias[df[Category][i]])
df[output] = temp_list
return df

df = wrangles.recipe.run(recipe='temporary.wrgl.yml', dataframe=data, functions=template_builder)

In the above code, a custom function (template_builder) is used to step into the alias and output a column of templates based on category.

BrandProduct TypeProduct CategoryVoltageHandle LengthTPIDescription
MakitaImpact DriverPower Tools18vMakita Impact Driver (18v)
KleinDiagonal Cutting PliersHand Tools4.5"Klein Diagonal Cutting Pliers (4.5")
DiabloReciprocating Saw BladePower Tool Accessories11Diablo Reciprocating Saw Blade (11tpi)

As you can see in our output, each product utilizes a different template based on their respective category.

There are many ways to implement jinja templates to write descriptions, and this is a very simple example. For loops, if statements and many more python-like functions can be used in jinja to taylor your output to your needs. See the jinja documentation if this example does not fit your needs.