Passing a variable from Python to Jinja

I'm trying to define a variable in python and have that be used in my Jinja template. My understanding is that this needs to (somehow) get from Python into the Jinja context, but as Octoprint is ultimately making the render call I'm not quite sure how to do this correctly.

I naively tried putting:

myVar = "Hello World"

in init.py and then putting

<span>{{myVar}}</span>

but to no avail. I'm sure I'm just being dumb!

I believe this is the section of the docs you are looking for (direct link):
https://docs.octoprint.org/en/master/plugins/mixins.html#octoprint.plugin.TemplatePlugin.get_template_vars
Use get_template_vars and return a dict of values, then they can be referenced using the prefix plugin_<plugin id>_

Examples:

https://github.com/cp2004/OctoPrint-EEPROM-Marlin/blob/a0b6d1f92740da1233f8049107c13e9d8c1d5547/octoprint_eeprom_marlin/init.py#L103-L105

1 Like

Perfect! I figured it would be buried in there somewhere! Thanks :slight_smile: