Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Oct 11, 2024
1 parent 7dfaa05 commit 1faa3d7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
29 changes: 29 additions & 0 deletions tests/app/page_config_hook_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

import pytest


BASE_DIR = os.path.dirname(__file__)


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, "print.ipynb")


@pytest.fixture
def voila_config():
def foo(current_page_config, **kwargs):
current_page_config["foo"] = "my custom config"
return current_page_config

def config(app):
app.voila_configuration.page_config_hook = foo

return config


async def test_prelaunch_hook(http_server_client, base_url):
response = await http_server_client.fetch(base_url)
assert response.code == 200
assert "my custom config" in response.body.decode("utf-8")
37 changes: 35 additions & 2 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

from traitlets import (
Bool,
Callable,
Dict,
Integer,
List,
Expand Down Expand Up @@ -192,8 +193,6 @@ class Voila(Application):
"theme": "VoilaConfiguration.theme",
"classic_tree": "VoilaConfiguration.classic_tree",
"kernel_spec_manager_class": "VoilaConfiguration.kernel_spec_manager_class",
"prelaunch_hook": "VoilaConfiguration.prelaunch_hook", # For backward compatibility
"page_config_hook": "VoilaConfiguration.page_config_hook",
}
if JUPYTER_SERVER_2:
aliases = {**aliases, "token": "Voila.token"}
Expand Down Expand Up @@ -326,6 +325,40 @@ class Voila(Application):
),
)

prelaunch_hook = Callable(
default_value=None,
allow_none=True,
config=True,
help=_(
"""A function that is called prior to the launch of a new kernel instance
when a user visits the voila webpage. Used for custom user authorization
or any other necessary pre-launch functions.
Should be of the form:
def hook(req: tornado.web.RequestHandler,
notebook: nbformat.NotebookNode,
cwd: str)
Although most customizations can leverage templates, if you need access
to the request object (e.g. to inspect cookies for authentication),
or to modify the notebook itself (e.g. to inject some custom structure,
although much of this can be done by interacting with the kernel
in javascript) the prelaunch hook lets you do that.
"""
),
)

@validate("prelaunch_hook")
def _valid_prelaunch_hook(self, proposal):
warn(
"Voila.prelaunch_hook is deprecated, please use VoilaConfiguration.prelaunch_hook instead",
DeprecationWarning,
stacklevel=2,
)
self.voila_configuration.prelaunch_hook = proposal["value"]
return proposal["value"]

if JUPYTER_SERVER_2:
cookie_secret = Bytes(
b"",
Expand Down

0 comments on commit 1faa3d7

Please sign in to comment.