Get lengths of a list in a jinja2 template

How do I get the number of elements in a list in jinja2 template? For example, in Python: print(template.render(products=[???])) and in jinja2 <span>You have {{what goes here?}} products</span>

Sane way to define default variable values from within a jinja template?

I'd like to set default values for variables used in my Jinja template inside of the template itself. Looking at the Jinja2 documentation, I don't see any way to do this. Have I missed something?...

How to pass selected, named arguments to Jinja2's include context?

Using Django templating engine I can include another partial template while setting a custom context using named arguments, like this: {% include "list.html" with articles=articles_list1 only...

How to output loop.counter in python jinja template?

I want to be able to output the current loop iteration to my template. According to the docs, there is a loop.counter variable that I am trying to use: <ul> {% for user in userlist %} <li> ...

Return a dict object from Jinja2 macros

I have a separate file for helper macros, so I import one when needed. Here is problematic macro: {% macro row_cycler(tiles) -%} {%- set row1 = [] -%} {%- set row2 = [] -%} {%- set row3 = []...

flask render_template returning blank page/empty response

I am programming a relatively straight forward web application in Flask. I have used flask for about a year now, and have never had this issue before. In my '/create_user' function, the...

Check if key exists in a Python dict in Jinja2 templates

I have a python dictionary: settings = { "foo" : "baz", "hello" : "world" } This variable settings is then available in the Jinja2 template. I want to check if a key myProperty exists in...

Jinja2 - Keep undefined variables

I am interested in rendering a template in multiple steps or keeping the tags for the undefined variables in Jinja2. I believe this would mean not only creating the 'UndefinedSilent" class (so the...

Ansible jinja2 filters '|'(pipe) what does it mean?

I have written a task as below but can not understand what '|' does? tasks: - shell: /usr/bin/foo register: result ignore_errors: True - debug: msg="it failed" when: result|failed -...

Remove unnecessary whitespace from Jinja rendered template

I'm using curl to watch the output of my web app. When Flask and Jinja render templates, there's a lot of unnecessary white space in the output. It seems to be added by rendering various...

Ansible: Get all the IP addresses of a group

Let's imagine an inventory file like this: node-01 ansible_ssh_host=192.168.100.101 node-02 ansible_ssh_host=192.168.100.102 node-03 ansible_ssh_host=192.168.100.103 node-04...

How to get ALL undefined variables from a Jinja2 template?

I am trying to get all undefined variables from a Jinja2 template. Assume that I have a template like below. tmpstr = """ {% for row in csv %} sample {{row.field1}} stuff {{row.field2}} morestuff...

Can I use jinja2 without a framework?

I'm a complete newbie when it comes to jinja2. Is it necessary to build in a framework to use jinja2? Ideally, I would use html, css, js, and jinja. And I suppose a python file.

Generate HTML from HTML template in python?

I want to design my own HTML template with tags like JSP or Jade and then pass data from python to it and let it generate full html page. I don't want to construct document at python side like...

Do Django templates allow for range() in for loops?

I am trying to use a for loop in my template but I am getting an error. Here is the jinja: {% for i in range(1,10) %} <h2>{{i}}</h2> {% endfor %} Here is the...

Running Flask environment using HTML:receiving error message of expected else statement

I am getting an error message as follows: TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to...

flask multiple submit button

I am using flask and jinja2 to create a simple web app to serve up a simple sklearn algorithm for predictions. In my html I need to get 4 variables: client id, textid, textid1, textid2 It...

How to remove trailing whitespaces from string in Jinja?

In my case all the strings are single-worded and the trailing whitespaces from the strings need to be removed, e.g., 'hello '→ 'hello'; 'hello '→ 'hello'. One approach could be using split,...

Jupyter Notebook - Cannot Connect to Kernel

I've tried installing jupyter notebook using pip3 install jupyter. Every time i launch a new jupyter notebook, the notebook is unable to connect to the kernel. See screenshot below Attaching my...

WEBP support not installed error with Pillow included in Anaconda

I have written a small code to open webp image in the Anaconda prompt. from PIL import Image im = Image.open('test.webp') It causes the following...

Ansible: How to extract values from a dictionary of lists

I'm trying to fetch the values from list of dict but unable to get the exact output, which is required Using Linux server with installed versions of ansible 2.7.5 & jinja2 2.7.2 . The below one is...

jinja2.exceptions.TemplateSyntaxError: unexpected char '&'

I am deploying my machine learning model locally and use flask for that. I have my main.html and app.py but when I try to run it in the console I get the Error message above. I cannot find any "&"...

"AssertionError: Torch not compiled with CUDA enabled" in spite upgrading to CUDA version

I figured out this is a popular question, but still I couldn't find a solution for that. I'm trying to run a simple repo Here which uses PyTorch. Although I just upgraded my Pytorch to the latest...

flask run does not work when using flask restplus

I am new to python and flask. I want to use flask-restplus. On using flask restplus, I am unable to start my app with "flask run" command. However, it does start when I run it as module "python3...

Ansible search for a substring in stdout output

Team, I am trying to match a version and that fails when whole string does not match. so I just want to match first two octects. I tried several combons but no luck. - name: "Validate k8s...

Flask not activating debug mode

I am getting started with flask, I am trying to follow some tutorials, but I have been unable to run Flask app in debug mode. I tried the simplest code I found: from flask import Flask app =...

Error with Python3 to connect to snowflake: snowflake.connector.errors.OperationalError::Failed to execute:'SSLSocket' object has no attribute 'conn

It seems there is some issue with latest snowflake connector . I am trying to install it on Ubuntu OS and trying to connect to snowflake but it gives error AttributeError: 'SSLSocket' object has...

Heroku SSL connection error unsupported protocol

I have been using Heroku for a while to host my Discord bot. It has been connecting to a MySQL database hosted on ClearDB successfully. However, very recently, whenever I use the bot and it tries...

How to copy license from host package

When compiling software that makes use of a header-only library, licensing can require a copy of the library's license to be distributed with the compiled software. Moreover, Conda Forge is...

Google app engine deployment fails- Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')

We are using command prompt c:\gcloud app deploy app.yaml, but get the following error: Running "python3 -m pip install --requirement requirements.txt --upgrade --upgrade-strategy only-if-needed...