Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding prelaunch hook for kernel instances #218

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 8 additions & 2 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from traitlets.config.application import Application
from traitlets.config.loader import Config
from traitlets import Unicode, Integer, Bool, Dict, List, default
from traitlets import Unicode, Integer, Bool, Dict, List, Any, default

from jupyter_server.services.kernels.kernelmanager import MappingKernelManager
from jupyter_server.services.kernels.handlers import KernelHandler, ZMQChannelsHandler
Expand Down Expand Up @@ -241,6 +241,11 @@ class Voila(Application):
cannot be determined reliably by the Jupyter notebook server (proxified
or containerized setups for example)."""))

prelaunch_hook = Any(default_value=None, allow_none=True,
timkpaine marked this conversation as resolved.
Show resolved Hide resolved
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."""))

@property
def display_url(self):
if self.custom_display_url:
Expand Down Expand Up @@ -487,7 +492,8 @@ def start(self):
'notebook_path': os.path.relpath(self.notebook_path, self.root_dir),
'nbconvert_template_paths': self.nbconvert_template_paths,
'config': self.config,
'voila_configuration': self.voila_configuration
'voila_configuration': self.voila_configuration,
'prelaunch_hook': self.prelaunch_hook
}
))
else:
Expand Down
9 changes: 9 additions & 0 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def initialize(self, **kwargs):
self.nbconvert_template_paths = kwargs.pop('nbconvert_template_paths', [])
self.traitlet_config = kwargs.pop('config', None)
self.voila_configuration = kwargs['voila_configuration']
self.prelaunch_hook = kwargs.get('prelaunch_hook')
# we want to avoid starting multiple kernels due to template mistakes
self.kernel_started = False

Expand Down Expand Up @@ -59,6 +60,14 @@ async def get(self, path=None):
return
self.cwd = os.path.dirname(notebook_path)

if self.prelaunch_hook:
# Allow for preprocessing the notebook.
# Can be used to add auth, do custom formatting/standardization
# of the notebook, raise exceptions, etc
self.prelaunch_hook(self,
notebook=self.notebook,
cwd=self.cwd)

# render notebook to html
resources = {
'base_url': self.base_url,
Expand Down