Sync OctoPi settings when changing them in the backend

Hi!

I'm currently struggling with a feature I want to implement for my plugin.
It should have quick action buttons on its main tab (e.g. for enabling/disabling some functionality).

Therefore I call an endpoint in my backend and there I change a setting like this:

import flask
        data = flask.request.get_json()

        if 'enabled' in data:
            self._settings.set(["enabled"], bool(data['enabled']))

Since the frontend doesn't know about this, I manually update the corresponding observable in my settings so when I open the settings page of my plugin it has the correct values:

self.settings.settings.plugins.timelapseplus.enabled(newVal);

This works and whenever I use this funcitonality, the checkbox on my settings page is updated properly. But here comes the problem: Let's say I update the 'enabled' state via the backend to 'false', the checkbox in my settings is unchecked but when I turn it back on in the settings and try to save, nothing happens. It seems like the frontend or backend has an internal state (?) and decides that there is nothing to save. Also the 'on_settings_save()' method isn't called in my backend.

Do you have an idea how I could solve this?
Thank you in advance!

After you are finished setting the data with set, call self._settings.save(trigger_event=True). You need to call save to actually write the config to the config.yaml on disk, then the trigger_event part tells any currently connected clients that they need to update the settings in the UI, and OctoPrint handles that part.

For the part about changes to the frontend not saving, if that's still the case after the above change then we'll probably need to see more of your front-end code.

1 Like

Hi @Charlie_Powell
That was a helpful answer :+1:
It works perfectly! I even don't have to to write the setting in the frontend. Now I'm just calling the save() method and the frontend is updated properly! Thank you very much! :slight_smile: