diff --git a/voila/app.py b/voila/app.py index c32734e34..89965d600 100644 --- a/voila/app.py +++ b/voila/app.py @@ -331,24 +331,17 @@ def hook(req: tornado.web.RequestHandler, ) get_page_config_hook = Callable( - default_value=None, - allow_none=True, + default_value=lambda page_config, **kwargs: page_config, config=True, help=_( - """A function that is called to get the page config for a given notebook. + """A function that is called to modify the page config for a given notebook. Should be of the form: - def hook_fn( - base_url: str, - settings: Dict[str, Any], - log: Logger, - voila_configuration: VoilaConfiguration, - **kwargs - ) -> Dict - - The hook should return a dictionary that will be passed to the template - as the `page_config` variable and the NotebookRenderer. This can be used to pass custom - configuration. + def hook_fn(page_config, **kwargs) -> Dict + + The hook receives the default page_config dictionary and should return a dictionary + that will be passed to the template as the `page_config` variable and the + NotebookRenderer. This can be used to pass custom configuration. """ ), ) @@ -792,7 +785,14 @@ def init_handlers(self) -> List: self.log.debug("serving directory: %r", self.root_dir) handlers.extend( [ - (self.server_url, TornadoVoilaTreeHandler, tree_handler_conf), + ( + self.server_url, + TornadoVoilaTreeHandler, + { + "voila_configuration": self.voila_configuration, + "get_page_config_hook": self.get_page_config_hook, + }, + ), ( url_path_join(self.server_url, r"/voila/tree" + path_regex), TornadoVoilaTreeHandler, diff --git a/voila/handler.py b/voila/handler.py index be97818fc..c9e9e049d 100644 --- a/voila/handler.py +++ b/voila/handler.py @@ -73,7 +73,10 @@ def initialize(self, **kwargs): self.traitlet_config = kwargs.pop("config", None) self.voila_configuration: VoilaConfiguration = kwargs["voila_configuration"] self.prelaunch_hook = kwargs.get("prelaunch_hook", None) - self.get_page_config = kwargs.get("get_page_config_hook") or get_page_config + self.get_page_config_hook = kwargs.get( + "get_page_config_hook", lambda page_config, **kwargs: page_config + ) + # we want to avoid starting multiple kernels due to template mistakes self.kernel_started = False @@ -187,6 +190,19 @@ async def get_generator(self, path=None): return mathjax_config = self.settings.get("mathjax_config") mathjax_url = self.settings.get("mathjax_url") + + page_config_kwargs = { + "base_url": self.base_url, + "settings": self.settings, + "log": self.log, + "voila_configuration": self.voila_configuration, + } + page_config = self.get_page_config_hook( + get_page_config(**page_config_kwargs), + **page_config_kwargs, + notebook_path=notebook_path, + ) + gen = NotebookRenderer( request_handler=self, voila_configuration=self.voila_configuration, @@ -198,13 +214,7 @@ async def get_generator(self, path=None): base_url=self.base_url, kernel_spec_manager=self.kernel_spec_manager, prelaunch_hook=self.prelaunch_hook, - page_config=self.get_page_config( - base_url=self.base_url, - settings=self.settings, - log=self.log, - voila_configuration=self.voila_configuration, - notebook_path=notebook_path, - ), + page_config=page_config, mathjax_config=mathjax_config, mathjax_url=mathjax_url, ) diff --git a/voila/tornado/treehandler.py b/voila/tornado/treehandler.py index df6b10603..aa609f975 100644 --- a/voila/tornado/treehandler.py +++ b/voila/tornado/treehandler.py @@ -23,7 +23,9 @@ class TornadoVoilaTreeHandler(VoilaTreeHandler): def initialize(self, **kwargs): super().initialize(**kwargs) - self.get_page_config = kwargs.get("get_page_config_hook") or get_page_config + self.get_page_config_hook = kwargs.get( + "get_page_config_hook", lambda page_config, **kwargs: page_config + ) @web.authenticated async def get(self, path=""): @@ -62,11 +64,15 @@ def allowed_content(content): theme_arg = self.validate_theme(theme_arg, classic_tree) - page_config = self.get_page_config( - base_url=self.base_url, - settings=self.settings, - log=self.log, - voila_configuration=self.voila_configuration, + page_config_kwargs = { + "base_url": self.base_url, + "settings": self.settings, + "log": self.log, + "voila_configuration": self.voila_configuration, + } + page_config = self.get_page_config_hook( + get_page_config(**page_config_kwargs), + **page_config_kwargs, notebook_path=path, ) page_config["jupyterLabTheme"] = theme_arg diff --git a/voila/utils.py b/voila/utils.py index 0b04ac9e5..662c94136 100644 --- a/voila/utils.py +++ b/voila/utils.py @@ -95,7 +95,6 @@ def get_page_config( settings: Dict[str, Any], log: Logger, voila_configuration: VoilaConfiguration, - **kwargs, ): """Get the page configuration for Voila. @@ -104,7 +103,6 @@ def get_page_config( settings (Dict[str, Any]): The settings of the Voila application. log (Logger): The logger instance. voila_configuration (VoilaConfiguration): The Voila configuration instance. - **kwargs: additional keyword arguments that can be used when get_page_config_hook is set. """ page_config = { "appVersion": __version__, diff --git a/voila/voila_kernel_manager.py b/voila/voila_kernel_manager.py index 2f5141fd1..4e48adcf6 100644 --- a/voila/voila_kernel_manager.py +++ b/voila/voila_kernel_manager.py @@ -37,7 +37,7 @@ def voila_kernel_manager_factory( base_class: Type[T], preheat_kernel: bool, default_pool_size: int, - get_page_config_hook: Callable = None, + get_page_config_hook: Callable = lambda page_config, **kwargs: page_config, ) -> T: """ Decorator used to make a normal kernel manager compatible with pre-heated @@ -53,14 +53,12 @@ def voila_kernel_manager_factory( - preheat_kernel (Bool): Flag to decorate the input class - default_pool_size (int): Size of pre-heated kernel pool for each notebook. Zero or negative number means disabled - - get_page_config_hook (Callable, optional): Hook to get the page config. + - get_page_config_hook (Callable): Hook to modify the default page config. Returns: T: Decorated class """ - get_page_config_fn = get_page_config_hook or get_page_config - if not preheat_kernel: class NormalKernelManager(base_class): @@ -395,6 +393,19 @@ def _notebook_renderer_factory( settings = self.parent.app.settings mathjax_config = settings.get("mathjax_config") mathjax_url = settings.get("mathjax_url") + + page_config_kwargs = { + "base_url": self.parent.base_url, + "settings": self.parent.app.settings, + "log": self.parent.log, + "voila_configuration": voila_configuration, + } + page_config = get_page_config_hook( + get_page_config(**page_config_kwargs), + **page_config_kwargs, + notebook_path=notebook_path, + ) + return NotebookRenderer( voila_configuration=voila_configuration, traitlet_config=self.parent.config, @@ -404,13 +415,7 @@ def _notebook_renderer_factory( contents_manager=self.parent.contents_manager, base_url=self.parent.base_url, kernel_spec_manager=self.parent.kernel_spec_manager, - page_config=get_page_config_fn( - base_url=self.parent.base_url, - settings=self.parent.app.settings, - log=self.parent.log, - voila_configuration=voila_configuration, - notebook_path=notebook_path, - ), + page_config=page_config, mathjax_config=mathjax_config, mathjax_url=mathjax_url, )