Giter Club home page Giter Club logo

jinja2_humanize_extension's Introduction

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

jinja2_humanize_extension's People

Contributors

thebaptiste avatar thefab avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

velle

jinja2_humanize_extension's Issues

Broken dependency on `humanize.abs_timedelta`

As of v4.0.0 of humanize, abs_timedelta() is deprecated.

My installation (humanize-4.0.0, jinja2-3.0.3, jinja2-humanize-extension-0.2.0) produces this:

Traceback (most recent call last):
  File "/var/tmp/pbench-test-server/test-2/opt/pbench-server/bin/pbench-tarball-stats.py", line 325, in <module>
    status = main(parsed)
  File "/var/tmp/pbench-test-server/test-2/opt/pbench-server/bin/pbench-tarball-stats.py", line 297, in main
    env = jinja2.Environment(
  File "/var/tmp/tox/jenkins-unittests/lib/python3.9/site-packages/jinja2/environment.py", line 362, in __init__
    self.extensions = load_extensions(self, extensions)
  File "/var/tmp/tox/jenkins-unittests/lib/python3.9/site-packages/jinja2/environment.py", line 118, in load_extensions
    extension = t.cast(t.Type["Extension"], import_string(extension))
  File "/var/tmp/tox/jenkins-unittests/lib/python3.9/site-packages/jinja2/utils.py", line 215, in import_string
    return getattr(__import__(module, None, None, [obj]), obj)
  File "/var/tmp/tox/jenkins-unittests/lib/python3.9/site-packages/jinja2_humanize_extension/__init__.py", line 5, in <module>
    from humanize.time import (
ImportError: cannot import name 'abs_timedelta' from 'humanize.time' (/var/tmp/tox/jenkins-unittests/lib/python3.9/site-packages/humanize/time.py)

Use more recent version of humanize?

jinja2 humanize does not work with values that are timezone aware. In order to get the right result, one must do stuff like:

{{ t.astimezone().replace(tzinfo=None) | humanize_naturaltime }}

or

{{ t | humanize_naturaltime(when=datetime.datetime.now(tz=dateutil.tz.gettz()) ) }}

More recent versions of humanize work well with time/datetime/date objects with timezone set, and that would allow the following to work:

{{ t | humanize_naturaltime }}

Are there any specific reasons why the requirements.txt file is set to not use above version 3 of humanize?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.