Skip to content
Advertisement

Tag: jinja2

how to get list of all variables in jinja 2 template file

I have a usecase where I want to get a list of unreferenced variables in a jinja2 template. I found some explanations on stackoverflow on how to do this, but not of these exaples use a file as a template, and I am very, very stuck Here is my code. Lines 8 and 9 can be omitted, ofc. here is

So my Jinga code is not outputing an option

This is a cs50 pset if you didn’t know. Here is the app.py You can consider that stocks is an array and its not empty The Output is just the first placeholder option and nothing more Answer When you call render_template(), you can pass Python variables to it which makes them available in your Jinja template. Change return render_template(“sell.html”) to

How to evaluate a variable within a dict in jinja

I would like to evaluate a variable within a dict object in Jinja. Is that possible? In the example, {{ obj_list }} is treated as text. Is there a syntax to evaluate this variable within? Answer You can use ~ to include variables within strings: {%- set long_dict_set = {“key”:”text text ” ~ obj_list ~ ” text text”} %} Output:

Alternative to the pandas negation operator

I’m trying to use the pandas negation operator ~ in one of my jinja2 templates but I believe its conflicting with their special operator ~. yields the following exception… I could do the operation on the python side and pass another variable with the negated selection but what’s the method name equivalent that the ~ operator maps to that I

Jinja loop.index does not print

When running the following jinja code, I only get “Column info” printed. Why the index does not appears ? Answer It sounds like the template is being treated as a Django template, not a Jinja template. Using {{ loop.index }} should work in a Jinja template, but wouldn’t work in a Django template, where you would use {{ forloop.counter }}

Advertisement