Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
),
)
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 18 additions & 8 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down
18 changes: 12 additions & 6 deletions voila/tornado/treehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def get_page_config(
settings: Dict[str, Any],
log: Logger,
voila_configuration: VoilaConfiguration,
**kwargs,
):
"""Get the page configuration for Voila.

Expand All @@ -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__,
Expand Down
27 changes: 16 additions & 11 deletions voila/voila_kernel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down