0

I would like to use the humanize functions in my jinja2 flask app and the example from the documentation was too vague for me. I installed the extensions with pip and tried to implement them as follows:

What I would like to render is this:

<small class="text-muted">Time remaining: {{ ((submission.date_expiration - now)|naturaldelta()) }}</small>

Although I keep getting the error: jinja2.exceptions.TemplateAssertionError: No filter named 'naturaldelta'.

I added env = Environment(extensions=["jinja2_humanize_extension.HumanizeExtension"]) to my init.py but realized this isn't adding the extensions properly. Does anyone have experience with this?

1

1 Answer 1

1

Guessing you're getting that error when calling render_template from within your Flask app. I note that the full example in the extension's docs, only mentions rendering with env.from_string.

How to register this on a normal Flask app? Seems you can set app.jinja_options once you've defined the app:

app = Flask(__name__)
app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension']

I'm not actually sure if the above approach is "correct". I'd looked around for documentation on how to register a Jinja2 env on a Flask app, but couldn't find much. Here's how I did test this...

With some clues from this answer your Flask app should have an attribute app.jinja_options, which in my case with a fresh app was is empty dictionary:

>>> app = Flask(__name__)
>>> app.jinja_options
{}

So I add the extension with:

>>> app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension']

Then verify:

>>> app.jinja_options
{'extensions': ['jinja2_humanize_extension.HumanizeExtension']}

Then test at the terminal with Flask's render_template_string:

>>> from flask import render_template_string
>>> with app.app_context(): 
...  print( render_template_string('''{{300000|humanize_naturalsize()}}''', asd='abc'))
... 
300.0 kB

This should have the same effect when you're template is rendered as standard with render_template

7
  • Hey v25, thanks so much for the in depth response! I also struggled to find documentation on registering the Jinja2 environment in Flask. How is this then related to the render_template_string? Regarding the env.from_string, I don't quite understand what that is doing. Do you mind explaining a little? Does that mean that these extension functions cannot be directly implemented in the HTML file? I am curious if I can then use these functions in my HTML code (for example in the line shown in the post) as normal after adding the extension as you described. Thanks for your help! Aug 31, 2021 at 8:15
  • The example in the humanize docs uses env.from_string to return a template which can then be rendered with template.render. I think this example assumes that you're just using Jinja2 directly (Jinja2 is a module which is used by flask, but can also be used alone). The approach in my answer works with Flask, where you don't typically have direct access to the env instance in any case, and you would just use render_template (Although you didn't state how you were rendering your template - you only showed the HTML template code).
    – v25
    Aug 31, 2021 at 18:09
  • As for render_template_string this is just a Flask function which takes a string instead of the path to an HTML file, which is useful for trying stuff out in the console (within the with app.app_context block), as I have in my answer: It saves me having to write template code to a file, then call render_template with that path. BUT yes the apprach in my answer should also be compatible with "your HTML code". Please try this out, and consider marking this answer accepted if this works) :) Hope these comments clear some stuff up.
    – v25
    Aug 31, 2021 at 18:12
  • Thanks for those explanations :) I'm gonna test it out and will let you know! Aug 31, 2021 at 18:46
  • I have it working in the terminal like you showed but am still getting the same error with my render_template. I am assuming that I didn't properly add it to the app.jinja_options in my code. Here is my code: from flask import Flask app = Flask (__name__) app.jinja_options['extensions'] = ['jinja2_humanize_extension.HumanizeExtension'] @app.route("/submissions", methods=["GET", "POST"]) @login_required def submissions(): ... return render_template("curate.html", title="Curate", submission_count=submission_count, submissions=submissions, now=now) Aug 31, 2021 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.