Skip to content

Commit

Permalink
Allow NBClassic to work with NB7 and ServerApp.base_url (#165)
Browse files Browse the repository at this point in the history
* Allow NBClassic to work if ServerApp.base_url is set and Notebook 7 is installed
Also resolves an issue where New->New Notebook did not respect the "/nbclassic/" in the url if it was set
Personally not a fan of the var still being called base_url_prefix but wans't willing to make that big of a change
Closes #164

* Change name of url prefix to be more appropriate
Rename base_url_prefix to nbclassic_path
rename baseUrlPrefix to nbclassicPath
rename url_prefix_notebook() to nbclassic_path()
  • Loading branch information
mcrutch authored Oct 18, 2022
1 parent 26e49b4 commit 04169e2
Show file tree
Hide file tree
Showing 22 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion nbclassic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
]


def url_prefix_notebook():
def nbclassic_path():
if NOTEBOOK_V7_DETECTED:
return "/nbclassic"
return ""
Expand Down
6 changes: 2 additions & 4 deletions nbclassic/bundler/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import inspect
import concurrent.futures

from nbclassic import url_prefix_notebook
from nbclassic import nbclassic_path

from traitlets.utils.importstring import import_item
from tornado import web, gen
Expand All @@ -16,8 +16,6 @@
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 @@ -108,5 +106,5 @@ def get(self, path):


default_handlers = [
(r"%s/bundle/(.*)" % url_prefix_notebook(), BundlerHandler)
(r"%s/bundle/(.*)" % nbclassic_path(), BundlerHandler)
]
4 changes: 2 additions & 2 deletions nbclassic/edit/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ExtensionHandlerJinjaMixin
)

from nbclassic import url_prefix_notebook
from nbclassic import nbclassic_path


class EditorHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
Expand All @@ -35,5 +35,5 @@ def get(self, path):


default_handlers = [
(r"{}/edit{}".format(url_prefix_notebook(), path_regex), EditorHandler),
(r"{}/edit{}".format(nbclassic_path(), path_regex), EditorHandler),
]
4 changes: 2 additions & 2 deletions nbclassic/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from jupyter_server.base.handlers import JupyterHandler
HTTPError = web.HTTPError

from nbclassic import url_prefix_notebook
from nbclassic import nbclassic_path


def get_frontend_exporters():
Expand Down Expand Up @@ -114,5 +114,5 @@ def get(self, path):


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

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

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

# Override the default open_Browser trait in ExtensionApp,
# setting it to True.
Expand Down Expand Up @@ -194,7 +194,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)
self.jinja2_env.globals.update(nbclassic_path=nbclassic_path)

