extension_dev.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. .. _developer_extensions:
  2. Extension Developer Guide
  3. -------------------------
  4. JupyterLab can be extended in four ways via:
  5. - **application plugins (top level):** Application plugins extend the
  6. functionality of JupyterLab itself.
  7. - **mime renderer extensions (top level):** Mime Renderer extensions are
  8. a convenience for creating an extension that can render mime data and
  9. potentially render files of a given type.
  10. - **theme extensions (top level):** Theme extensions allow you to customize the appearance of
  11. JupyterLab by adding your own fonts, CSS rules, and graphics to the application.
  12. - **document widget extensions (lower level):** Document widget extensions
  13. extend the functionality of document widgets added to the
  14. application, and we cover them in :ref:`documents`.
  15. See :ref:`apod_extension_tutorial` to learn how to make a simple JupyterLab extension.
  16. A JupyterLab application is comprised of:
  17. - A core Application object
  18. - Plugins
  19. Plugins
  20. ~~~~~~~
  21. A plugin adds a core functionality to the application:
  22. - A plugin can require other plugins for operation.
  23. - A plugin is activated when it is needed by other plugins, or when
  24. explicitly activated.
  25. - Plugins require and provide ``Token`` objects, which are used to
  26. provide a typed value to the plugin's ``activate()`` method.
  27. - The module providing plugin(s) must meet the
  28. `JupyterLab.IPluginModule <https://jupyterlab.github.io/jupyterlab/application/interfaces/jupyterlab.ipluginmodule.html>`__
  29. interface, by exporting a plugin object or array of plugin objects as
  30. the default export.
  31. We provide two cookiecutters to create JupyterLab plugin extensions in
  32. `CommonJS <https://github.com/jupyterlab/extension-cookiecutter-js>`__ and
  33. `TypeScript <https://github.com/jupyterlab/extension-cookiecutter-ts>`__.
  34. The default plugins in the JupyterLab application include:
  35. - `Terminal <https://github.com/jupyterlab/jupyterlab/blob/master/packages/terminal-extension/src/index.ts>`__
  36. - Adds the ability to create command prompt terminals.
  37. - `Shortcuts <https://github.com/jupyterlab/jupyterlab/blob/master/packages/shortcuts-extension/src/index.ts>`__
  38. - Sets the default set of shortcuts for the application.
  39. - `Images <https://github.com/jupyterlab/jupyterlab/blob/master/packages/imageviewer-extension/src/index.ts>`__
  40. - Adds a widget factory for displaying image files.
  41. - `Help <https://github.com/jupyterlab/jupyterlab/blob/master/packages/help-extension/src/index.tsx>`__
  42. - Adds a side bar widget for displaying external documentation.
  43. - `File
  44. Browser <https://github.com/jupyterlab/jupyterlab/blob/master/packages/filebrowser-extension/src/index.ts>`__
  45. - Creates the file browser and the document manager and the file
  46. browser to the side bar.
  47. - `Editor <https://github.com/jupyterlab/jupyterlab/blob/master/packages/fileeditor-extension/src/index.ts>`__
  48. - Add a widget factory for displaying editable source files.
  49. - `Console <https://github.com/jupyterlab/jupyterlab/blob/master/packages/console-extension/src/index.ts>`__
  50. - Adds the ability to launch Jupyter Console instances for
  51. interactive kernel console sessions.
  52. Here is a dependency graph for the core JupyterLab components: |dependencies|
  53. .. danger::
  54. Installing an extension allows for arbitrary code execution on the
  55. server, kernel, and in the client's browser. You should therefore
  56. take steps to protect against malicious changes to your extension's
  57. code. This includes ensuring strong authentication for your npm
  58. account.
  59. Application Object
  60. ~~~~~~~~~~~~~~~~~~
  61. A Jupyter front-end application object is given to each plugin in its
  62. ``activate()`` function. The application object has:
  63. - ``commands`` - an extensible registry used to add and execute commands in the application.
  64. - ``commandLinker`` - used to connect DOM nodes with the command registry so that clicking on them executes a command.
  65. - ``docRegistry`` - an extensible registry containing the document types that the application is able to read and render.
  66. - ``restored`` - a promise that is resolved when the application has finished loading.
  67. - ``serviceManager`` - low-level manager for talking to the Jupyter REST API.
  68. - ``shell`` - a generic Jupyter front-end shell instance, which holds the user interface for the application.
  69. Jupyter Front-End Shell
  70. ~~~~~~~~~~~~~~~~~~~~~~~
  71. The Jupyter front-end
  72. `shell <https://jupyterlab.github.io/jupyterlab/application/interfaces/jupyterfrontend.ishell.html>`__
  73. is used to add and interact with content in the application. The ``IShell``
  74. interface provides an ``add()`` method for adding widgets to the application.
  75. In JupyterLab, the application shell consists of:
  76. - A ``top`` area for things like top level menus and toolbars.
  77. - ``left`` and ``right`` side bar areas for collapsible content.
  78. - A ``main`` work area for user activity.
  79. - A ``bottom`` area for things like status bars.
  80. - A ``header`` area for custom elements.
  81. Phosphor
  82. ~~~~~~~~
  83. The Phosphor library is used as the underlying architecture of
  84. JupyterLab and provides many of the low level primitives and widget
  85. structure used in the application. Phosphor provides a rich set of
  86. widgets for developing desktop-like applications in the browser, as well
  87. as patterns and objects for writing clean, well-abstracted code. The
  88. widgets in the application are primarily **Phosphor widgets**, and
  89. Phosphor concepts, like message passing and signals, are used
  90. throughout. **Phosphor messages** are a *many-to-one* interaction that
  91. enables information like resize events to flow through the widget
  92. hierarchy in the application. **Phosphor signals** are a *one-to-many*
  93. interaction that enable listeners to react to changes in an observed
  94. object.
  95. Extension Authoring
  96. ~~~~~~~~~~~~~~~~~~~
  97. An Extension is a valid `npm
  98. package <https://docs.npmjs.com/getting-started/what-is-npm>`__ that
  99. meets the following criteria:
  100. - Exports one or more JupyterLab plugins as the default export in its
  101. main file.
  102. - Has a ``jupyterlab`` key in its ``package.json`` which has
  103. ``"extension"`` metadata. The value can be ``true`` to use the main
  104. module of the package, or a string path to a specific module (e.g.
  105. ``"lib/foo"``).
  106. - It is also recommended to include the keyword ``jupyterlab-extension``
  107. in the ``package.json``, to aid with discovery (e.g. by the extension
  108. manager).
  109. While authoring the extension, you can use the command:
  110. .. code:: bash
  111. npm install # install npm package dependencies
  112. npm run build # optional build step if using TypeScript, babel, etc.
  113. jupyter labextension install # install the current directory as an extension
  114. This causes the builder to re-install the source folder before building
  115. the application files. You can re-build at any time using
  116. ``jupyter lab build`` and it will reinstall these packages. You can also
  117. link other local ``npm`` packages that you are working on simultaneously
  118. using ``jupyter labextension link``; they will be re-installed but not
  119. considered as extensions. Local extensions and linked packages are
  120. included in ``jupyter labextension list``.
  121. When using local extensions and linked packages, you can run the command
  122. ::
  123. jupyter lab --watch
  124. This will cause the application to incrementally rebuild when one of the
  125. linked packages changes. Note that only compiled JavaScript files (and
  126. the CSS files) are watched by the WebPack process. This means that if
  127. your extension is in TypeScript you'll have to run a ``jlpm run build``
  128. before the changes will be reflected in JupyterLab. To avoid this step
  129. you can also watch the TypeScript sources in your extension which is
  130. usually assigned to the ``tsc -w`` shortcut. If WebPack doesn't seem to
  131. detect the changes, this can be related to `the number of available watches <https://github.com/webpack/docs/wiki/troubleshooting#not-enough-watchers>`__.
  132. Note that the application is built against **released** versions of the
  133. core JupyterLab extensions. If your extension depends on JupyterLab
  134. packages, it should be compatible with the dependencies in the
  135. ``jupyterlab/static/package.json`` file. Note that building will always use the latest JavaScript packages that meet the dependency requirements of JupyterLab itself and any installed extensions. If you wish to test against a
  136. specific patch release of one of the core JupyterLab packages you can
  137. temporarily pin that requirement to a specific version in your own
  138. dependencies.
  139. If you must install a extension into a development branch of JupyterLab, you have to graft it into the source tree of JupyterLab itself. This may be done using the command
  140. ::
  141. jlpm run add:sibling <path-or-url>
  142. in the JupyterLab root directory, where ``<path-or-url>`` refers either
  143. to an extension ``npm`` package on the local file system, or a URL to a git
  144. repository for an extension ``npm`` package. This operation may be
  145. subsequently reversed by running
  146. ::
  147. jlpm run remove:package <extension-dir-name>
  148. This will remove the package metadata from the source tree and delete
  149. all of the package files.
  150. The package should export EMCAScript 6 compatible JavaScript. It can
  151. import CSS using the syntax ``require('foo.css')``. The CSS files can
  152. also import CSS from other packages using the syntax
  153. ``@import url('~foo/index.css')``, where ``foo`` is the name of the
  154. package.
  155. The following file types are also supported (both in JavaScript and
  156. CSS): ``json``, ``html``, ``jpg``, ``png``, ``gif``, ``svg``,
  157. ``js.map``, ``woff2``, ``ttf``, ``eot``.
  158. If your package uses any other file type it must be converted to one of
  159. the above types or `include a loader in the import statement <https://webpack.js.org/concepts/loaders/#inline>`__.
  160. If you include a loader, the loader must be importable at build time, so if
  161. it is not already installed by JupyterLab, you must add it as a dependency
  162. of your extension.
  163. If your JavaScript is written in any other dialect than
  164. EMCAScript 6 (2015) it should be converted using an appropriate tool.
  165. You can use Webpack to pre-build your extension to use any of it's features
  166. not enabled in our build configuration. To build a compatible package set
  167. ``output.libraryTarget`` to ``"commonjs2"`` in your Webpack configuration.
  168. (see `this <https://github.com/saulshanabrook/jupyterlab-webpack>`__ example repo).
  169. If you publish your extension on ``npm.org``, users will be able to install
  170. it as simply ``jupyter labextension install <foo>``, where ``<foo>`` is
  171. the name of the published ``npm`` package. You can alternatively provide a
  172. script that runs ``jupyter labextension install`` against a local folder
  173. path on the user's machine or a provided tarball. Any valid
  174. ``npm install`` specifier can be used in
  175. ``jupyter labextension install`` (e.g. ``foo@latest``, ``bar@3.0.0.0``,
  176. ``path/to/folder``, and ``path/to/tar.gz``).
  177. There are a number of helper functions in ``testutils`` in this repo (which
  178. is a public ``npm`` package called ``@jupyterlab/testutils``) that can be used when
  179. writing tests for an extension. See ``tests/test-application`` for an example
  180. of the infrastructure needed to run tests. There is a ``karma`` config file
  181. that points to the parent directory's ``karma`` config, and a test runner,
  182. ``run-test.py`` that starts a Jupyter server.
  183. .. _rendermime:
  184. Mime Renderer Extensions
  185. ~~~~~~~~~~~~~~~~~~~~~~~~
  186. Mime Renderer extensions are a convenience for creating an extension
  187. that can render mime data and potentially render files of a given type.
  188. We provide a cookiecutter for mime renderer extensions in TypeScript
  189. `here <https://github.com/jupyterlab/mimerender-cookiecutter-ts>`__.
  190. Mime renderer extensions are more declarative than standard extensions.
  191. The extension is treated the same from the command line perspective
  192. (``jupyter labextension install`` ), but it does not directly create
  193. JupyterLab plugins. Instead it exports an interface given in the
  194. `rendermime-interfaces <https://jupyterlab.github.io/jupyterlab/rendermime-interfaces/interfaces/irendermime.iextension.html>`__
  195. package.
  196. The JupyterLab repo has an example mime renderer extension for
  197. `pdf <https://github.com/jupyterlab/jupyterlab/tree/master/packages/pdf-extension>`__
  198. files. It provides a mime renderer for pdf data and registers itself as
  199. a document renderer for pdf file types.
  200. The JupyterLab organization also has a mime renderer extension tutorial
  201. which adds mp4 video rendering to the application
  202. `here <https://github.com/jupyterlab/jupyterlab-mp4>`__.
  203. The ``rendermime-interfaces`` package is intended to be the only
  204. JupyterLab package needed to create a mime renderer extension (using the
  205. interfaces in TypeScript or as a form of documentation if using plain
  206. JavaScript).
  207. The only other difference from a standard extension is that has a
  208. ``jupyterlab`` key in its ``package.json`` with ``"mimeExtension"``
  209. metadata. The value can be ``true`` to use the main module of the
  210. package, or a string path to a specific module (e.g. ``"lib/foo"``).
  211. The mime renderer can update its data by calling ``.setData()`` on the
  212. model it is given to render. This can be used for example to add a
  213. ``png`` representation of a dynamic figure, which will be picked up by a
  214. notebook model and added to the notebook document. When using
  215. ``IDocumentWidgetFactoryOptions``, you can update the document model by
  216. calling ``.setData()`` with updated data for the rendered MIME type. The
  217. document can then be saved by the user in the usual manner.
  218. Themes
  219. ~~~~~~
  220. A theme is a JupyterLab extension that uses a ``ThemeManager`` and can
  221. be loaded and unloaded dynamically. The package must include all static
  222. assets that are referenced by ``url()`` in its CSS files. Local URLs can
  223. be used to reference files relative to the location of the referring sibling CSS files. For example ``url('images/foo.png')`` or
  224. ``url('../foo/bar.css')``\ can be used to refer local files in the
  225. theme. Absolute URLs (starting with a ``/``) or external URLs (e.g.
  226. ``https:``) can be used to refer to external assets. The path to the
  227. theme asset entry point is specified ``package.json`` under the ``"jupyterlab"``
  228. key as ``"themePath"``. See the `JupyterLab Light
  229. Theme <https://github.com/jupyterlab/jupyterlab/tree/master/packages/theme-light-extension>`__
  230. for an example. Ensure that the theme files are included in the
  231. ``"files"`` metadata in ``package.json``. Note that if you want to use SCSS, SASS, or LESS files,
  232. you must compile them to CSS and point JupyterLab to the CSS files.
  233. The theme extension is installed in the same way as a regular extension (see
  234. `extension authoring <#extension-authoring>`__).
  235. It is also possible to create a new theme using the
  236. `TypeScript theme cookiecutter <https://github.com/jupyterlab/theme-cookiecutter>`__.
  237. Standard (General-Purpose) Extensions
  238. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  239. JupyterLab's modular architecture is based around the idea
  240. that all extensions are on equal footing, and that they interact
  241. with each other through typed interfaces that are provided by ``Token`` objects.
  242. An extension can provide a ``Token`` to the application,
  243. which other extensions can then request for their own use.
  244. .. _tokens:
  245. Core Tokens
  246. ^^^^^^^^^^^
  247. The core packages of JupyterLab provide a set of tokens,
  248. which are listed here, along with short descriptions of when you
  249. might want to use them in your extensions.
  250. - ``@jupyterlab/application:IConnectionLost``: A token for invoking the dialog shown
  251. when JupyterLab has lost its connection to the server. Use this if, for some reason,
  252. you want to bring up the "connection lost" dialog under new circumstances.
  253. - ``@jupyterlab/application:IInfo``: A token providing metadata about the current
  254. application, including currently disabled extensions and whether dev mode is enabled.
  255. - ``@jupyterlab/application:IPaths``: A token providing information about various
  256. URLs and server paths for the current application. Use this token if you want to
  257. assemble URLs to use the JupyterLab REST API.
  258. - ``@jupyterlab/application:ILabStatus``: An interface for interacting with the application busy/dirty
  259. status. Use this if you want to set the application "busy" favicon, or to set
  260. the application "dirty" status, which asks the user for confirmation before leaving.
  261. - ``@jupyterlab/application:ILabShell``: An interface to the JupyterLab shell.
  262. The top-level application object also has a reference to the shell, but it has a restricted
  263. interface in order to be agnostic to different spins on the application.
  264. Use this to get more detailed information about currently active widgets and layout state.
  265. - ``@jupyterlab/application:ILayoutRestorer``: An interface to the application layout
  266. restoration functionality. Use this to have your activities restored across
  267. page loads.
  268. - ``@jupyterlab/application:IMimeDocumentTracker``: A widget tracker for documents
  269. rendered using a mime renderer extension. Use this if you want to list and interact
  270. with documents rendered by such extensions.
  271. - ``@jupyterlab/application:IRouter``: The URL router used by the application.
  272. Use this to add custom URL-routing for your extension (e.g., to invoke
  273. a command if the user navigates to a sub-path).
  274. - ``@jupyterlab/apputils:ICommandPalette``: An interface to the application command palette
  275. in the left panel. Use this to add commands to the palette.
  276. - ``@jupyterlab/apputils:ISplashScreen``: An interface to the splash screen for the application.
  277. Use this if you want to show the splash screen for your own purposes.
  278. - ``@jupyterlab/apputils:IThemeManager``: An interface to the theme manager for the application.
  279. Most extensions will not need to use this, as they can register a
  280. `theme extension <#themes>`__.
  281. - ``@jupyterlab/apputils:IWindowResolver``: An interface to a window resolver for the
  282. application. JupyterLab workspaces are given a name, which are determined using
  283. the window resolver. Require this if you want to use the name of the current workspace.
  284. - ``@jupyterlab/codeeditor:IEditorServices``: An interface to the text editor provider
  285. for the application. Use this to create new text editors and host them in your
  286. UI elements.
  287. - ``@jupyterlab/completer:ICompletionManager``: An interface to the completion manager
  288. for the application. Use this to allow your extension to invoke a completer.
  289. - ``@jupyterlab/console:IConsoleTracker``: A widget tracker for code consoles.
  290. Use this if you want to be able to iterate over and interact with code consoles
  291. created by the application.
  292. - ``@jupyterlab/console:IContentFactory``: A factory object that creates new code
  293. consoles. Use this if you want to create and host code consoles in your own UI elements.
  294. - ``@jupyterlab/coreutils:ISettingRegistry``: An interface to the JupyterLab settings system.
  295. Use this if you want to store settings for your application.
  296. See `extension settings <#extension-settings>`__ for more information.
  297. - ``@jupyterlab/coreutils:IStateDB``: An interface to the JupyterLab state database.
  298. Use this if you want to store data that will persist across page loads.
  299. See `state database <#state-database>`__ for more information.
  300. - ``@jupyterlab/docmanager:IDocumentManager``: An interface to the manager for all
  301. documents used by the application. Use this if you want to open and close documents,
  302. create and delete files, and otherwise interact with the file system.
  303. - ``@jupyterlab/documentsearch:ISearchProviderRegistry``: An interface for a registry of search
  304. providers for the application. Extensions can register their UI elements with this registry
  305. to provide find/replace support.
  306. - ``@jupyterlab/filebrowser:IFileBrowserFactory``: A factory object that creates file browsers.
  307. Use this if you want to create your own file browser (e.g., for a custom storage backend),
  308. or to interact with other file browsers that have been created by extensions.
  309. - ``@jupyterlab/fileeditor:IEditorTracker``: A widget tracker for file editors.
  310. Use this if you want to be able to iterate over and interact with file editors
  311. created by the application.
  312. - ``@jupyterlab/htmlviewer:IHTMLViewerTracker``: A widget tracker for rendered HTML documents.
  313. Use this if you want to be able to iterate over and interact with HTML documents
  314. viewed by the application.
  315. - ``@jupyterlab/imageviewer:IImageTracker``: A widget tracker for images.
  316. Use this if you want to be able to iterate over and interact with images
  317. viewed by the application.
  318. - ``@jupyterlab/inspector:IInspector``: An interface for adding variable inspectors to widgets.
  319. Use this to add the ability to hook into the variable inspector to your extension.
  320. - ``@jupyterlab/launcher:ILauncher``: An interface to the application activity launcher.
  321. Use this to add your extension activities to the launcher panel.
  322. - ``@jupyterlab/mainmenu:IMainMenu``: An interface to the main menu bar for the application.
  323. Use this if you want to add your own menu items.
  324. - ``@jupyterlab/markdownviewer:IMarkdownViewerTracker``: A widget tracker for markdown
  325. document viewers. Use this if you want to iterate over and interact with rendered markdown documents.
  326. - ``@jupyterlab/notebook:INotebookTools``: An interface to the ``Notebook Tools`` panel in the
  327. application left area. Use this to add your own functionality to the panel.
  328. - ``@jupyterlab/notebook:IContentFactory``: A factory object that creates new notebooks.
  329. Use this if you want to create and host notebooks in your own UI elements.
  330. - ``@jupyterlab/notebook:INotebookTracker``: A widget tracker for notebooks.
  331. Use this if you want to be able to iterate over and interact with notebooks
  332. created by the application.
  333. - ``@jupyterlab/rendermime:IRenderMimeRegistry``: An interface to the rendermime registry
  334. for the application. Use this to create renderers for various mime-types in your extension.
  335. Most extensions will not need to use this, as they can register a
  336. `mime renderer extension <#mime-renderer-extensions>`__.
  337. - ``@jupyterlab/rendermime:ILatexTypesetter``: An interface to the LaTeX typesetter for the
  338. application. Use this if you want to typeset math in your extension.
  339. - ``@jupyterlab/settingeditor:ISettingEditorTracker``: A widget tracker for setting editors.
  340. Use this if you want to be able to iterate over and interact with setting editors
  341. created by the application.
  342. - ``@jupyterlab/statusbar:IStatusBar``: An interface to the status bar on the application.
  343. Use this if you want to add new status bar items.
  344. - ``@jupyterlab/terminal:ITerminalTracker``: A widget tracker for terminals.
  345. Use this if you want to be able to iterate over and interact with terminals
  346. created by the application.
  347. - ``@jupyterlab/tooltip:ITooltipManager``: An interface to the tooltip manager for the application.
  348. Use this to allow your extension to invoke a tooltip.
  349. - ``@jupyterlab/vdom:IVDOMTracker``: A widget tracker for virtual DOM (VDOM) documents.
  350. Use this to iterate over and interact with VDOM instances created by the application.
  351. Standard Extension Example
  352. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  353. For a concrete example of a standard extension, see :ref:`How to extend the Notebook plugin <extend-notebook-plugin>`.
  354. Notice that the mime renderer extensions use a limited,
  355. simplified interface to JupyterLab's extension system. Modifying the
  356. notebook plugin requires the full, general-purpose interface to the
  357. extension system.
  358. Storing Extension Data
  359. ^^^^^^^^^^^^^^^^^^^^^^
  360. In addition to the file system that is accessed by using the
  361. ``@jupyterlab/services`` package, JupyterLab offers two ways for
  362. extensions to store data: a client-side state database that is built on
  363. top of ``localStorage`` and a plugin settings system that provides for
  364. default setting values and user overrides.
  365. Extension Settings
  366. ``````````````````
  367. An extension can specify user settings using a JSON Schema. The schema
  368. definition should be in a file that resides in the ``schemaDir``
  369. directory that is specified in the ``package.json`` file of the
  370. extension. The actual file name should use is the part that follows the
  371. package name of extension. So for example, the JupyterLab
  372. ``apputils-extension`` package hosts several plugins:
  373. - ``'@jupyterlab/apputils-extension:menu'``
  374. - ``'@jupyterlab/apputils-extension:palette'``
  375. - ``'@jupyterlab/apputils-extension:settings'``
  376. - ``'@jupyterlab/apputils-extension:themes'``
  377. And in the ``package.json`` for ``@jupyterlab/apputils-extension``, the
  378. ``schemaDir`` field is a directory called ``schema``. Since the
  379. ``themes`` plugin requires a JSON schema, its schema file location is:
  380. ``schema/themes.json``. The plugin's name is used to automatically
  381. associate it with its settings file, so this naming convention is
  382. important. Ensure that the schema files are included in the ``"files"``
  383. metadata in ``package.json``.
  384. See the
  385. `fileeditor-extension <https://github.com/jupyterlab/jupyterlab/tree/master/packages/fileeditor-extension>`__
  386. for another example of an extension that uses settings.
  387. Note: You can override default values of the extension settings by
  388. defining new default values in an ``overrides.json`` file in the
  389. application settings directory. So for example, if you would like
  390. to set the dark theme by default instead of the light one, an
  391. ``overrides.json`` file containing the following lines needs to be
  392. added in the application settings directory (by default this is the
  393. ``share/jupyter/lab/settings`` folder).
  394. .. code:: json
  395. {
  396. "@jupyterlab/apputils-extension:themes": {
  397. "theme": "JupyterLab Dark"
  398. }
  399. }
  400. State Database
  401. ``````````````
  402. The state database can be accessed by importing ``IStateDB`` from
  403. ``@jupyterlab/coreutils`` and adding it to the list of ``requires`` for
  404. a plugin:
  405. .. code:: typescript
  406. const id = 'foo-extension:IFoo';
  407. const IFoo = new Token<IFoo>(id);
  408. interface IFoo {}
  409. class Foo implements IFoo {}
  410. const plugin: JupyterFrontEndPlugin<IFoo> = {
  411. id,
  412. requires: [IStateDB],
  413. provides: IFoo,
  414. activate: (app: JupyterFrontEnd, state: IStateDB): IFoo => {
  415. const foo = new Foo();
  416. const key = `${id}:some-attribute`;
  417. // Load the saved plugin state and apply it once the app
  418. // has finished restoring its former layout.
  419. Promise.all([state.fetch(key), app.restored])
  420. .then(([saved]) => { /* Update `foo` with `saved`. */ });
  421. // Fulfill the plugin contract by returning an `IFoo`.
  422. return foo;
  423. },
  424. autoStart: true
  425. };
  426. Context Menus
  427. ^^^^^^^^^^^^^
  428. JupyterLab has an application-wide context menu available as
  429. ``app.contextMenu``. See the Phosphor
  430. `docs <https://phosphorjs.github.io/phosphor/api/widgets/interfaces/contextmenu.iitemoptions.html>`__
  431. for the item creation options. If you wish to preempt the
  432. application context menu, you can use a 'contextmenu' event listener and
  433. call ``event.stopPropagation`` to prevent the application context menu
  434. handler from being called (it is listening in the bubble phase on the
  435. ``document``). At this point you could show your own Phosphor
  436. `contextMenu <https://phosphorjs.github.io/phosphor/api/widgets/classes/contextmenu.html>`__,
  437. or simply stop propagation and let the system context menu be shown.
  438. This would look something like the following in a ``Widget`` subclass:
  439. .. code:: javascript
  440. // In `onAfterAttach()`
  441. this.node.addEventListener('contextmenu', this);
  442. // In `handleEvent()`
  443. case 'contextmenu':
  444. event.stopPropagation();
  445. .. |dependencies| image:: dependency-graph.svg
  446. Using React
  447. ^^^^^^^^^^^
  448. We also provide support for using :ref:`react` in your JupyterLab
  449. extensions, as well as in the core codebase.
  450. .. _ext-author-companion-packages:
  451. Companion Packages
  452. ^^^^^^^^^^^^^^^^^^
  453. If your extensions depends on the presence of one or more packages in the
  454. kernel, or on a notebook server extension, you can add metadata to indicate
  455. this to the extension manager by adding metadata to your package.json file.
  456. The full options available are::
  457. "jupyterlab": {
  458. "discovery": {
  459. "kernel": [
  460. {
  461. "kernel_spec": {
  462. "language": "<regexp for matching kernel language>",
  463. "display_name": "<regexp for matching kernel display name>" // optional
  464. },
  465. "base": {
  466. "name": "<the name of the kernel package>"
  467. },
  468. "overrides": { // optional
  469. "<manager name, e.g. 'pip'>": {
  470. "name": "<name of kernel package on pip, if it differs from base name>"
  471. }
  472. },
  473. "managers": [ // list of package managers that have your kernel package
  474. "pip",
  475. "conda"
  476. ]
  477. }
  478. ],
  479. "server": {
  480. "base": {
  481. "name": "<the name of the server extension package>"
  482. },
  483. "overrides": { // optional
  484. "<manager name, e.g. 'pip'>": {
  485. "name": "<name of server extension package on pip, if it differs from base name>"
  486. }
  487. },
  488. "managers": [ // list of package managers that have your server extension package
  489. "pip",
  490. "conda"
  491. ]
  492. }
  493. }
  494. }
  495. A typical setup for e.g. a jupyter-widget based package will then be::
  496. "keywords": [
  497. "jupyterlab-extension",
  498. "jupyter",
  499. "widgets",
  500. "jupyterlab"
  501. ],
  502. "jupyterlab": {
  503. "extension": true,
  504. "discovery": {
  505. "kernel": [
  506. {
  507. "kernel_spec": {
  508. "language": "^python",
  509. },
  510. "base": {
  511. "name": "myipywidgetspackage"
  512. },
  513. "managers": [
  514. "pip",
  515. "conda"
  516. ]
  517. }
  518. ]
  519. }
  520. }
  521. Currently supported package managers are:
  522. - ``pip``
  523. - ``conda``