Jinja2 Humanize Extension

0.4.0 · active · verified Thu Apr 09

jinja2-humanize-extension is a Jinja2 extension that integrates the popular `humanize` library, providing a suite of human-readable formatting filters directly within Jinja2 templates. It allows developers to easily format numbers, dates, and sizes (e.g., `naturalsize`, `naturaldate`, `intcomma`, `intword`) using Jinja's filter syntax. The current version is 0.4.0, with releases occurring periodically to maintain compatibility with its `humanize` dependency.

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a Jinja2 environment, load the `HumanizeExtension`, and then render a template using several `humanize` filters.

from jinja2 import Environment
from jinja2_humanize_extension import HumanizeExtension

# We load the extension in a jinja2 Environment
env = Environment(extensions=[HumanizeExtension])

template = env.from_string(
    "The file size is : {{ 30000000|humanize_naturalsize() }}\n"
    "Today is: {{ '2023-01-15'|humanize_naturaldate() }}\n"
    "Number with commas: {{ 1234567|humanize_intcomma() }}"
)

result = template.render()
print(result)
# Expected output:
# The file size is : 30.0 MB
# Today is: Jan 15th 2023
# Number with commas: 1,234,567

view raw JSON →