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

Handlers under nbclassic if notebook 7 is found #141

Merged
merged 10 commits into from
Aug 30, 2022
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

NBClassic runs the [Jupyter NbClassic](https://github.com/jupyter/nbclassic) frontend on the Jupyter Server backend.

This project prepares for a future where JupyterLab and other frontends switch to [Jupyter Server](https://github.com/jupyter/jupyter_server/) for their Python Web application backend. Using this package, users can launch Jupyter Notebook, JupyterLab and other frontends side-by-side on top of the new Python server backend.
This project prepares for a future where JupyterLab and other frontends switch to [Jupyter Server](https://github.com/jupyter/jupyter_server/) for their Python Web application backend. Using this package, users can launch Jupyter NbClassic, JupyterLab and other frontends side-by-side on top of the new Python server backend.

## Basic Usage

Expand Down
10 changes: 9 additions & 1 deletion nbclassic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
DEFAULT_STATIC_FILES_PATH = os.path.join(os.path.dirname(__file__), "static")


NOTEBOOK_V7_DETECTED = False

# Notebook shim to ensure notebook extensions backwards compatiblity.

try:
from notebook import version_info as notebook_version_info
from notebook._version import version_info as notebook_version_info
except Exception:
notebook_version_info = None
# No notebook python package found.
Expand All @@ -27,6 +29,7 @@
# Notebook is available on the platform.
# We shim based on the notebook version.
if notebook_version_info >= (7,):
NOTEBOOK_V7_DETECTED = True
from .shim_notebook import shim_notebook
# Shimming existing notebook python package > 6 to jupyter_server.
# For notebook extensions backwards compatibility.
Expand All @@ -52,6 +55,11 @@
]


def url_prefix_notebook():
if NOTEBOOK_V7_DETECTED:
return "/nbclassic"
return ""

def _jupyter_server_extension_paths():
# Locally import to avoid install errors.
from .notebookapp import NotebookApp
Expand Down
7 changes: 5 additions & 2 deletions nbclassic/bundler/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import inspect
import concurrent.futures

from nbclassic import url_prefix_notebook

from traitlets.utils.importstring import import_item
from tornado import web, gen

from jupyter_server.utils import url2path
from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.services.config import ConfigManager

from nbclassic import url_prefix_notebook

from . import tools


Expand Down Expand Up @@ -102,8 +106,7 @@ def get(self, path):
# finish the request
yield maybe_future(bundler_mod.bundle(self, model))

_bundler_id_regex = r'(?P<bundler_id>[A-Za-z0-9_]+)'

default_handlers = [
(r"/bundle/(.*)", BundlerHandler)
(r"%s/bundle/(.*)" % url_prefix_notebook(), BundlerHandler)
]
7 changes: 6 additions & 1 deletion nbclassic/edit/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
# Distributed under the terms of the Modified BSD License.

from tornado import web

from jupyter_server.base.handlers import JupyterHandler, path_regex
from jupyter_server.utils import url_escape
from jupyter_server.extension.handler import (
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin
)

from nbclassic import url_prefix_notebook


class EditorHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
"""Render the text editor interface."""

Expand All @@ -29,6 +33,7 @@ def get(self, path):
)
)


default_handlers = [
(r"/edit%s" % path_regex, EditorHandler),
(r"{}/edit{}".format(url_prefix_notebook(), path_regex), EditorHandler),
]
11 changes: 7 additions & 4 deletions nbclassic/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from collections import namedtuple

from tornado import web, gen

from jupyter_server.transutils import _i18n
from jupyter_server.utils import (
ensure_async
Expand All @@ -16,11 +20,10 @@
ExtensionHandlerJinjaMixin
)
from jupyter_server.base.handlers import JupyterHandler
from collections import namedtuple
import os
from tornado import web, gen
HTTPError = web.HTTPError

from nbclassic import url_prefix_notebook


def get_frontend_exporters():
from nbconvert.exporters.base import get_export_names, get_exporter
Expand Down Expand Up @@ -111,5 +114,5 @@ def get(self, path):


