Registry / web-framework / jinja2-humanize-extension

jinja2-humanize-extension

JSON →
library0.4.0pypypi✓ verified 23d ago

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.

pip install jinja2-humanize-extension
INSTALL
IMPORT
SIG · JINJA2-HUMANIZE-EX
J
jinja2-humanize-extension
web-frameworkpythonv0.4.0
Install
1.9s avg
Import
148ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.154s · 19.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.142s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HumanizeExtension
from jinja2_humanize_extension import HumanizeExtension
Used when adding the extension to a Jinja2 Environment.
Environment
from jinja2 import Environment
Required to create a Jinja2 environment and load the extension.

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
Debug
Known issues
breakingVersion 0.3.0 of `jinja2-humanize-extension` introduced compatibility with `humanize` library version 4.0 and later. Prior to v0.3.0 (specifically v0.2.1 and earlier), the extension was only compatible with `humanize < 4.0.0`.
fix
When upgrading `jinja2-humanize-extension` to `0.3.0` or higher, ensure your `humanize` dependency is `4.0.0` or greater. If using `jinja2-humanize-extension < 0.3.0`, ensure `humanize < 4.0.0` to avoid dependency conflicts and runtime errors.
affects: <0.3.0
deprecated`humanize` version 4.0 deprecated `abs_timedelta` and `date_and_delta` functions. While `jinja2-humanize-extension` retains these as filters for compatibility, their future availability or behavior might be subject to changes in the `humanize` library itself.
fix
Consider refactoring templates to use alternative `humanize` functions if `abs_timedelta` or `date_and_delta` are heavily used, to future-proof your application against potential removals in future `humanize` versions.
affects: 0.3.0 and later (when used with humanize >= 4.0)
gotchaWhen integrating `jinja2-humanize-extension` with Flask, merely importing the extension might not make the filters available through `render_template`. The extension needs to be explicitly added to Flask's Jinja2 environment options.
fix
Configure Flask's `jinja_options` to include the extension, typically after initializing your Flask app: `app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension']`.
affects: All versions when used with Flask
Errors
Common errors & fixes
TemplateAssertionError: No filter named 'naturaldate'.
The jinja2-humanize-extension was not loaded into the Jinja2 environment, or the filter is being called with the incorrect name (e.g., 'naturaldate' instead of 'humanize_naturaldate'). Filters provided by this extension are prefixed with 'humanize_'.
fix
Ensure the extension is properly loaded in your Jinja2 Environment: `env = Environment(extensions=["jinja2_humanize_extension.HumanizeExtension"])`. Then, use the correct filter name in your template, e.g., `{{ my_date_value|humanize_naturaldate() }}` for dates, or `{{ 30000000|humanize_naturalsize() }}` for sizes.
jinja2.exceptions.ExtensionError: Could not load extension 'jinja2_humanize_extension.HumanizeExtension'
Jinja2 cannot find the specified extension module. This usually indicates a typo in the extension's import path or the package is not correctly installed or accessible in the Python environment.
fix
Verify that `jinja2-humanize-extension` is installed (`pip show jinja2-humanize-extension`) and that the extension string in the `Environment` constructor is exactly `'jinja2_humanize_extension.HumanizeExtension'`. For Flask applications, you might need to configure `app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension']`.
ImportError: No module named 'jinja2_humanize_extension'
The `jinja2-humanize-extension` Python package, or one of its dependencies, has not been installed in the current Python environment.
fix
Install the package using pip: `pip install jinja2-humanize-extension`.
Upgrade
Version history
0.4.0latest on PyPI · released Sep 1, 2023
Audit
Dependencies
jinja2requiredCore templating engine this library extends.
humanizerequiredProvides the core humanization functions. Version compatibility is critical for this dependency, see warnings.
Agent activity
9 hits · last 30 days
node
8
Resources
jinja2-humanize-extension — pip install jinja2-humanize-extension · libregistry