Skip to content

Commit 359d455

Browse files
committed
[docs] update example nbextension
* to use example CSS file * to use simpler config loading mechanism [skip CI]
1 parent 431fbdb commit 359d455

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,17 @@ See also [installing Jupyter](https://jupyter.readthedocs.io/en/latest/install.h
231231
Notebook extension structure
232232
============================
233233

234-
The nbextensions are stored each as a separate subdirectory of `src/jupyter_contrib_nbextensions/nbextensions`
235-
Each notebook extension typically has it's own directory containing:
234+
The nbextensions are stored in the repository each as a separate subdirectory of
235+
`src/jupyter_contrib_nbextensions/nbextensions`.
236236

237-
* `thisextension/main.js` - javascript implementing the extension
238-
* `thisextension/main.css` - optional CSS
239-
* `thisextension/readme.md` - readme file describing the extension in markdown format
240-
* `thisextension/config.yaml` - file describing the extension to the `jupyter_nbextensions_configurator` server extension
237+
Each notebook extension typically has its own directory named after the extension, containing:
241238

239+
* `thisextension/thisextension.js` - javascript implementing the nbextension
240+
* `thisextension/thisextension.yml` - file describing the nbextension to the `jupyter_nbextensions_configurator` server extension
241+
* `thisextension/thisextension.css` - optional CSS file, which may be loaded by the javascript
242+
* `thisextension/README.md` - readme file describing the nbextension in markdown format
243+
244+
For further details, see [the documentation at jupyter-contrib-nbextensions.readthedocs.io](http://jupyter-contrib-nbextensions.readthedocs.io/en/latest/internals.html).
242245

243246
Changes
244247
=======

docs/source/internals.rst

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@
33
Notebook extension structure
44
============================
55

6-
The nbextensions are stored each as a separate subdirectory of
7-
`src/jupyter_contrib_nbextensions/nbextensions`
6+
The nbextensions are stored in the repository each as a separate subdirectory of
7+
`src/jupyter_contrib_nbextensions/nbextensions`.
88

9-
Each notebook extension typically has its own directory containing:
9+
Each notebook extension typically has its own directory named after the extension, containing:
1010

11-
* `thisextension/main.js`: javascript implementing the nbextension
12-
* `thisextension/main.css`: optional CSS
13-
* `thisextension/readme.md`: readme file describing the nbextension in markdown format
14-
* `thisextension/config.yaml`: file describing the nbextension to the `jupyter_nbextensions_configurator` server extension
11+
* `thisextension/thisextension.js` - javascript implementing the nbextension
12+
* `thisextension/thisextension.yml` - file describing the nbextension to the `jupyter_nbextensions_configurator` server extension
13+
* `thisextension/thisextension.css` - optional CSS file, which may be loaded by the javascript
14+
* `thisextension/README.md` - readme file describing the nbextension in markdown format
1515

16-
The file names do not need to have the shown names, they can be choosen freely.
17-
This is an example for the `main.js` file:
16+
The file names do not need to have the shown names, they can be chosen freely, as long as they refer to each other using the appropriate names.
17+
This is an example for the main `thisextension.js` file:
1818

1919
.. code-block:: javascript
2020
2121
define([
22+
'require',
2223
'jquery',
2324
'base/js/namespace',
24-
'base/js/utils',
25-
'services/config'
2625
], function (
26+
requirejs
2727
$,
28-
IPython,
29-
utils,
30-
configmod,
28+
Jupyter,
3129
) {
3230
"use strict";
3331
@@ -36,74 +34,90 @@ This is an example for the `main.js` file:
3634
my_config_value : 100
3735
};
3836
39-
// create config object to load parameters
40-
var base_url = utils.get_body_data("baseUrl");
41-
var config = new configmod.ConfigSection('notebook', {base_url: base_url});
42-
43-
// update params with any specified in the server's config file
44-
var update_params = function() {
45-
for (var key in params) {
46-
if (config.data.hasOwnProperty(key))
47-
params[key] = config.data[key];
48-
}
49-
};
50-
51-
// will be called when the config parameters have been loaded
52-
config.loaded.then( function() {
53-
update_params();
54-
55-
});
56-
57-
function config_loaded_callback () {
58-
$.extend(true, params, config.data.thisextension);
37+
var initialize = function () {
38+
// update params with any specified in the server's config file.
39+
// the "thisextension" value of the Jupyter notebook config's
40+
// data may be undefined, but that's ok when using JQuery's extend
41+
$.extend(true, params, Jupyter.notebook.config.thisextension);
42+
43+
// add our extension's css to the page
44+
$('<link/>')
45+
.attr({
46+
rel: 'stylesheet',
47+
type: 'text/css',
48+
href: requirejs.toUrl('./thisextension.css')
49+
})
50+
.appendTo('head');
5951
};
6052
61-
// will be called when the nbextension is loaded
53+
// The specially-named function load_ipython_extension will be called
54+
// by the notebook when the nbextension is to be loaded.
55+
// It mustn't take too long to execute however, or the notebook will
56+
// assume an error has occurred.
6257
var load_ipython_extension = function () {
63-
config.load(); // trigger loading config parameters
64-
58+
// Once the config has been loaded, do everything else.
59+
// The loaded object is a javascript Promise object, so the then
60+
// call return immediately. See
61+
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
62+
// for details.
63+
return Jupyter.notebook.config.loaded.then(initialize);
6564
};
6665
67-
// return public methods
66+
// return object to export public methods
6867
return {
69-
load_jupyter_extension : load_jupyter_extension
68+
load_ipython_extension : load_ipython_extension
7069
};
7170
});
7271
73-
And for the `config.yaml` file:
72+
And for the `thisextension.yml` file:
7473

7574
.. code-block:: yaml
7675
7776
Type: Jupyter Notebook Extension
78-
Compatibility: 4.x
79-
Name: thisextension
80-
Main: main.js
77+
Compatibility: 4.x, 5.x
78+
Name: This Extension
79+
Main: thisextension.js
8180
Icon: icon.png
8281
Link: README.md
8382
Description: My super duper extension
8483
Parameters:
85-
- name: my_config_value
86-
description: A configuration parameter
84+
- name: thisextension.my_config_value
85+
description: Number of dupers to create
8786
input_type: number
8887
min: -1
8988
step: 1
9089
default: 100
9190
92-
When supplying a `readme.md` file, please supply a main heading with the
91+
For further details on the yaml file format, and the option types supported by the configurator, see the `jupyter_nbextension_configurator repo <https://github.com/jupyter-contrib/jupyter_nbextensions_configurator>`__.
92+
93+
When supplying a `README.md` file, please supply a main heading with the
9394
nbextension's title, as this will be linked in the generated documentation at
9495
`jupyter-contrib-nbextensions.readthedocs.io <http://jupyter-contrib-nbextensions.readthedocs.io/en/latest>`__.
95-
This is a simple example for `readme.md`::
96+
This is a simple example for a `README.md`:
97+
98+
.. code-block:: markdown
9699
97100
This extension
98101
==============
99102
100-
How to use
101-
----------
102-
Some description here.
103+
A quick summary of what this nbextension does.
104+
105+
106+
Usage
107+
-----
108+
109+
A more detailed description can go here, maybe covering different variations of functionality, differences in versions or for different kernels etc.
103110
104111
![Screenshot image](screenshot.png)
105112
113+
114+
Options
115+
-------
116+
117+
Some description of the different options the nbextension provides, what they do, and their default values
118+
106119
Internals
107120
---------
121+
108122
How this extension works.
109123

0 commit comments

Comments
 (0)