Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Jinja2 templates in VSCode

So I found the "jinja: "true" option for launch.json and am trying to make jinja debugging work, unsuccessfully so far.

My launch.json is currently:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": " uvicorn debug",
            "type": "python",
            "request": "launch", // set to "test" for "justMyCode" to work. 
            "module": "uvicorn",
            "args":
            [
                "project.asgi:app",
                "--reload",
            ],
            "jinja": true,
            "justMyCode": false
        }
    ] }

I faced a problem that I could not set up breakpoints in jinja template file, but solved it adding "debug.allowBreakpointsEverywhere": true to vscode settings.

My questions are as follows:
a) How is debugging template supposed to work? I add breakpoint anywhere in the template and execution stops there? Or maybe this 'jinja': true means something completely different?
b) Based on the response on a), if breakpoints in jinja template file should work, how to make this happen, as currently they seem to just be ignored?

Thanks a lot!

like image 544
toinbis Avatar asked Aug 21 '20 06:08

toinbis


People also ask

How do I Debug code in Visual Studio Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I Debug a code in Visual Studio Python?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


1 Answers

First I tried the example on the Jinja Wikipedia page

**jinja-test.py**

from jinja2 import Template
with open('example.html.jinja') as f:
    tmpl = Template(f.read())
print tmpl.render(
    variable = 'Value with <unsafe> data',
    item_list = [1, 2, 3, 4, 5, 6]
)

**example.html.jinja**

<!DOCTYPE html>
<html>
  <head>
    <title>{{ variable|escape }}</title>
  </head>
  <body>
  {%- for item in item_list %}
    {{ item }}{% if not loop.last %},{% endif %}
  {%- endfor %}
  </body>
</html>

Make sure the Language type of the template file is set to Jinja. Otherwise you can't set breakpoints.

Because the file was read to a string before given to the Jinja Template class there was no link to the breakpoint set on the template.

Reading the source code of jinja2.Template I found that the preferred way of creating a jinja2.Template is through the jinja2.Environment instance.

After a search I found an other Jinja usage example on SO using the jinja2.Environment a Loader.

**jinja-test-2.py** using a FileSystemLoader

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('.'))
# env = Environment(loader=FileSystemLoader('templates'))
tmpl = env.get_template('example.html.jinja')

print (tmpl.render(
    variable = 'Value with <unsafe> data',
    item_list = [1, 2, 3, 4, 5, 6]))

**jinja-test-2.py** using a PackageLoader

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('main', 'templates'))
tmpl = env.get_template('example.html.jinja')

print (tmpl.render(
    variable = 'Value with <unsafe> data',
    item_list = [1, 2, 3, 4, 5, 6]))

Because Jinja uses the module pkg_resources you must add an empty main/__init__.py if you use the PackageLoader. Or you get an Exception

"Can't perform this operation for unregistered loader type"

When you now set a breakpoint in the template and run the application the debugger stops in the template and you can step through with F10.

Make sure the Language type of the template file is set to Jinja. Otherwise you can't set breakpoints.

like image 74
rioV8 Avatar answered Oct 17 '22 18:10

rioV8