default_handlers = [
(r"/notebooks%s" % path_regex, NotebookHandler),
(r"{}/notebooks{}".format(url_prefix_notebook(), path_regex), NotebookHandler),
]
7 changes: 5 additions & 2 deletions nbclassic/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import nbclassic
from nbclassic import (
DEFAULT_STATIC_FILES_PATH,
DEFAULT_TEMPLATE_PATH_LIST
DEFAULT_TEMPLATE_PATH_LIST,
url_prefix_notebook
)

from nbclassic._version import __version__
Expand Down Expand Up @@ -117,7 +118,7 @@ class NotebookApp(
extension_url = "/tree"
subcommands = {}

default_url = Unicode("/tree").tag(config=True)
default_url = Unicode("%s/tree" % url_prefix_notebook()).tag(config=True)

# Override the default open_Browser trait in ExtensionApp,
# setting it to True.
Expand Down Expand Up @@ -200,6 +201,7 @@ def _prepare_templates(self):
nbui = gettext.translation('nbui', localedir=os.path.join(
base_dir, 'nbclassic/i18n'), fallback=True)
self.jinja2_env.install_gettext_translations(nbui, newstyle=False)
self.jinja2_env.globals.update(base_url_prefix=url_prefix_notebook)

def _link_jupyter_server_extension(self, serverapp):
# Monkey-patch Jupyter Server's and nbclassic's static path list to include
Expand Down Expand Up @@ -257,6 +259,7 @@ def initialize_settings(self):
)
self.settings.update(**settings)


