Skip to content

Commit

Permalink
Allow options for non-enabled backends (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-raillard-arm committed Apr 16, 2024
1 parent 372eb6c commit 491d4ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
11 changes: 9 additions & 2 deletions holoviews/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,13 @@ def apply(self, value, input_ranges, backend=None):
return transformed


class _OptionsDict(dict):
def __missing__(self, backend):
options = OptionTree([], groups=Options._option_groups, backend=backend)
self[backend] = options
return options


class Store:
"""
The Store is what links up HoloViews objects to their
Expand All @@ -1143,7 +1150,7 @@ class Store:

# Once register_plotting_classes is called, this OptionTree is
# populated for the given backend.
_options = {}
_options = _OptionsDict()

# Weakrefs to record objects per id
_weakrefs = {}
Expand All @@ -1156,7 +1163,7 @@ class Store:
option_setters = []

# A dictionary of custom OptionTree by custom object id by backend
_custom_options = {'matplotlib': {}}
_custom_options = defaultdict(dict)
load_counter_offset = None
save_option_state = False

Expand Down
23 changes: 14 additions & 9 deletions holoviews/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,16 @@ def _expand_options(cls, options, backend=None):

try:
backend_options = Store.options(backend=backend or current_backend)
except KeyError as e:
raise Exception(f'The {e} backend is not loaded. Please load the backend using hv.extension.') from None
except KeyError:
return {}
expanded = {}
if isinstance(options, list):
options = merge_options_to_dict(options)

for objspec, option_values in options.items():
objtype = objspec.split('.')[0]
if objtype not in backend_options:
raise ValueError(f'{objtype} type not found, could not apply options.')
continue
obj_options = backend_options[objtype]
expanded[objspec] = {g: {} for g in obj_options.groups}
for opt, value in option_values.items():
Expand Down Expand Up @@ -450,8 +450,9 @@ def builder(cls, spec=None, **kws):
backend = kws.get('backend', None)
keys = set(kws.keys())
if backend:
allowed_kws = cls._element_keywords(backend,
elements=[element])[element]
keywords = cls._element_keywords(backend,
elements=[element])
allowed_kws = keywords.get(element, keys)
invalid = keys - set(allowed_kws)
else:
mismatched = {}
Expand Down Expand Up @@ -506,11 +507,15 @@ def _element_keywords(cls, backend, elements=None):
if '.' in element: continue
element = element if isinstance(element, tuple) else (element,)
element_keywords = []
options = backend_options['.'.join(element)]
for group in Options._option_groups:
element_keywords.extend(options[group].allowed_keywords)
try:
options = backend_options['.'.join(element)]
except KeyError:
pass
else:
for group in Options._option_groups:
element_keywords.extend(options[group].allowed_keywords)

mapping[element[0]] = element_keywords
mapping[element[0]] = element_keywords
return mapping


Expand Down

0 comments on commit 491d4ca

Please sign in to comment.