Skip to content

Commit

Permalink
make page_config_hook optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dfguerrerom committed Oct 10, 2024
1 parent 6f07177 commit 6e1d5eb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
31 changes: 19 additions & 12 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,25 @@ def hook(req: tornado.web.RequestHandler,
),
)

get_page_config_hook = Callable(
default_value=lambda page_config, **kwargs: page_config,
page_config_hook = Callable(
default_value=None,
allow_none=True,
config=True,
help=_(
"""A function that is called to modify the page config for a given notebook.
Should be of the form:
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.
def page_config_hook(
base_url: str,
settings: Dict[str, Any],
log: Logger,
voila_configuration: VoilaConfiguration,
notebook_path: str
) -> Dict[str, Any]:
The hook receives the default page_config dictionary and all its parameters, it 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 @@ -631,7 +638,7 @@ def init_settings(self) -> Dict:
self.voila_configuration.multi_kernel_manager_class,
preheat_kernel,
pool_size,
get_page_config_hook=self.get_page_config_hook,
page_config_hook=self.page_config_hook,
)
self.kernel_manager = kernel_manager_class(
parent=self,
Expand Down Expand Up @@ -807,7 +814,7 @@ def init_handlers(self) -> List:
"config": self.config,
"voila_configuration": self.voila_configuration,
"prelaunch_hook": self.prelaunch_hook,
"get_page_config_hook": self.get_page_config_hook,
"page_config_hook": self.page_config_hook,
},
)
)
Expand All @@ -820,15 +827,15 @@ def init_handlers(self) -> List:
TornadoVoilaTreeHandler,
{
"voila_configuration": self.voila_configuration,
"get_page_config_hook": self.get_page_config_hook,
"page_config_hook": self.page_config_hook,
},
),
(
url_path_join(self.server_url, r"/voila/tree" + path_regex),
TornadoVoilaTreeHandler,
{
"voila_configuration": self.voila_configuration,
"get_page_config_hook": self.get_page_config_hook,
"page_config_hook": self.page_config_hook,
},
),
(
Expand All @@ -839,7 +846,7 @@ def init_handlers(self) -> List:
"config": self.config,
"voila_configuration": self.voila_configuration,
"prelaunch_hook": self.prelaunch_hook,
"get_page_config_hook": self.get_page_config_hook,
"page_config_hook": self.page_config_hook,
},
),
# On serving a directory, expose the content handler.
Expand Down
18 changes: 10 additions & 8 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ 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_hook = kwargs.get(
"get_page_config_hook", lambda page_config, **kwargs: page_config
)
self.page_config_hook = kwargs.get("page_config_hook", None)

# we want to avoid starting multiple kernels due to template mistakes
self.kernel_started = False
Expand Down Expand Up @@ -197,11 +195,15 @@ async def get_generator(self, path=None):
"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,
)

page_config = get_page_config(**page_config_kwargs)

if self.page_config_hook:
page_config = self.page_config_hook(
page_config,
**page_config_kwargs,
notebook_path=notebook_path,
)

gen = NotebookRenderer(
request_handler=self,
Expand Down
19 changes: 11 additions & 8 deletions voila/tornado/treehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
class TornadoVoilaTreeHandler(VoilaTreeHandler):
def initialize(self, **kwargs):
super().initialize(**kwargs)
self.get_page_config_hook = kwargs.get(
"get_page_config_hook", lambda page_config, **kwargs: page_config
)
self.page_config_hook = kwargs.get("page_config_hook", None)

@web.authenticated
async def get(self, path=""):
Expand Down Expand Up @@ -70,11 +68,16 @@ def allowed_content(content):
"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 = get_page_config(**page_config_kwargs)

if self.page_config_hook:
self.page_config_hook(
page_config,
**page_config_kwargs,
notebook_path=path,
)

page_config["jupyterLabTheme"] = theme_arg
page_config["frontend"] = "voila"
page_config["query"] = self.request.query
Expand Down
2 changes: 1 addition & 1 deletion voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_page_config(
settings: Dict[str, Any],
log: Logger,
voila_configuration: VoilaConfiguration,
):
) -> Dict[str, Any]:
"""Get the page configuration for Voila.
Args:
Expand Down
18 changes: 11 additions & 7 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 = lambda page_config, **kwargs: page_config,
page_config_hook: Callable = None,
) -> T:
"""
Decorator used to make a normal kernel manager compatible with pre-heated
Expand All @@ -53,7 +53,7 @@ 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): Hook to modify the default page config.
- page_config_hook (Callable, optional): Hook to modify the default page config.
Returns:
T: Decorated class
Expand Down Expand Up @@ -400,11 +400,15 @@ def _notebook_renderer_factory(
"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,
)

page_config = get_page_config(**page_config_kwargs)

if page_config_hook:
page_config = page_config_hook(
page_config,
**page_config_kwargs,
notebook_path=notebook_path,
)

return NotebookRenderer(
voila_configuration=voila_configuration,
Expand Down

0 comments on commit 6e1d5eb

Please sign in to comment.