How to Create Jinja2 Filters in Flask.

Posted by Dmitry

Updated on

pythonflaskjinja2snippet

Flask uses Jinja2 as a template engine. Although you are free to use any other template engine, Jinja2 is a de-facto industry standard. In this post, I'll talk about filters. Jinja2 has a list of built-in filters, and Flask leverages them. For example, when you want to capitalize a string:

<p>{{ name|capitalize }}</p>

This way capitalize function receives the name value as parameter and returns a formatted string. You can also invoke it as a function to pass extra parameters, like with default:

<p>{{ name|default("Robot") }}</p>

How to Register Custom Jinja2 Filter?

If you want to register your filter there are two ways to do this in Flask. You can either use template_filter decorator, or you can put them to jinja_env. Decorators can be defined app-wise or at the blueprint level. I prefer to define filters globally via jinja_env.

Extending jinja_env

def locale_code_to_name_filter(locale_code: str) -> str:
    if locale_code == "en_us":
        return "English (US)"

    return locale_code


app.jinja_env.filters['lc_name'] = locale_code_to_name_filter

Using template_filter decorator

@app.template_filter("lc_name")
def locale_code_to_name_filter(locale_code: str) -> str:
    if locale_code == "en_us":
        return "English (US)"

    return locale_code

Then in your templates:

<p>{{ locale|lc_name }}</p>
Take a look at how I solved Jinja2 filters structure with my Flask Boilerplate. Jinja Customization.

The Latest

Quick Python Intro to OpenAI Chat Completion Functions

Quick Python Intro to OpenAI Chat Completion Functions
pythonopenaichatgpt

In this brief article, I will underline the key points and present an illustrative code snippet demonstrating how to make an API call to Wikipedia using user input.

recv() failed (104: Connection reset by peer) while reading response header from upstream

uWSGI. recv() failed (104: Connection reset by peer) while reading response header from upstream
pythonuwsgierrorsnippet

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript

Geolocate the Location of an IP Address With Cloudflare Workers and JavaScript
javascriptcloudflaregeolocationworkers

Detect a visitor's country and city by IP address with Cloudflare Workers and JavaScript.

JavaScript Document Onload + TypeScript version

JavaScript Document Onload + TypeScript version
javascriptDOMsnippetbasicstypescript
🗃JavaScript Basics

Universal code-snippet to know when HTML is ready for modern browsers.

JavaScript addEventListener

JavaScript addEventListener
javascriptDOMsnippetbasics
🗃JavaScript Basics

How to Upload Files To Bunnynet Storage

How to Upload Files To Bunnynet Storage
pythonbunny_netstoragesnippet

Bunny.net is a well-known CDN and storage among developers. Here is my python snippet on how to upload a file to the storage.

How to Copy Text to Clipboard With Javascript

How to Copy Text to Clipboard With Javascript.
简体中文javascriptDOMbrowser

Here is a short snippet on how to copy text to the clipboard with Javascript. Your visitors will thank you. Most likely not, but here we are.

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.

Flask Boilerplate and Your Guide to Flask in 2023. With SQLAlchemy.
boilerplateopen sourceflask

Flask-Backbone is my take to create an initial structure and a set of rules, so most of my flask projects are easy to support.

How to Import CSV Files to PostgreSQL with Pgfutter

How to Import CSV Files to PostgreSQL with Pgfutter.
csvpostgresql

Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.

How to Upload Files to DigitalOcean Spaces with Python

How to Upload Files to DigitalOcean Spaces with Python
digitaloceanpython

Snippet on how to upload files to DigitalOcean Spaces with boto3 and python.