| |
@@ -9,50 +9,12 @@
|
| |
from importlib import import_module
|
| |
from .caching import CachedFunction
|
| |
from .view import WidgetView
|
| |
+ from .parameters import WidgetParameter
|
| |
|
| |
|
| |
log = logging.getLogger(__name__)
|
| |
|
| |
|
| |
- class WidgetParameter(object):
|
| |
- """
|
| |
- Configuration option for a widget.
|
| |
-
|
| |
- A widget can be configured differently for each instance of this widget in
|
| |
- a hub. The list of configuration options is described by the list of
|
| |
- :py:class:`WidgetParameter` objects returned by the widget's
|
| |
- :py:meth:`~Widget.get_parameters` method.
|
| |
-
|
| |
- The value of the parameter is stored in the database as the value returned
|
| |
- by the validator's call. It can thus be a string, an integer, a list, a
|
| |
- dict, or any JSON-serializable value.
|
| |
-
|
| |
- Attributes:
|
| |
- name (str): The name of the parameter.
|
| |
- label (str): A humanized name of the parameter, which will be shown in
|
| |
- the UI.
|
| |
- default: The default value if this parameter is not set.
|
| |
- validator (callable): A validator function that will be used to convert
|
| |
- the parameter value to a JSON-serializable value, raising an
|
| |
- exception if it is invalid.
|
| |
- help (str): A help text that will be shown in the UI.
|
| |
- secret (bool): If True, this parameter will not be exposed to users
|
| |
- viewing the widget, it will only be available in the widget
|
| |
- configuration.
|
| |
- """
|
| |
-
|
| |
- _attrs = ('name', 'label', 'default', 'validator', 'help', 'secret')
|
| |
-
|
| |
- def __init__(self, **kwargs):
|
| |
- for name in self._attrs:
|
| |
- setattr(self, name, kwargs.pop(name, None))
|
| |
- for name in kwargs:
|
| |
- raise TypeError("Invalid attribute: %s" % name)
|
| |
- # Set default validator
|
| |
- if self.validator is None:
|
| |
- self.validator = lambda x: x
|
| |
-
|
| |
-
|
| |
class Widget(object):
|
| |
"""
|
| |
The main widget class, you must subclass it to create a widget.
|
| |
@@ -254,16 +216,10 @@
|
| |
hiddenIfEmpty=self.hidden_if_empty,
|
| |
cssClass=self.display_css,
|
| |
isReact=self.is_react,
|
| |
- params=[],
|
| |
+ params=[
|
| |
+ param.to_dict() for param in self.get_parameters()
|
| |
+ ],
|
| |
)
|
| |
- for param in self.get_parameters():
|
| |
- param_data = dict(
|
| |
- name=param.name,
|
| |
- label=param.label,
|
| |
- default=param.default,
|
| |
- help=param.help,
|
| |
- )
|
| |
- props["params"].append(param_data)
|
| |
if instance is not None:
|
| |
props.update({
|
| |
"idx": instance.idx,
|
| |
Currently if you need anything different from an input field for your widget parameter, you need to setup a React-based widget. It's a bit sad. This PR lets you use, for example, textarea tags for your parameters by just setting
render_tag = "textarea"
in your parameter definition.