Skip to content

Commit 7dfaa05

Browse files
committed
Move hooks to VoilaConfiguration
1 parent d8f0cbf commit 7dfaa05

File tree

8 files changed

+70
-91
lines changed

8 files changed

+70
-91
lines changed

docs/customize.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ def page_config_hook_function(current_page_config,**kwargs):
309309
"""Modify the current_page_config"""
310310
return new_page_config
311311

312-
c.Voila.prelaunch_hook = hook_function
313-
c.Voila.page_config_hook = page_config_hook
312+
c.VoilaConfiguration.prelaunch_hook = hook_function
313+
c.VoilaConfiguration.page_config_hook = page_config_hook
314314

315315
```
316316

@@ -374,18 +374,18 @@ from voila.config import VoilaConfiguration
374374
# customize config how you like
375375
config = VoilaConfiguration()
376376

377+
# set the prelaunch hook
378+
config.prelaunch_hook = parameterize_with_papermill
379+
380+
# set the page config hook
381+
config.config_page_hook = page_config_hook
382+
377383
# create a voila instance
378384
app = Voila()
379385

380386
# set the config
381387
app.voila_configuration = config
382388

383-
# set the prelaunch hook
384-
app.prelaunch_hook = parameterize_with_papermill
385-
386-
# set the page config hook
387-
app.config_page_hook = page_config_hook
388-
389389
# launch
390390
app.start()
391391
```

voila/app.py

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171
from traitlets import (
7272
Bool,
73-
Callable,
7473
Dict,
7574
Integer,
7675
List,
@@ -193,6 +192,8 @@ class Voila(Application):
193192
"theme": "VoilaConfiguration.theme",
194193
"classic_tree": "VoilaConfiguration.classic_tree",
195194
"kernel_spec_manager_class": "VoilaConfiguration.kernel_spec_manager_class",
195+
"prelaunch_hook": "VoilaConfiguration.prelaunch_hook", # For backward compatibility
196+
"page_config_hook": "VoilaConfiguration.page_config_hook",
196197
}
197198
if JUPYTER_SERVER_2:
198199
aliases = {**aliases, "token": "Voila.token"}
@@ -325,54 +326,6 @@ class Voila(Application):
325326
),
326327
)
327328

328-
prelaunch_hook = Callable(
329-
default_value=None,
330-
allow_none=True,
331-
config=True,
332-
help=_(
333-
"""A function that is called prior to the launch of a new kernel instance
334-
when a user visits the voila webpage. Used for custom user authorization
335-
or any other necessary pre-launch functions.
336-
337-
Should be of the form:
338-
339-
def hook(req: tornado.web.RequestHandler,
340-
notebook: nbformat.NotebookNode,
341-
cwd: str)
342-
343-
Although most customizations can leverage templates, if you need access
344-
to the request object (e.g. to inspect cookies for authentication),
345-
or to modify the notebook itself (e.g. to inject some custom structure,
346-
although much of this can be done by interacting with the kernel
347-
in javascript) the prelaunch hook lets you do that.
348-
"""
349-
),
350-
)
351-
352-
page_config_hook = Callable(
353-
default_value=None,
354-
allow_none=True,
355-
config=True,
356-
help=_(
357-
"""A function that is called to modify the page config for a given notebook.
358-
Should be of the form:
359-
360-
def page_config_hook(
361-
current_page_config: Dict[str, Any],
362-
base_url: str,
363-
settings: Dict[str, Any],
364-
log: Logger,
365-
voila_configuration: VoilaConfiguration,
366-
notebook_path: str
367-
) -> Dict[str, Any]:
368-
369-
The hook receives the default page_config dictionary and all its parameters, it should
370-
return a dictionary that will be passed to the template as the `page_config` variable
371-
and the NotebookRenderer. This can be used to pass custom configuration.
372-
"""
373-
),
374-
)
375-
376329
if JUPYTER_SERVER_2:
377330
cookie_secret = Bytes(
378331
b"",
@@ -632,14 +585,14 @@ def init_settings(self) -> Dict:
632585
preheat_kernel: bool = self.voila_configuration.preheat_kernel
633586
pool_size: int = self.voila_configuration.default_pool_size
634587

635-
if preheat_kernel and self.prelaunch_hook:
588+
if preheat_kernel and self.voila_configuration.prelaunch_hook:
636589
raise Exception("`preheat_kernel` and `prelaunch_hook` are incompatible")
637590

638591
kernel_manager_class = voila_kernel_manager_factory(
639592
self.voila_configuration.multi_kernel_manager_class,
640593
preheat_kernel,
641594
pool_size,
642-
page_config_hook=self.page_config_hook,
595+
page_config_hook=self.voila_configuration.page_config_hook,
643596
)
644597
self.kernel_manager = kernel_manager_class(
645598
parent=self,
@@ -814,8 +767,6 @@ def init_handlers(self) -> List:
814767
"template_paths": self.template_paths,
815768
"config": self.config,
816769
"voila_configuration": self.voila_configuration,
817-
"prelaunch_hook": self.prelaunch_hook,
818-
"page_config_hook": self.page_config_hook,
819770
},
820771
)
821772
)
@@ -826,18 +777,12 @@ def init_handlers(self) -> List:
826777
(
827778
self.server_url,
828779
TornadoVoilaTreeHandler,
829-
{
830-
"voila_configuration": self.voila_configuration,
831-
"page_config_hook": self.page_config_hook,
832-
},
780+
{"voila_configuration": self.voila_configuration},
833781
),
834782
(
835783
url_path_join(self.server_url, r"/voila/tree" + path_regex),
836784
TornadoVoilaTreeHandler,
837-
{
838-
"voila_configuration": self.voila_configuration,
839-
"page_config_hook": self.page_config_hook,
840-
},
785+
{"voila_configuration": self.voila_configuration},
841786
),
842787
(
843788
url_path_join(self.server_url, r"/voila/render/(.*)"),
@@ -846,8 +791,6 @@ def init_handlers(self) -> List:
846791
"template_paths": self.template_paths,
847792
"config": self.config,
848793
"voila_configuration": self.voila_configuration,
849-
"prelaunch_hook": self.prelaunch_hook,
850-
"page_config_hook": self.page_config_hook,
851794
},
852795
),
853796
# On serving a directory, expose the content handler.

