Binding to OctoPrint version or feature presence?

I'd like to make a setting for my plugin that should only be visible either

  • for OctoPrint users starting version 1.9.0
  • or when Events.CONNECTIONS_AUTOREFRESHED exists (which was added in 1.9.0).

(whatever is easier)

How could I implement such kind of bindings in the plugin settings template?

I wasn't able to find and ko observables that you could use to bind visibility to, so my best guess here is to use jinja conditional around the block you don't want to include and use octoprint.util.version.is_octoprint_compatible(">=1.9.0") as your check. something along the lines of

{% if octoprint.util.version.is_octoprint_compatible(">=1.9.0") %}
<stuff you want to show in template>
{% endif %}

granted, I have no idea if that will work. @Charlie_Powell is a better jinja wizard than me and may have some ideas.

1 Like

Thank you, @jneilliii

I tried this, but I'm getting There was an error with the template: 'octoprint' is undefined.
Perhaps some additional action is required to make octoprint available for jinja-template?

yeah, wasn't sure what kind of context is available in the templating side, I haven't done so much with that personally. I think I may have found a trick that might work...

<div class="row-fluid" data-bind="visible: VERSION.split('.')[0] >= 1 && VERSION.split('.')[1] >= 9">stuff goes here</div>
1 Like

Thank you very much, @jneilliii .

Binding to VERSION works.

Is there any documentation on which entities are available for binding or another way for finding that out?

Not that I'm aware of. It's JavaScript though in the context of your view model, which includes global variables so I just opened developer tools and started typing version to see if anything was there.

1 Like

VERSION is one of the variables injected by OctoPrint that's injected into the generic JavaScript land, but they aren't documented. The list is in the file here OctoPrint/src/octoprint/templates/initscript.jinja2.

The list of dependencies & callbacks available in the JS viewmodels are documented here Viewmodels β€” OctoPrint master documentation

Just to round out my answer, yes Jinja doesn't automatically have access to everything in the Python side. You would have to inject a variable using get_template_vars(), which would then be prefixed using plugin_<plugin id>_<name you gave it>.

octoprint.util.version has a few utilities like this, but yeah they are only available in the Python side of the plugin, not the UI. VERSION is just a text string, the other has functions and can interpret the normal PEP440 version specifiers.


So with this logic, you could have

def get_template_vars(self):
    return {"show_setting": octoprint.util.version.is_octoprint_compatible(">=1.9.0")}

and then in Jinja template

{% if plugin_octorelay_show_setting %}
<stuff you want to show in template>
{% endif %}

If I'm understanding correctly.