Skip to content

Commit 1266e17

Browse files
committed
[collapsible_headings] simplify embedding js into main html page
by detecting whether we're in a live notebook, then defining with/without an explicit module name as appropriate
1 parent 2dfa072 commit 1266e17

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/jupyter_contrib_nbextensions/nbconvert_support/collapsible_headings.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def __init__(self, *args, **kwargs):
3232
'collapsible_headings')
3333

3434
with open(os.path.join(ch_dir, 'main.css'), 'r') as f:
35-
main_css = f.read()
36-
self.inliner_resources['css'].append(main_css)
35+
self.inliner_resources['css'].append(f.read())
3736

3837
self.inliner_resources['css'].append("""
3938
/* no local copies of fontawesome fonts from basic templates, so get them from cdn */
@@ -50,11 +49,7 @@ def __init__(self, *args, **kwargs):
5049
""") # noqa: E501
5150

5251
with open(os.path.join(ch_dir, 'main.js'), 'r') as f:
53-
self.inliner_resources['js'].append(
54-
f.read().replace(
55-
"define([",
56-
"define('nbextensions/collapsible_headings/main', [")
57-
)
52+
self.inliner_resources['js'].append(f.read())
5853

5954
cm = ConfigManager()
6055
collapsible_headings_options = cm.get('notebook').get(

src/jupyter_contrib_nbextensions/nbextensions/collapsible_headings/main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
define(['jquery', 'require'], function ($, require) {
1+
(require.specified('base/js/namespace') ? define : function (deps, callback) {
2+
// if here, the Jupyter namespace hasn't been specified to be loaded.
3+
// This means that we're probably embedded in a page, so we need to make
4+
// our definition with a specific module name
5+
return define('nbextensions/collapsible_headings/main', deps, callback);
6+
})(['jquery', 'require'], function ($, require) {
27
"use strict";
38

49
var mod_name = 'collapsible_headings';
@@ -632,7 +637,7 @@ define(['jquery', 'require'], function ($, require) {
632637
var filter_func;
633638
if (is_h) {
634639
var lvl = get_cell_level(cell);
635-
filter_func = function (c) { return get_cell_level(c) < lvl; }
640+
filter_func = function (c) { return get_cell_level(c) < lvl; };
636641
}
637642
cell = find_header_cell(cell, filter_func);
638643
if (cell !== undefined) {
@@ -1025,7 +1030,7 @@ define(['jquery', 'require'], function ($, require) {
10251030
add_buttons_and_shortcuts();
10261031
})
10271032
.catch(function on_reject (reason) {
1028-
console.error(log_prefix, 'error:', reason)
1033+
console.error(log_prefix, 'error:', reason);
10291034
});
10301035
}
10311036

0 commit comments

Comments
 (0)