voila/configuration.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#############################################################################
99

1010
import traitlets.config
11-
from traitlets import Bool, Dict, Enum, Int, List, Type, Unicode, validate
11+
from traitlets import Bool, Callable, Dict, Enum, Int, List, Type, Unicode, validate
1212

1313
from warnings import warn
1414

@@ -217,3 +217,47 @@ def _valid_file_blacklist(self, proposal):
217217
config=True,
218218
help="""Whether or not voila should attempt to fix and resolve a notebooks kernelspec metadata""",
219219
)
220+
221+
prelaunch_hook = Callable(
222+
default_value=None,
223+
allow_none=True,
224+
config=True,
225+
help="""A function that is called prior to the launch of a new kernel instance
226+
when a user visits the voila webpage. Used for custom user authorization
227+
or any other necessary pre-launch functions.
228+
229+
Should be of the form:
230+
231+
def hook(req: tornado.web.RequestHandler,
232+
notebook: nbformat.NotebookNode,
233+
cwd: str)
234+
235+
Although most customizations can leverage templates, if you need access
236+
to the request object (e.g. to inspect cookies for authentication),
237+
or to modify the notebook itself (e.g. to inject some custom structure,
238+
although much of this can be done by interacting with the kernel
239+
in javascript) the prelaunch hook lets you do that.
240+
""",
241+
)
242+
243+
page_config_hook = Callable(
244+
default_value=None,
245+
allow_none=True,
246+
config=True,
247+
help="""A function that is called to modify the page config for a given notebook.
248+
Should be of the form:
249+
250+
def page_config_hook(
251+
current_page_config: Dict[str, Any],
252+
base_url: str,
253+
settings: Dict[str, Any],
254+
log: Logger,
255+
voila_configuration: VoilaConfiguration,
256+
notebook_path: str
257+
) -> Dict[str, Any]:
258+
259+
The hook receives the default page_config dictionary and all its parameters, it should
260+
return a dictionary that will be passed to the template as the `page_config` variable
261+
and the NotebookRenderer. This can be used to pass custom configuration.
262+
""",
263+
)

voila/handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def initialize(self, **kwargs):
7272
self.template_paths = kwargs.pop("template_paths", [])
7373
self.traitlet_config = kwargs.pop("config", None)
7474
self.voila_configuration: VoilaConfiguration = kwargs["voila_configuration"]
75-
self.prelaunch_hook = kwargs.get("prelaunch_hook", None)
76-
self.page_config_hook = kwargs.get("page_config_hook", None)
75+
self.prelaunch_hook = self.voila_configuration.prelaunch_hook
76+
self.page_config_hook = self.voila_configuration.page_config_hook
7777

