jinja2-humanize-extension

a jinja2 extension to use humanize library inside jinja2 templates


Keywords
jinja2, extension, autoreadme, github-actions, integration-level-3, jinja2-extension
License
BSD-3-Clause
Install
pip install jinja2-humanize-extension==0.4.0

Documentation

jinja2_humanize_extension

Status (master branch)

GitHub CI Maintenance

What is it ?

This is a jinja2 extension to use humanize library inside jinja2 templates.

Syntax

The generic syntax is {{ 'VALUE'|humanize_{humanize_fn}([humanize_fn_args]) }}.

Following humanize functions are currently mapped:

  • naturalsize
  • abs_timedelta (deprecated with humanize >= 4.0, we keep it for compatibility)
  • date_and_delta (deprecated with humanize >= 4.0, we keep it for compatibility)
  • naturaldate
  • naturalday
  • naturaldelta
  • naturaltime
  • precisedelta
  • ordinal
  • intcomma
  • intword
  • apnumber
  • fractional
  • scientific
  • clamp
  • metric
  • activate
  • deactivate
  • thousands_separator
  • decimal_separator

See humanize documentation for argument details.

To take a more real example, let's take the naturalsize() function. To use it inside a jinja2 template with this extension, you have to use:


The file size is: {{ 30000000|humanize_naturalsize(binary=False, gnu=True) }}

result content will be : The file size is : 30.0 MB

You can use the same logic with all supported functions. If you need other functions, feel free to open a PullRequest.

Installation

pip install jinja2-humanize-extension

Full example

from jinja2 import Template, Environment

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

template = env.from_string("The file size is : {{ 30000000|humanize_naturalsize() }}")
result = template.render()

# [...]

result content will be : The file size is : 30.0 MB

Contributing guide

See CONTRIBUTING.md file.

Code of Conduct

See CODE_OF_CONDUCT.md file.

Sponsors

(If you are officially paid to work on MetWork Framework, please contact us to add your company logo here!)

logo