Skip to content

Commit

Permalink
Merge pull request #63 from Zsailer/notebook-static-paths
Browse files Browse the repository at this point in the history
Expose classic notebook's static assets from their original endpoints
  • Loading branch information
Zsailer authored Oct 8, 2021
2 parents 0d6fd44 + 1ace067 commit eac6680
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 163 deletions.
85 changes: 40 additions & 45 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Testing nbclassic
on:
push:
branches:
- master
- master
pull_request:
branches: '*'
branches: "*"

jobs:
build:
Expand All @@ -14,48 +14,43 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu, macos, windows]
python-version: ['3.6', '3.7', '3.8', '3.9', 'pypy3']
python-version: ["3.6", "3.7", "3.8", "3.9", "pypy-3.7-v7.3.5"]
exclude:
- os: windows
python-version: pypy3
- os: windows
python-version: pypy-3.7-v7.3.5
steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Upgrade pip, etc.
run: |
python -m pip install --user --upgrade pip setuptools wheel
- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache pip
uses: actions/cache@v1
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('setup.py') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
${{ runner.os }}-pip-
- name: Install pip dependencies
run: |
pip install -v -e ".[test]" pytest-cov
- name: Check pip environment
run: |
pip freeze
pip check
# - name: Install Jupyter Server from source
# run: |
# cd ..
# git clone https://github.com/jupyter/jupyter_server.git
# cd jupyter_server
# pip install -e .
# cd ../nbclassic
- name: Run the help command
run: |
jupyter nbclassic -h
- name: Test with pytest
run: |
pytest -vv --cov nbclassic --cov-report term-missing:skip-covered
- name: Checkout
uses: actions/checkout@v2
- name: Install Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: "x64"
- name: Upgrade packaging dependencies
run: |
pip install --upgrade pip setuptools wheel --user
- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache pip
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('setup.cfg') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
${{ runner.os }}-pip-
- name: Install pip dependencies
run: |
pip install -v -e ".[test]" pytest-cov
- name: Check pip environment
run: |
pip freeze
pip check
- name: Run the help command
run: |
jupyter nbclassic -h
- name: Test with pytest
run: |
pytest -vv --cov nbclassic --cov-report term-missing:skip-covered
51 changes: 50 additions & 1 deletion nbclassic/nbserver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
"""
This module contains a Jupyter Server extension that attempts to
make classic server and notebook extensions work in the new server.
Unfortunately, you'll notice that requires some major monkey-patching.
The goal is that this extension will only be used as a temporary
patch to transition extension authors from classic notebook server to jupyter_server.
"""
import os
import types
import inspect
from functools import wraps
from jupyter_core.paths import jupyter_config_path
from jupyter_server.services.config.manager import ConfigManager
from traitlets.traitlets import is_trait

import notebook

import jupyter_server
from jupyter_server.services.config.manager import ConfigManager
from .traits import NotebookAppTraits


Expand Down Expand Up @@ -121,6 +133,43 @@ def sorted_extensions(self):
)
manager.link_extension(name)

# Monkey-patch Jupyter Server's template and static path list to include
# the classic notebooks template folder. Since there are some
# redundancy in the template names between these two packages,
# this patch makes an opinionated choice to use the templates
# in the classic notebook first. This should be a *safe* choice
# because the Jupyter Server templates are simpler, more
# stripped down versions of the classic notebook templates. If
# the templates in Jupyter server eventually change, we may
# need to revisit this patch.
def template_file_path(self):
"""return extra paths + the default locations"""
return self.extra_template_paths + \
notebook.DEFAULT_TEMPLATE_PATH_LIST + \
jupyter_server.DEFAULT_TEMPLATE_PATH_LIST

serverapp.__class__.template_file_path = property(template_file_path)

def static_file_path_jupyter_server(self):
"""return extra paths + the default location"""
return self.extra_static_paths + [jupyter_server.DEFAULT_STATIC_FILES_PATH, notebook.DEFAULT_STATIC_FILES_PATH]

serverapp.__class__.static_file_path = property(
static_file_path_jupyter_server)

def static_file_path_nbclassic(self):
"""return extra paths + the default location"""
# NBExtensions look for classic notebook static files under the `/static/notebook/...`
# URL. Unfortunately, this conflicts with nbclassic's new static endpoints which are
# prefixed with `/static/notebooks`, and therefore, serves these files under
# `/static/notebook/notebooks/...`. This monkey-patch places a new file-finder path
# to nbclassic's static file handlers that drops the extra "notebook".
return self.extra_static_paths + \
[os.path.join(notebook.DEFAULT_STATIC_FILES_PATH,
"notebook"), notebook.DEFAULT_STATIC_FILES_PATH]

nbapp.__class__.static_file_path = property(static_file_path_nbclassic)


def _load_jupyter_server_extension(serverapp):
# Patch the config service manager to find the
Expand Down
48 changes: 22 additions & 26 deletions nbclassic/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from collections import namedtuple
import os
from tornado import web, gen
HTTPError = web.HTTPError

from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.transutils import _i18n
from jupyter_server.utils import (
ensure_async
)
from jupyter_server.base.handlers import path_regex, FilesRedirectHandler
from jupyter_server.extension.handler import (
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin
)
from jupyter_server.base.handlers import path_regex, FilesRedirectHandler
from jupyter_server.utils import (
url_path_join,
url_escape,
ensure_async
)
from jupyter_server.transutils import _i18n
from jupyter_server.base.handlers import JupyterHandler
from collections import namedtuple
import os
from tornado import web, gen
HTTPError = web.HTTPError


def get_frontend_exporters():
Expand Down Expand Up @@ -53,7 +50,7 @@ def get_frontend_exporters():
# Ensure export_from_notebook is explicitly defined & not inherited
if ux_name is not None and ux_name != super_uxname:
display = _i18n('{} ({})'.format(ux_name,
exporter_instance.file_extension))
exporter_instance.file_extension))
frontend_exporters.append(ExporterInfo(name, display))

# Ensure default_exporters are in frontend_exporters if not already
Expand All @@ -77,7 +74,6 @@ def get_frontend_exporters():

class NotebookHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):


@web.authenticated
@gen.coroutine
def get(self, path):
Expand All @@ -100,18 +96,18 @@ def get(self, path):
yield FilesRedirectHandler.redirect_to_files(self, path)
name = path.rsplit('/', 1)[-1]
self.write(self.render_template('notebook.html',
notebook_path=path,
notebook_name=name,
kill_kernel=False,
mathjax_url=self.mathjax_url,
mathjax_config=self.mathjax_config,
get_frontend_exporters=get_frontend_exporters
)
)

#-----------------------------------------------------------------------------
notebook_path=path,
notebook_name=name,
kill_kernel=False,
mathjax_url=self.mathjax_url,
mathjax_config=self.mathjax_config,
get_frontend_exporters=get_frontend_exporters
)
)

# -----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------


default_handlers = [
Expand Down
Loading

0 comments on commit eac6680

Please sign in to comment.