def initialize_handlers(self):
"""Load the (URL pattern, handler) tuples for each component."""
# Tornado adds two types of "Routers" to the web application, 1) the
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/base/js/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ define(function(){
// tree
jglobal('SessionList','tree/js/sessionlist');

Jupyter.version = "0.4.3";
Jupyter.version = "0.5.0.dev0";
Jupyter._target = '_blank';

return Jupyter;
Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/edit/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ define([
* file_path : string
*/
options = options || {};
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.selector = selector;
this.editor = options.editor;
Expand All @@ -54,7 +55,7 @@ define([
editor.contents.new_untitled(parent, {type: "file"}).then(
function (data) {
w.location = utils.url_path_join(
that.base_url, 'edit', utils.encode_uri_components(data.path)
that.base_url_prefix, that.base_url, 'edit', utils.encode_uri_components(data.path)
);
},
function(error) {
Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ define([
* config: ConfigSection instance
*/
options = options || {};
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.selector = selector;
this.notebook = options.notebook;
Expand Down Expand Up @@ -177,7 +178,7 @@ define([
var parent = utils.url_path_split(that.notebook.notebook_path)[0];
window.open(
utils.url_path_join(
that.base_url, 'tree',
that.base_url_prefix, that.base_url, 'tree',
utils.encode_uri_components(parent)
), IPython._target);
});
Expand Down
1 change: 1 addition & 0 deletions nbclassic/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ define([
this.config.loaded.then(this.validate_config.bind(this));
this.class_config = new configmod.ConfigWithDefaults(this.config,
Notebook.options_default, 'Notebook');
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url;
this.notebook_path = options.notebook_path;
this.notebook_name = options.notebook_name;
Expand Down
1 change: 1 addition & 0 deletions nbclassic/static/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function(utils) {
"use strict";
var ConfigSection = function(section_name, options) {
this.section_name = section_name;
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url;
this.data = {};

Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/services/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ define(function(requirejs) {
* Dictionary of keyword arguments.
* base_url: string
*/
this.base_url = options.base_url;
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url;
};

/** Error type */
Expand Down
1 change: 1 addition & 0 deletions nbclassic/static/services/sessions/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ define([
name: options.kernel_name
};

this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url;
this.ws_url = options.ws_url;
this.session_service_url = utils.url_path_join(this.base_url, 'api/sessions');
Expand Down
1 change: 1 addition & 0 deletions nbclassic/static/tree/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ requirejs([

var common_options = {
base_url: utils.get_body_data("baseUrl"),
base_url_prefix: document.baseUrlPrefix || "",
notebook_path: utils.get_body_data("notebookPath"),
};
var cfg = new config.ConfigSection('tree', common_options);
Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/tree/js/newnotebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define([

var NewNotebookWidget = function (selector, options) {
this.selector = selector;
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url;
this.contents = options.contents;
this.events = options.events;
Expand Down Expand Up @@ -84,7 +85,7 @@ define([
this.contents.new_untitled(dir_path, {type: "notebook"}).then(
function (data) {
var url = utils.url_path_join(
that.base_url, 'notebooks',
that.base_url_prefix, that.base_url, 'notebooks',
utils.encode_uri_components(data.path)
);
if (kernel_name) {
Expand Down
10 changes: 7 additions & 3 deletions nbclassic/static/tree/js/notebooklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ define([
}
this.notebooks_list = [];
this.sessions = {};
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
this.initial_notebook_path = this.notebook_path;
Expand Down Expand Up @@ -170,7 +171,7 @@ define([
var w = window.open('', IPython._target);
that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) {
w.location = utils.url_path_join(
that.base_url, 'edit',
that.base_url_prefix, that.base_url, 'edit',
utils.encode_uri_components(data.path)
);
}).catch(function (e) {
Expand Down Expand Up @@ -384,7 +385,7 @@ define([
var breadcrumb = $('.breadcrumb');
breadcrumb.empty();
var list_item = $('<li/>');
var root_url = utils.url_path_join(that.base_url, '/tree');
var root_url = utils.url_path_join(that.base_url_prefix, that.base_url, '/tree');
var root = $('<li/>').append(
$("<a/>")
.attr('href', root_url)
Expand All @@ -402,7 +403,7 @@ define([
window.history.pushState(
{path: path},
'Home',
utils.url_path_join(that.base_url, 'tree')
utils.url_path_join(that.base_url_prefix, that.base_url, 'tree')
);
that.update_location(path);
return false;
Expand All @@ -414,6 +415,7 @@ define([
path_parts.push(path_part);
var path = path_parts.join('/');
var url = utils.url_path_join(
that.base_url_prefix,
that.base_url,
'/tree',
utils.encode_uri_components(path)
Expand Down Expand Up @@ -905,6 +907,7 @@ define([
var link = item.find("a.item_link")
.attr('href',
utils.url_path_join(
this.base_url_prefix,
this.base_url,
uri_prefix,
utils.encode_uri_components(model.path)
Expand All @@ -928,6 +931,7 @@ define([
window.history.pushState({
path: model.path
}, model.path, utils.url_path_join(
that.base_url_prefix,
that.base_url,
'tree',
utils.encode_uri_components(model.path)
Expand Down
1 change: 1 addition & 0 deletions nbclassic/static/tree/js/sessionlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ define([
*/
this.events = options.events;
this.sessions = {};
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url || utils.get_body_data("baseUrl");

// Add collapse arrows.
Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/tree/js/terminallist.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ define([
* Dictionary of keyword arguments.
* base_url: string
*/
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.base_url_prefix = options.base_url_prefix;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.element_name = options.element_name || 'running';
this.selector = selector;
this.terminals = [];
Expand Down
1 change: 1 addition & 0 deletions nbclassic/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<script src="{{static_url('components/create-react-class/index.js')}}" type="text/javascript"></script>
<script src="{{static_url('components/requirejs/require.js') }}" type="text/javascript" charset="utf-8"></script>
<script>
document.baseUrlPrefix = '{{base_url_prefix()}}';
require.config({
{% if version_hash %}
urlArgs: "v={{version_hash}}",
Expand Down
8 changes: 5 additions & 3 deletions nbclassic/tree/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import re
from tornado import web
import os

from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.extension.handler import (
Expand All @@ -17,6 +17,8 @@
from jupyter_server.base.handlers import path_regex
from jupyter_server.utils import url_path_join, url_escape

from nbclassic import url_prefix_notebook


class TreeHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
"""Render the tree view, listing notebooks, etc."""
Expand Down Expand Up @@ -82,6 +84,6 @@ def get(self, path=''):


default_handlers = [
(r"/tree%s" % path_regex, TreeHandler),
(r"/tree", TreeHandler),
(r"{}/tree{}".format(url_prefix_notebook(), path_regex), TreeHandler),
(r"%s/tree" % url_prefix_notebook(), TreeHandler),
]