def _link_jupyter_server_extension(self, serverapp):
# Monkey-patch Jupyter Server's and nbclassic's static path list to include
Expand Down
4 changes: 2 additions & 2 deletions nbclassic/static/edit/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ define([
* file_path : string
*/
options = options || {};
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.selector = selector;
this.editor = options.editor;
Expand All @@ -55,7 +55,7 @@ define([
editor.contents.new_untitled(parent, {type: "file"}).then(
function (data) {
w.location = utils.url_path_join(
that.base_url_prefix, that.base_url, 'edit', utils.encode_uri_components(data.path)
that.base_url, that.nbclassic_path, 'edit', utils.encode_uri_components(data.path)
);
},
function(error) {
Expand Down
4 changes: 2 additions & 2 deletions nbclassic/static/notebook/js/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ requirejs([
], function ($, dialog, i18n, _, IPython) {
'use strict';
$('#notebook_about').click(function () {
// The baseUrlPrefix is only injected in the document by nbclassic.
var is_nbclassic = document.baseUrlPrefix !== undefined;
// The nbclassicPath is only injected in the document by nbclassic.
var is_nbclassic = document.nbclassicPath !== undefined;
// use underscore template to auto html escape
if (sys_info) {
var text = '';
Expand Down
6 changes: 4 additions & 2 deletions nbclassic/static/notebook/js/kernelselector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ define([
], function($, IPython, dialog, utils, i18n) {
"use strict";

var KernelSelector = function(selector, notebook) {
var KernelSelector = function(selector, notebook, options) {
options = options || {};
var that = this;
this.selector = selector;
this.notebook = notebook;
this.nbclassic_path = options.nbclassic_path;
this.notebook.set_kernelselector(this);
this.events = notebook.events;
this.current_selection = null;
Expand Down Expand Up @@ -307,7 +309,7 @@ define([
that.notebook.contents.new_untitled(parent, {type: "notebook"}).then(
function (data) {
var url = utils.url_path_join(
that.notebook.base_url, 'notebooks',
that.notebook.base_url, that.nbclassic_path, 'notebooks',
utils.encode_uri_components(data.path)
);
url += "?kernel_name=" + kernel_name;
Expand Down
3 changes: 2 additions & 1 deletion nbclassic/static/notebook/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ requirejs([
var common_options = {
ws_url : utils.get_body_data("wsUrl"),
base_url : utils.get_body_data("baseUrl"),
nbclassic_path: document.nbclassicPath || "",
notebook_path : utils.get_body_data("notebookPath"),
notebook_name : utils.get_body_data('notebookName')
};
Expand Down Expand Up @@ -168,7 +169,7 @@ requirejs([
keyboard_manager: keyboard_manager});
notification_area.init_notification_widgets();
var kernel_selector = new kernelselector.KernelSelector(
'#kernel_logo_widget', notebook);
'#kernel_logo_widget', notebook, common_options);
searchandreplace.load(keyboard_manager);

$('body').append('<div id="fonttest"><pre><span id="test1">x</span>'+
Expand Down
4 changes: 2 additions & 2 deletions nbclassic/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define([
* config: ConfigSection instance
*/
options = options || {};
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.selector = selector;
this.notebook = options.notebook;
Expand Down Expand Up @@ -178,7 +178,7 @@ define([
var parent = utils.url_path_split(that.notebook.notebook_path)[0];
window.open(
utils.url_path_join(
that.base_url_prefix, that.base_url, 'tree',
that.base_url, that.nbclassic_path, 'tree',
utils.encode_uri_components(parent)
), IPython._target);
});
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +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.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url;
this.notebook_path = options.notebook_path;
this.notebook_name = options.notebook_name;
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url;
this.data = {};

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

Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/services/sessions/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define([
name: options.kernel_name
};

this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
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/terminal/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ requirejs([

var common_options = {
base_url : utils.get_body_data("baseUrl"),
nbclassic_path: document.nbclassicPath || ""
};

var config = new configmod.ConfigSection('terminal', common_options);
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/tree/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ requirejs([

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

var NewNotebookWidget = function (selector, options) {
this.selector = selector;
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url;
this.contents = options.contents;
this.events = options.events;
Expand Down Expand Up @@ -85,7 +85,7 @@ define([
this.contents.new_untitled(dir_path, {type: "notebook"}).then(
function (data) {
var url = utils.url_path_join(
that.base_url_prefix, that.base_url, 'notebooks',
that.base_url, that.nbclassic_path, 'notebooks',
utils.encode_uri_components(data.path)
);
if (kernel_name) {
Expand Down
14 changes: 7 additions & 7 deletions nbclassic/static/tree/js/notebooklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ define([
}
this.notebooks_list = [];
this.sessions = {};
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
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 @@ -171,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_prefix, that.base_url, 'edit',
that.base_url, that.nbclassic_path, 'edit',
utils.encode_uri_components(data.path)
);
}).catch(function (e) {
Expand Down Expand Up @@ -385,7 +385,7 @@ define([
var breadcrumb = $('.breadcrumb');
breadcrumb.empty();
var list_item = $('<li/>');
var root_url = utils.url_path_join(that.base_url_prefix, that.base_url, '/tree');
var root_url = utils.url_path_join(that.base_url, that.nbclassic_path, '/tree');
var root = $('<li/>').append(
$("<a/>")
.attr('href', root_url)
Expand All @@ -403,7 +403,7 @@ define([
window.history.pushState(
{path: path},
'Home',
utils.url_path_join(that.base_url_prefix, that.base_url, 'tree')
utils.url_path_join(that.base_url, that.nbclassic_path, 'tree')
);
that.update_location(path);
return false;
Expand All @@ -415,8 +415,8 @@ define([
path_parts.push(path_part);
var path = path_parts.join('/');
var url = utils.url_path_join(
that.base_url_prefix,
that.base_url,
that.nbclassic_path,
'/tree',
utils.encode_uri_components(path)
);
Expand Down Expand Up @@ -907,8 +907,8 @@ define([
var link = item.find("a.item_link")
.attr('href',
utils.url_path_join(
this.base_url_prefix,
this.base_url,
this.nbclassic_path,
uri_prefix,
utils.encode_uri_components(model.path)
)
Expand All @@ -931,8 +931,8 @@ define([
window.history.pushState({
path: model.path
}, model.path, utils.url_path_join(
that.base_url_prefix,
that.base_url,
that.nbclassic_path,
'tree',
utils.encode_uri_components(model.path)
));
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/tree/js/sessionlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define([
*/
this.events = options.events;
this.sessions = {};
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url || utils.get_body_data("baseUrl");

// Add collapse arrows.
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/static/tree/js/terminallist.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define([
* Dictionary of keyword arguments.
* base_url: string
*/
this.base_url_prefix = options.base_url_prefix;
this.nbclassic_path = options.nbclassic_path;
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.element_name = options.element_name || 'running';
this.selector = selector;
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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()}}';
document.nbclassicPath = '{{nbclassic_path()}}';
require.config({
{% if version_hash %}
urlArgs: "v={{version_hash}}",
Expand Down
6 changes: 3 additions & 3 deletions nbclassic/tree/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from jupyter_server.base.handlers import path_regex
from jupyter_server.utils import url_path_join, url_escape

from nbclassic import url_prefix_notebook
from nbclassic import nbclassic_path


class TreeHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
Expand Down Expand Up @@ -84,6 +84,6 @@ def get(self, path=''):


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

0 comments on commit 04169e2

Please sign in to comment.