Skip to main content

a jinja2 extension to use humanize library inside jinja2 templates

Project description

What is it ?

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


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

Following humanize functions are currently mapped:

  • naturalsize
  • abs_timedelta
  • date_and_delta
  • naturaldate
  • naturalday
  • naturaldelta
  • naturaltime
  • precisedelta
  • humanize_intword

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) }}

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


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()

# [...]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2_humanize_extension-0.2.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

jinja2_humanize_extension-0.2.1-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page