--- title: Stateful Plugins keywords: fastai sidebar: home_sidebar nb_path: "nbs/plugin.stateful.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

If a plugin with persistent and runtime states are going to be used, this includes the plugins with user interaction, then StatefulPlugin should be used.

If a plugin needs views, an account to login or a last state to continue then a persistent state should be deployed into the pod.

Persistent state of a plugin

{% raw %}

class PersistentState[source]

PersistentState(pluginName=None, state=None, account=None, view=None, **kwargs) :: Item

Persistent state variables saved for plugin such as views, accounts, the last state to resume from etc.

{% endraw %} {% raw %}
{% endraw %}

Runtime state of a plugin

{% raw %}
{% endraw %}

StatefulPlugin is a sub-class of PluginBase that allows setting both runtime and persistent states.

{% raw %}

class StatefulPlugin[source]

StatefulPlugin(runId=None, persistenceId=None, **kwargs) :: PluginBase

Provides state/view setter and getter functions to plugin runtime

{% endraw %} {% raw %}
{% endraw %}

Setting and communicating states through PluginRun

An example stateful plugin is below.

Through this inherited class you can set states to control the flow and views that are displayed to user.

from pymemri.data.schema import Person

class MyStatefulPlugin(StatefulPlugin):

    def __init__(self, runId=None, views=None, **kwargs):
        super().__init__(runId=runId, views=None, **kwargs)

    def run(self, client):
        # plugin's magic happens here

        # manipulate run state
        self.set_run_vars(client, {'state': 'Running'})

        # set UI view (CVU)
        self.set_run_view(client, 'splash-screen')

        # create items in POD
        imported_person = Person(firstName="Hari", lastName="Seldon")
        client.create(imported_person)

        # set persistent state
        self.set_state_str("continue_from:5021")

        return

    def add_to_schema(self, client):
        print("Adding schema")
        super().add_to_schema(client)
        # add plugin-specific schemas here
        client.add_to_schema(Person(firstName="", lastName=""))
        pass