Skip to content

Commit

Permalink
Make plugin id semi-deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-kiaer committed Aug 5, 2020
1 parent c7967cf commit eff8431
Showing 1 changed file with 17 additions and 9 deletions.
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

0 comments on commit eff8431

Please sign in to comment.