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

Shim to support the notebook extensions #113

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 40 additions & 3 deletions nbclassic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import os
import sys
from ._version import __version__


# Packagers: modify this line if you store the notebook static files elsewhere
DEFAULT_STATIC_FILES_PATH = os.path.join(os.path.dirname(__file__), "static")

# Packagers: modify the next line if you store the notebook template files
# elsewhere

# Include both notebook/ and notebook/templates/. This makes it
# Notebook shim to ensure notebook extensions backwards compatiblity.

try:
from notebook._version import __version__ as notebook_version
except Exception as e:
# Notebook is not available on the platform.
# We shim the complete notebook module.
print('No notebook python package found. Shimming notebook to jupyter_server for notebook extensions backwards compatiblity.')
import jupyter_server
sys.modules["notebook"] = jupyter_server
from jupyter_server.base import handlers
from notebook.base import handlers as notebook_handlers
handlers.IPythonHandler = handlers.JupyterHandler
notebook_handlers.IPythonHandler = handlers.JupyterHandler

if "notebook_version" in locals():
# Notebook is available on the platform.
# We shim based on the notebook version.
if notebook_version < "7":
from .shim_notebook import shim_notebook_6
print('Shimming existing notebook python package < 7 to jupyter_server for notebook extensions backwards compatiblity.')
shim_notebook_6()
else:
from .shim_notebook import shim_notebook_7_and_above
print('Shimming existing notebook python package >= 7 to jupyter_server for notebook extensions backwards compatiblity.')
shim_notebook_7_and_above()


# Sanity check for the notebook shim.

from jupyter_server.base.handlers import IPythonHandler as JupyterServerIPythonHandler
assert JupyterServerIPythonHandler.__name__ == "JupyterHandler"

from notebook.base.handlers import IPythonHandler as NotebookIPythonHandler
assert NotebookIPythonHandler.__name__ == "JupyterHandler"


# Include both nbclassic/ and nbclassic/templates/. This makes it
# possible for users to override a template with a file that inherits from that
# template.
#
Expand Down
96 changes: 96 additions & 0 deletions nbclassic/shim_notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Shim the notebook module for the classic extensions.
"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import sys


def shim_notebook_6():
"""Define in sys.module the needed notebook packages that should be fullfilled by
their corresponding and backwards-compatible jupyter-server packages.

TODO Can we lazy load these loadings?

Note: We could a custom module loader to achieve similar functionality. The
logic thar conditional loading seems to be more complicated than simply
listing by hand the needed subpackages but could avoid latency on server start.

https://docs.python.org/3/library/importlib.html#importlib.abc.Loader

These are the notebook packages we need to shim:

auth
base
bundler <- no, already available in nbclassic
edit <- no, already available in nbclassic
files
gateway
i18n <- no, already available in nbclassic
kernelspecs
nbconvert
notebook <- no, already available in nbclassic
prometheus
services
static <- no, already available in nbclassic
templates <- no, already available in nbclassic
terminal <- no, already available in nbclassic
tests <- no, already available in nbclassic
tree <- no, already available in nbclassic
view
__init__.py <- no, already available in nbclassic
__main__.py <- no, already available in nbclassic
_sysinfo.py <- no, already available in nbclassic
_tz.py
_version.py <- no, already available in nbclassic
config_manager.py <- no, already available in nbclassic
extensions.py <- no, already available in nbclassic
jstest.py <- no, already available in nbclassic
log.py
nbextensions.py <- no, already available in nbclassic
notebookapp.py <- no, already available in nbclassic
serverextensions.py <- no, already available in nbclassic
traittypes.py <- no, already available in nbclassic
transutils.py <- no, already available in nbclassic
utils.py

"""

from jupyter_server import auth
sys.modules["notebook.auth"] = auth
from jupyter_server import base
sys.modules["notebook.base"] = base
from jupyter_server import files
sys.modules["notebook.files"] = files
from jupyter_server import gateway
sys.modules["notebook.gateway"] = gateway
from jupyter_server import kernelspecs
sys.modules["notebook.kernelspecs"] = kernelspecs
from jupyter_server import nbconvert
sys.modules["notebook.nbconvert"] = nbconvert
from jupyter_server import prometheus
sys.modules["notebook.prometheus"] = prometheus
from jupyter_server import services
sys.modules["notebook.services"] = services
from jupyter_server import view
sys.modules["notebook.view"] = view
from jupyter_server import _tz
sys.modules["notebook._tz"] = _tz
from jupyter_server import log
sys.modules["notebook.log"] = log
from jupyter_server import utils
sys.modules["notebook.utils"] = utils

base.handlers.IPythonHandler = base.handlers.JupyterHandler
sys.modules["notebook.base.handlers.IPythonHandler"] = base.handlers.JupyterHandler


def shim_notebook_7_and_above():
"""For now, notebook v7 should be shimmed for now the same way
as notebook v6. This distinction could be useful for later
notebook >=7 evolutions.

TODO Discuss and remove if not needed.
"""
shim_notebook_6()