7878
# we want to avoid starting multiple kernels due to template mistakes
7979
self.kernel_started = False

voila/notebook_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, **kwargs):
4545
self.config_manager = kwargs.get("config_manager")
4646
self.contents_manager = kwargs.get("contents_manager")
4747
self.kernel_spec_manager = kwargs.get("kernel_spec_manager")
48-
self.prelaunch_hook = kwargs.get("prelaunch_hook")
48+
self.prelaunch_hook = self.voila_configuration.prelaunch_hook
4949
self.base_url = kwargs.get("base_url")
5050
self.page_config = deepcopy(kwargs.get("page_config"))
5151
self.default_kernel_name = "python3"

voila/server_extension.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def _load_jupyter_server_extension(server_app: ServerApp):
8181
voila_configuration = VoilaConfiguration(
8282
parent=server_app, config=load_config_file()
8383
)
84-
page_config_hook = voila_configuration.config.Voila.get("page_config_hook", None)
85-
prelaunch_hook = voila_configuration.config.Voila.get("prelaunch_hook", None)
8684

8785
template_name = voila_configuration.template
8886
template_paths = collect_template_paths(["voila", "nbconvert"], template_name)
@@ -104,6 +102,8 @@ def _load_jupyter_server_extension(server_app: ServerApp):
104102
host_pattern = ".*$"
105103
base_url = url_path_join(web_app.settings["base_url"])
106104

105+
tree_handler_conf = {"voila_configuration": voila_configuration}
106+
107107
themes_dir = pjoin(get_data_dir(), "themes")
108108
web_app.add_handlers(
109109
host_pattern,
@@ -115,25 +115,17 @@ def _load_jupyter_server_extension(server_app: ServerApp):
115115
"config": server_app.config,
116116
"template_paths": template_paths,
117117
"voila_configuration": voila_configuration,
118-
"prelaunch_hook": prelaunch_hook,
119-
"page_config_hook": page_config_hook,
120118
},
121119
),
122120
(
123121
url_path_join(base_url, "/voila"),
124122
TornadoVoilaTreeHandler,
125-
{
126-
"voila_configuration": voila_configuration,
127-
"page_config_hook": page_config_hook,
128-
},
123+
tree_handler_conf,
129124
),
130125
(
131126
url_path_join(base_url, "/voila/tree" + path_regex),
132127
TornadoVoilaTreeHandler,
133-
{
134-
"voila_configuration": voila_configuration,
135-
"page_config_hook": page_config_hook,
136-
},
128+
tree_handler_conf,
137129
),
138130
(
139131
url_path_join(base_url, "/voila/templates/(.*)"),
@@ -170,7 +162,7 @@ def _load_jupyter_server_extension(server_app: ServerApp):
170162
(
171163
url_path_join(base_url, r"/voila/api/contents%s" % path_regex),
172164
VoilaContentsHandler,
173-
{"voila_configuration": voila_configuration},
165+
tree_handler_conf,
174166
),
175167
],
176168
)

voila/tornado/treehandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class TornadoVoilaTreeHandler(VoilaTreeHandler):
2424
def initialize(self, **kwargs):
2525
super().initialize(**kwargs)
26-
self.page_config_hook = kwargs.get("page_config_hook", None)
26+
self.page_config_hook = self.voila_configuration.page_config_hook
2727

2828
@web.authenticated
2929
async def get(self, path=""):

voila/voila_kernel_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import os
1313
import re
1414
from pathlib import Path
15-
from typing import Awaitable, Callable
15+
from typing import Awaitable, Callable, Optional
1616
from typing import Dict as TypeDict
1717
from typing import List as TypeList
1818
from typing import Tuple, Type, TypeVar, Union
@@ -37,7 +37,7 @@ def voila_kernel_manager_factory(
3737
base_class: Type[T],
3838
preheat_kernel: bool,
3939
default_pool_size: int,
40-
page_config_hook: Callable = None,
40+
page_config_hook: Optional[Callable] = None,
4141
) -> T:
4242
"""
4343
Decorator used to make a normal kernel manager compatible with pre-heated

0 commit comments

Comments
 (0)