From b76d1a0844423d02b7b4be693dde2944ceb7e7d9 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Wed, 18 Jan 2023 13:42:44 +0100 Subject: [PATCH] Add ability to set default Download parameters --- lumen/dashboard.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lumen/dashboard.py b/lumen/dashboard.py index f45b6587e..cc2808415 100644 --- a/lumen/dashboard.py +++ b/lumen/dashboard.py @@ -37,7 +37,7 @@ ValidationError, match_suggestion_message, validate_callback, ) from .variables.base import Variable, Variables -from .views.base import View # noqa +from .views.base import Download, View # noqa if TYPE_CHECKING: from bokeh.server.contexts import BokehSessionContext @@ -290,6 +290,8 @@ class Defaults(Component): `Defaults` to apply to the component classes. """ + download = param.Dict(default={}, doc="Defaults for the Download object") + filters = param.List(doc="Defaults for Filter objects.", class_=dict) sources = param.List(doc="Defaults for Source objects.", class_=dict) @@ -337,6 +339,30 @@ def _validate_defaults( raise ValidationError(msg, default, p) return defaults + @classmethod + def _validate_download( + cls, download_defaults: Dict[str, Any], spec: Dict[str, Any], context: Dict[str, Any] + ): + if not isinstance(download_defaults, dict): + msg = f'Defaults for Download component must be declared as a dictionary, not as a {type(download_defaults)}.' + raise ValidationError(msg, spec, 'download:') + for p in download_defaults: + if p in Download.param: + pobj = Download.param[p] + try: + pobj._validate(download_defaults[p]) + except Exception as e: + msg = f"The default for Download {p!r} parameter failed validation: {str(e)}" + raise ValidationError(msg, download_defaults, p) + continue + msg = ( + f'Default for Download {p!r} parameter cannot be set as there ' + 'is no such parameter.' + ) + msg = match_suggestion_message(p, list(Download.param), msg) + raise ValidationError(msg, download_defaults, p) + return download_defaults + @classmethod def _validate_filters( cls, filter_defaults: List[Dict[str, Any]], spec: Dict[str, Any], context: Dict[str, Any] @@ -362,6 +388,7 @@ def _validate_views( return cls._validate_defaults(View, view_defaults, spec) def apply(self): + Download.param.update(self.download) for obj, defaults in ((Filter, self.filters), (Source, self.sources), (Transform, self.transforms), (View, self.views)): for default in defaults: