Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semi-deterministic plugin IDs #260

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions webviz_config/_plugin_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import abc
import base64
import zipfile
from uuid import uuid4
from typing import List, Optional, Type, Union

import bleach
Expand All @@ -26,6 +25,13 @@ def layout(self):
```
"""

# Class variable used for counting number of plugin instances, which
# is again used for giving semi-deterministic plugin IDs. E.g. runs
# coming from the same configuration file or webviz app will have
# the same ID for the different plugin instances, which makes it possible
# for plugins to utilize the Dash persistence system.
_number_instances = 0

# This is the default set of buttons to show in the rendered plugin
# toolbar. If the list is empty, the subclass plugin layout will be
# used directly, without any visual encapsulation layout from this
Expand Down Expand Up @@ -60,7 +66,10 @@ def __init__(self) -> None:
in its own `__init__` function in order to also run the parent `__init__`.
"""

self._plugin_uuid = uuid4()
WebvizPluginABC._number_instances += 1
self._plugin_id = (
f"{self.__class__.__name__}-{WebvizPluginABC._number_instances}"
)

def uuid(self, element: str) -> str:
"""Typically used to get a unique ID for some given element/component in
Expand All @@ -71,13 +80,12 @@ def uuid(self, element: str) -> str:
Within the same plugin instance, the returned uuid is the same for the same
element string. I.e. storing the returned value in the plugin is not necessary.

Main benefit of using this function instead of creating a UUID directly,
is that the abstract base class can in the future provide IDs that
are consistent across application restarts (i.e. when the webviz configuration
file changes in a non-portable setting).
Main benefit of using this function instead of creating a UUID directly in
the plugin subclass, is that the abstract base class provides IDs that are
unique and constant across application restarts (which makes it possible to use
e.g. Dash's persistence framework in plugins).
"""

return f"{element}-{self._plugin_uuid}"
return f"{element}-{self._plugin_id}"

@property
@abc.abstractmethod
Expand All @@ -89,7 +97,7 @@ def layout(self) -> Union[str, Type[Component]]:

@property
def _plugin_wrapper_id(self) -> str:
return f"plugin-wrapper-{self._plugin_uuid}"
return f"plugin-wrapper-{self._plugin_id}"

@property
def plugin_data_output(self) -> Output:
Expand Down