notebook.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. Notebook
  2. --------
  3. Background
  4. ~~~~~~~~~~
  5. A JupyterLab architecture walkthrough from June 16, 2016, provides an overview of the notebook architecture.
  6. .. raw:: html
  7. <div class="jp-youtube-video">
  8. <iframe src="https://www.youtube-nocookie.com/embed/4Qm6oD_Rlw8?rel=0&amp;showinfo=0&amp;start=3326" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  9. </div>
  10. The most complicated plugin included in the **JupyterLab application**
  11. is the **Notebook plugin**.
  12. The
  13. `NotebookWidgetFactory <http://jupyterlab.github.io/jupyterlab/classes/_notebook_src_widgetfactory_.notebookwidgetfactory.html>`__
  14. constructs a new
  15. `NotebookPanel <http://jupyterlab.github.io/jupyterlab/classes/_notebook_src_panel_.notebookpanel.html>`__
  16. from a model and populates the toolbar with default widgets.
  17. Structure of the Notebook plugin
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. The Notebook plugin provides a model and widgets for dealing with
  20. notebook files.
  21. Model
  22. ^^^^^
  23. The
  24. `NotebookModel <http://jupyterlab.github.io/jupyterlab/classes/_notebook_src_model_.notebookmodel.html>`__
  25. contains an observable list of cells.
  26. A `cell
  27. model <http://jupyterlab.github.io/jupyterlab/modules/_cells_src_model_.html>`__
  28. can be:
  29. - a code cell
  30. - a markdown cell
  31. - raw cell
  32. A code cell contains a list of **output models**. The list of cells and
  33. the list of outputs can be observed for changes.
  34. Cell operations
  35. '''''''''''''''
  36. The NotebookModel cell list supports single-step operations such as
  37. moving, adding, or deleting cells. Compound cell list operations, such
  38. as undo/redo, are also supported by the NotebookModel. Right now,
  39. undo/redo is only supported on cells and is not supported on notebook
  40. attributes, such as notebook metadata. Currently, undo/redo for
  41. individual cell input content is supported by the CodeMirror editor's
  42. undo feature. (Note: CodeMirror editor's undo does not cover cell
  43. metadata changes.)
  44. Cursors and metadata
  45. ''''''''''''''''''''
  46. The notebook model and the cell model (i.e. notebook cells) support
  47. getting and setting metadata through cursors. You may request a cursor
  48. to write to a specific metadata key from a notebook model or a cell
  49. model.
  50. Notebook widget
  51. ^^^^^^^^^^^^^^^
  52. After the NotebookModel is created, the NotebookWidgetFactory constructs
  53. a new NotebookPanel from the model. The NotebookPanel widget is added to
  54. the DockPanel. The **NotebookPanel** contains:
  55. - a
  56. `Toolbar <http://jupyterlab.github.io/jupyterlab/modules/_apputils_src_toolbar_.html>`__
  57. - a `Notebook
  58. widget <http://jupyterlab.github.io/jupyterlab/classes/_notebook_src_widget_.notebook.html>`__.
  59. The NotebookPanel also adds completion logic.
  60. The **NotebookToolbar** maintains a list of widgets to add to the
  61. toolbar. The **Notebook widget** contains the rendering of the notebook
  62. and handles most of the interaction logic with the notebook itself (such
  63. as keeping track of interactions such as selected and active cells and
  64. also the current edit/command mode).
  65. The NotebookModel cell list provides ways to do fine-grained changes to
  66. the cell list.
  67. Higher level actions using NotebookActions
  68. ''''''''''''''''''''''''''''''''''''''''''
  69. Higher-level actions are contained in the
  70. `NotebookActions <http://jupyterlab.github.io/jupyterlab/modules/_notebook_src_actions_.notebookactions.html>`__
  71. namespace, which has functions, when given a notebook widget, to run a
  72. cell and select the next cell, merge or split cells at the cursor,
  73. delete selected cells, etc.
  74. Widget hierarchy
  75. ''''''''''''''''
  76. A Notebook widget contains a list of `cell
  77. widgets <http://jupyterlab.github.io/jupyterlab/modules/_cells_src_widget_.html>`__,
  78. corresponding to the cell models in its cell list.
  79. - Each cell widget contains an
  80. `InputArea <http://jupyterlab.github.io/jupyterlab/classes/_cells_src_inputarea_.inputarea.html>`__,
  81. - which contains n
  82. `CodeEditorWrapper <http://jupyterlab.github.io/jupyterlab/classes/_codeeditor_src_widget_.codeeditorwrapper.html>`__,
  83. - which contains a JavaScript CodeMirror instance.
  84. A
  85. `CodeCell <http://jupyterlab.github.io/jupyterlab/classes/_cells_src_widget_.codecell.html>`__
  86. also contains an
  87. `OutputArea <http://jupyterlab.github.io/jupyterlab/classes/_outputarea_src_widget_.outputarea.html>`__.
  88. An OutputArea is responsible for rendering the outputs in the
  89. `OutputAreaModel <http://jupyterlab.github.io/jupyterlab/classes/_outputarea_src_model_.outputareamodel.html>`__
  90. list. An OutputArea uses a notebook-specific
  91. `RenderMimeRegistry <http://jupyterlab.github.io/jupyterlab/classes/_rendermime_src_registry_.rendermimeregistry.html>`__
  92. object to render ``display_data`` output messages.
  93. Rendering output messages
  94. '''''''''''''''''''''''''
  95. A **Rendermime plugin** provides a pluggable system for rendering output
  96. messages. Default renderers are provided for markdown, html, images,
  97. text, etc. Extensions can register renderers to be used across the
  98. entire application by registering a handler and mimetype in the
  99. rendermime registry. When a notebook is created, it copies the global
  100. Rendermime singleton so that notebook-specific renderers can be added.
  101. The ipywidgets widget manager is an example of an extension that adds a
  102. notebook-specific renderer, since rendering a widget depends on
  103. notebook-specific widget state.
  104. How to extend the Notebook plugin
  105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. We'll walk through two notebook extensions:
  107. - adding a button to the toolbar
  108. - adding an ipywidgets extension
  109. Adding a button to the toolbar
  110. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  111. Start from the cookie cutter extension template.
  112. ::
  113. pip install cookiecutter
  114. cookiecutter https://github.com/jupyterlab/extension-cookiecutter-ts
  115. cd my-cookie-cutter-name
  116. Install the dependencies. Note that extensions are built against the
  117. released npm packages, not the development versions.
  118. ::
  119. npm install --save @jupyterlab/notebook @jupyterlab/application @jupyterlab/apputils @jupyterlab/docregistry @phosphor/disposable
  120. Copy the following to ``src/index.ts``:
  121. .. code:: typescript
  122. import {
  123. IDisposable, DisposableDelegate
  124. } from '@phosphor/disposable';
  125. import {
  126. JupyterLab, JupyterLabPlugin
  127. } from '@jupyterlab/application';
  128. import {
  129. ToolbarButton
  130. } from '@jupyterlab/apputils';
  131. import {
  132. DocumentRegistry
  133. } from '@jupyterlab/docregistry';
  134. import {
  135. NotebookActions, NotebookPanel, INotebookModel
  136. } from '@jupyterlab/notebook';
  137. /**
  138. * The plugin registration information.
  139. */
  140. const plugin: JupyterLabPlugin<void> = {
  141. activate,
  142. id: 'my-extension-name:buttonPlugin',
  143. autoStart: true
  144. };
  145. /**
  146. * A notebook widget extension that adds a button to the toolbar.
  147. */
  148. export
  149. class ButtonExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> {
  150. /**
  151. * Create a new extension object.
  152. */
  153. createNew(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable {
  154. let callback = () => {
  155. NotebookActions.runAll(panel.notebook, context.session);
  156. };
  157. let button = new ToolbarButton({
  158. className: 'myButton',
  159. onClick: callback,
  160. tooltip: 'Run All'
  161. });
  162. let i = document.createElement('i');
  163. i.classList.add('fa', 'fa-fast-forward');
  164. button.node.appendChild(i);
  165. panel.toolbar.insertItem(0, 'runAll', button);
  166. return new DisposableDelegate(() => {
  167. button.dispose();
  168. });
  169. }
  170. }
  171. /**
  172. * Activate the extension.
  173. */
  174. function activate(app: JupyterLab) {
  175. app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
  176. };
  177. /**
  178. * Export the plugin as default.
  179. */
  180. export default plugin;
  181. Run the following commands:
  182. ::
  183. npm install
  184. npm run build
  185. jupyter labextension install .
  186. jupyter lab
  187. Open a notebook and observe the new "Run All" button.
  188. The *ipywidgets* third party extension
  189. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  190. This discussion will be a bit confusing since we've been using the term
  191. *widget* to refer to *phosphor widgets*. In the discussion below,
  192. *ipython widgets* will be referred to as *ipywidgets*. There is no
  193. intrinsic relation between *phosphor widgets* and *ipython widgets*.
  194. The *ipywidgets* extension registers a factory for a notebook *widget*
  195. extension using the `Document
  196. Registry <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html>`__.
  197. The ``createNew()`` function is called with a NotebookPanel and
  198. `DocumentContext <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.icontext.html>`__.
  199. The plugin then creates a ipywidget manager (which uses the context to
  200. interact the kernel and kernel's comm manager). The plugin then
  201. registers an ipywidget renderer with the notebook instance's rendermime
  202. (which is specific to that particular notebook).
  203. When an ipywidget model is created in the kernel, a comm message is sent
  204. to the browser and handled by the ipywidget manager to create a
  205. browser-side ipywidget model. When the model is displayed in the kernel,
  206. a ``display_data`` output is sent to the browser with the ipywidget
  207. model id. The renderer registered in that notebook's rendermime is asked
  208. to render the output. The renderer asks the ipywidget manager instance
  209. to render the corresponding model, which returns a JavaScript promise.
  210. The renderer creates a container *phosphor widget* which it hands back
  211. synchronously to the OutputArea, and then fills the container with the
  212. rendered *ipywidget* when the promise resolves.
  213. Note: The ipywidgets third party extension has not yet been released.