Skip to content

Commit 47f7499

Browse files
committed
Add test
1 parent 7dfaa05 commit 47f7499

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

docs/customize.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Voila provides hooks that allow you to customize its behavior to fit your specif
233233
Currently, Voila supports the following hooks:
234234

235235
- prelaunch_hook: Access and modify the Tornado request and notebook before execution.
236-
- config_page_hook: Customize the page_config object, which controls the Voila frontend configuration.
236+
- page_config_hook: Customize the page_config object, which controls the Voila frontend configuration.
237237

238238
#### Accessing the tornado request (`prelaunch-hook`)
239239

@@ -261,7 +261,7 @@ def prelaunch_hook(req: tornado.web.RequestHandler,
261261

262262
#### Customize the page config object (`page_config_hook`)
263263

264-
The config_page_hook allows you to customize the page_config object, which controls various aspects of the Voila frontend. This is useful when you need to modify frontend settings such as the URLs for static assets or other configuration parameters.
264+
The page_config_hook allows you to customize the page_config object, which controls various aspects of the Voila frontend. This is useful when you need to modify frontend settings such as the URLs for static assets or other configuration parameters.
265265

266266
By default, Voila uses the following page_config:
267267

@@ -305,7 +305,7 @@ def prelaunch_hook_function(req, notebook, cwd):
305305
"""Do your stuffs here"""
306306
return notebook
307307

308-
def page_config_hook_function(current_page_config,**kwargs):
308+
def page_config_hook_function(current_page_config, **kwargs):
309309
"""Modify the current_page_config"""
310310
return new_page_config
311311

@@ -378,7 +378,7 @@ config = VoilaConfiguration()
378378
config.prelaunch_hook = parameterize_with_papermill
379379

380380
# set the page config hook
381-
config.config_page_hook = page_config_hook
381+
config.page_config_hook = page_config_hook
382382

383383
# create a voila instance
384384
app = Voila()

tests/app/page_config_hook_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
import pytest
4+
5+
6+
BASE_DIR = os.path.dirname(__file__)
7+
8+
9+
@pytest.fixture
10+
def voila_notebook(notebook_directory):
11+
return os.path.join(notebook_directory, "print.ipynb")
12+
13+
14+
@pytest.fixture
15+
def voila_config():
16+
def foo(current_page_config, **kwargs):
17+
current_page_config["foo"] = "my custom config"
18+
return current_page_config
19+
20+
def config(app):
21+
app.voila_configuration.page_config_hook = foo
22+
23+
return config
24+
25+
26+
async def test_prelaunch_hook(http_server_client, base_url):
27+
response = await http_server_client.fetch(base_url)
28+
assert response.code == 200
29+
assert "my custom config" in response.body.decode("utf-8")

voila/app.py

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

7171
from traitlets import (
7272
Bool,
73+
Callable,
7374
Dict,
7475
Integer,
7576
List,
@@ -192,8 +193,6 @@ class Voila(Application):
192193
"theme": "VoilaConfiguration.theme",
193194
"classic_tree": "VoilaConfiguration.classic_tree",
194195
"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",
197196
}
198197
if JUPYTER_SERVER_2:
199198
aliases = {**aliases, "token": "Voila.token"}
@@ -326,6 +325,40 @@ class Voila(Application):
326325
),
327326
)
328327

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+
@validate("prelaunch_hook")
353+
def _valid_prelaunch_hook(self, proposal):
354+
warn(
355+
"Voila.prelaunch_hook is deprecated, please use VoilaConfiguration.prelaunch_hook instead",
356+
DeprecationWarning,
357+
stacklevel=2,
358+
)
359+
self.voila_configuration.prelaunch_hook = proposal["value"]
360+
return proposal["value"]
361+
329362
if JUPYTER_SERVER_2:
330363
cookie_secret = Bytes(
331364
b"",

0 commit comments

Comments
 (0)