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-extensionVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a Jinja2 environment, load the `HumanizeExtension`, and then render a template using several `humanize` filters.
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.
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.
Configure Flask's `jinja_options` to include the extension, typically after initializing your Flask app: `app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension']`.
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.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']`.
Install the package using pip: `pip install jinja2-humanize-extension`.