From 3da79a15fc4d55a064c4b1508dc5d77afa6d1265 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Fri, 25 Jan 2019 00:35:23 +0000 Subject: [PATCH] Allow defining hooks on backend load --- holoviews/util/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/holoviews/util/__init__.py b/holoviews/util/__init__.py index 09cfcde687..0f807ad4e2 100644 --- a/holoviews/util/__init__.py +++ b/holoviews/util/__init__.py @@ -1,5 +1,7 @@ import os, sys, inspect, shutil +from collections import defaultdict + import param from pyviz_comms import extension as _pyviz_extension @@ -576,6 +578,9 @@ class extension(_pyviz_extension): 'bokeh': 'bokeh', 'plotly': 'plotly'} + # Hooks run when a backend is loaded + _backend_hooks = defaultdict(list) + def __call__(self, *args, **params): # Get requested backends config = params.pop('config', {}) @@ -616,11 +621,23 @@ def __call__(self, *args, **params): Store.output_settings.allowed['backend'] = list_backends() Store.output_settings.allowed['fig'] = list_formats('fig', backend) Store.output_settings.allowed['holomap'] = list_formats('holomap', backend) + for hook in self._backend_hooks[backend]: + try: + hook() + except Exception as e: + self.param.warning('%s backend hook %s failed with ' + 'following exception: %s' % + (backend, hook, e)) if selected_backend is None: raise ImportError('None of the backends could be imported') Store.set_current_backend(selected_backend) + @classmethod + def register_backend_callback(cls, backend, callback): + """Registers a hook which is run when a backend is loaded""" + self._backend_hooks[backend].append(callback) + def save(obj, filename, fmt='auto', backend=None, **kwargs): """