Skip to content

Instantly share code, notes, and snippets.

@MMesch
Created May 28, 2018 09:41
Show Gist options
  • Save MMesch/b2c5ad0e78937f4ed9aa0bd3d82bb386 to your computer and use it in GitHub Desktop.
Save MMesch/b2c5ad0e78937f4ed9aa0bd3d82bb386 to your computer and use it in GitHub Desktop.
minimal custom personality for the Jupyter kernel gateway
# Modified from:
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
"""
Custom personality for the jupyter kernel gateway.
This personality adds a custom handler under:
localhost:xxxx/MyHandler
To start the kernel gateway with the custom personality, execute
in the same folder:
PYTHONPATH=`pwd` jupyter kernelgateway --api mypersonality
Test the default handlers with:
curl localhost:8888/api
and the custom handler with:
curl localhost:8888/myhandler
"""
import tornado
import kernel_gateway as kg
from kernel_gateway.services.kernels.pool import KernelPool
from notebook.utils import url_path_join
from traitlets.config.configurable import LoggingConfigurable
class MyPersonality(LoggingConfigurable):
"""Custom Jupyter personality."""
def init_configurables(self):
self.kernel_pool = KernelPool(self.parent.prespawn_count,
self.parent.kernel_manager)
def create_request_handlers(self):
"""Create default Jupyter handlers."""
handlers = []
handlers_uri_and_class = (
[('/myhandler', MyHandler)] +
kg.base.handlers.default_handlers +
kg.services.kernels.handlers.default_handlers +
kg.services.kernelspecs.handlers.default_handlers +
kg.services.sessions.handlers.default_handlers)
for handler in handlers_uri_and_class:
pattern = url_path_join('/', self.parent.base_url, handler[0])
# Some handlers take args, so retain those in addition to the
# handler class ref
new_handler = tuple([pattern] + list(handler[1:]))
handlers.append(new_handler)
return handlers
def should_seed_cell(self, code):
"""Seed all code cells."""
return True
def shutdown(self):
"""Stop all kernels in the pool."""
self.kernel_pool.shutdown()
class MyHandler(tornado.web.RequestHandler):
def get(self):
self.write('\n called MyHandler \n')
self.write('\n' + self.request.path + '\n')
def create_personality(*args, **kwargs):
return MyPersonality(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment