extension_points.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. .. _developer-extension-points:
  2. Common Extension Points
  3. =======================
  4. Most of the component parts of JupyterLab are designed to be extensible,
  5. and they provide services that can be requested in extensions via tokens.
  6. A list of common core tokens that extension authors can request is given in :ref:`core_tokens`.
  7. Following the list of core tokens is a guide for using some of JupyterLab's most commonly-used extension points.
  8. However, it is not an exhaustive account of how to extend the application components,
  9. and more detailed descriptions of their public APIs may be found in the
  10. `JupyterLab <../api/index.html>`__ and
  11. `Lumino <http://jupyterlab.github.io/lumino/index.html>`__ API documentation.
  12. .. contents:: Table of contents
  13. :local:
  14. :depth: 1
  15. .. _core_tokens:
  16. Core Tokens
  17. -----------
  18. The core packages of JupyterLab provide many services for plugins. The tokens
  19. for these services are listed here, along with short descriptions of when you
  20. might want to use the services in your extensions.
  21. - ``@jupyterlab/application:IConnectionLost``: A service for invoking the dialog shown
  22. when JupyterLab has lost its connection to the server. Use this if, for some reason,
  23. you want to bring up the "connection lost" dialog under new circumstances.
  24. - ``@jupyterlab/application:IInfo``: A service providing metadata about the current
  25. application, including disabled extensions and whether dev mode is enabled.
  26. - ``@jupyterlab/application:IPaths``: A service providing information about various
  27. URLs and server paths for the current application. Use this service if you want to
  28. assemble URLs to use the JupyterLab REST API.
  29. - ``@jupyterlab/application:ILabStatus``: A service for interacting with the application busy/dirty
  30. status. Use this if you want to set the application "busy" favicon, or to set
  31. the application "dirty" status, which asks the user for confirmation before leaving the application page.
  32. - ``@jupyterlab/application:ILabShell``: A service for interacting with the JupyterLab shell.
  33. The top-level ``application`` object also has a reference to the shell, but it has a restricted
  34. interface in order to be agnostic to different shell implementations on the application.
  35. Use this to get more detailed information about currently active widgets and layout state.
  36. - ``@jupyterlab/application:ILayoutRestorer``: A service providing application layout
  37. restoration functionality. Use this to have your activities restored across
  38. page loads.
  39. - ``@jupyterlab/application:IMimeDocumentTracker``: A widget tracker for documents
  40. rendered using a mime renderer extension. Use this if you want to list and interact
  41. with documents rendered by such extensions.
  42. - ``@jupyterlab/application:IRouter``: The URL router used by the application.
  43. Use this to add custom URL-routing for your extension (e.g., to invoke
  44. a command if the user navigates to a sub-path).
  45. - ``@jupyterlab/apputils:ICommandPalette``: A service for the application command palette
  46. in the left panel. Use this to add commands to the palette.
  47. - ``@jupyterlab/apputils:ISplashScreen``: A service for the splash screen for the application.
  48. Use this if you want to show the splash screen for your own purposes.
  49. - ``@jupyterlab/apputils:IThemeManager``: A service for the theme manager for the application. This is used primarily in theme extensions to register new themes.
  50. - ``@jupyterlab/apputils:IWindowResolver``: A service for a window resolver for the
  51. application. JupyterLab workspaces are given a name, which are determined using
  52. the window resolver. Require this if you want to use the name of the current workspace.
  53. - ``@jupyterlab/codeeditor:IEditorServices``: A service for the text editor provider
  54. for the application. Use this to create new text editors and host them in your
  55. UI elements.
  56. - ``@jupyterlab/completer:ICompletionManager``: A service for the completion manager
  57. for the application. Use this to allow your extension to invoke a completer.
  58. - ``@jupyterlab/console:IConsoleTracker``: A widget tracker for code consoles.
  59. Use this if you want to be able to iterate over and interact with code consoles
  60. created by the application.
  61. - ``@jupyterlab/console:IContentFactory``: A factory object that creates new code
  62. consoles. Use this if you want to create and host code consoles in your own UI elements.
  63. - ``@jupyterlab/docmanager:IDocumentManager``: A service for the manager for all
  64. documents used by the application. Use this if you want to open and close documents,
  65. create and delete files, and otherwise interact with the file system.
  66. - ``@jupyterlab/documentsearch:ISearchProviderRegistry``: A service for a registry of search
  67. providers for the application. Plugins can register their UI elements with this registry
  68. to provide find/replace support.
  69. - ``@jupyterlab/filebrowser:IFileBrowserFactory``: A factory object that creates file browsers.
  70. Use this if you want to create your own file browser (e.g., for a custom storage backend),
  71. or to interact with other file browsers that have been created by extensions.
  72. - ``@jupyterlab/fileeditor:IEditorTracker``: A widget tracker for file editors.
  73. Use this if you want to be able to iterate over and interact with file editors
  74. created by the application.
  75. - ``@jupyterlab/htmlviewer:IHTMLViewerTracker``: A widget tracker for rendered HTML documents.
  76. Use this if you want to be able to iterate over and interact with HTML documents
  77. viewed by the application.
  78. - ``@jupyterlab/imageviewer:IImageTracker``: A widget tracker for images.
  79. Use this if you want to be able to iterate over and interact with images
  80. viewed by the application.
  81. - ``@jupyterlab/inspector:IInspector``: A service for adding contextual help to widgets (visible using "Show Contextual Help" from the Help menu).
  82. Use this to hook into the contextual help system in your extension.
  83. - ``@jupyterlab/launcher:ILauncher``: A service for the application activity launcher.
  84. Use this to add your extension activities to the launcher panel.
  85. - ``@jupyterlab/mainmenu:IMainMenu``: A service for the main menu bar for the application.
  86. Use this if you want to add your own menu items or provide implementations for standardized menu items for specific activities.
  87. - ``@jupyterlab/markdownviewer:IMarkdownViewerTracker``: A widget tracker for markdown
  88. document viewers. Use this if you want to iterate over and interact with rendered markdown documents.
  89. - ``@jupyterlab/notebook:INotebookTools``: A service for the ``Notebook Tools`` panel in the
  90. right sidebar. Use this to add your own functionality to the panel.
  91. - ``@jupyterlab/notebook:IContentFactory``: A factory object that creates new notebooks.
  92. Use this if you want to create and host notebooks in your own UI elements.
  93. - ``@jupyterlab/notebook:INotebookTracker``: A widget tracker for notebooks.
  94. Use this if you want to be able to iterate over and interact with notebooks
  95. created by the application.
  96. - ``@jupyterlab/rendermime:IRenderMimeRegistry``: A service for the rendermime registry
  97. for the application. Use this to create renderers for various mime-types in your extension. Many times it will be easier to create a `mime renderer extension <#mime-renderer-extensions>`__ rather than using this service directly.
  98. - ``@jupyterlab/rendermime:ILatexTypesetter``: A service for the LaTeX typesetter for the
  99. application. Use this if you want to typeset math in your extension.
  100. - ``@jupyterlab/settingeditor:ISettingEditorTracker``: A widget tracker for setting editors.
  101. Use this if you want to be able to iterate over and interact with setting editors
  102. created by the application.
  103. - ``@jupyterlab/settingregistry:ISettingRegistry``: A service for the JupyterLab settings system.
  104. Use this if you want to store settings for your application.
  105. See :ref:`schemaDir` for more information.
  106. - ``@jupyterlab/statedb:IStateDB``: A service for the JupyterLab state database.
  107. Use this if you want to store data that will persist across page loads.
  108. See `state database <#state-database>`__ for more information.
  109. - ``@jupyterlab/statusbar:IStatusBar``: A service for the status bar on the application.
  110. Use this if you want to add new status bar items.
  111. - ``@jupyterlab/terminal:ITerminalTracker``: A widget tracker for terminals.
  112. Use this if you want to be able to iterate over and interact with terminals
  113. created by the application.
  114. - ``@jupyterlab/tooltip:ITooltipManager``: A service for the tooltip manager for the application.
  115. Use this to allow your extension to invoke a tooltip.
  116. - ``@jupyterlab/vdom:IVDOMTracker``: A widget tracker for virtual DOM (VDOM) documents.
  117. Use this to iterate over and interact with VDOM document instances created by the application.
  118. Commands
  119. --------
  120. Add a Command to the Command Registry
  121. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  122. Perhaps the most common way to add functionality to JupyterLab is via commands.
  123. These are lightweight objects that include a function to execute combined with
  124. additional metadata, including how they are labeled and when they are to be enabled.
  125. The application has a single command registry, keyed by string command IDs,
  126. to which you can add your custom commands.
  127. The commands added to the command registry can then be used to populate
  128. several of the JupyterLab user interface elements, including menus and the launcher.
  129. Here is a sample block of code that adds a command to the application (given by ``app``):
  130. .. code:: typescript
  131. const commandID = 'my-command';
  132. const toggled = false;
  133. app.commands.addCommand(commandID, {
  134. label: 'My Cool Command',
  135. isEnabled: true,
  136. isVisible: true,
  137. isToggled: () => toggled,
  138. iconClass: 'some-css-icon-class',
  139. execute: () => {
  140. console.log(`Executed ${commandID}`);
  141. toggled = !toggled;
  142. });
  143. This example adds a new command, which, when triggered, calls the ``execute`` function.
  144. ``isEnabled`` indicates whether the command is enabled, and determines whether renderings of it are greyed out.
  145. ``isToggled`` indicates whether to render a check mark next to the command.
  146. ``isVisible`` indicates whether to render the command at all.
  147. ``iconClass`` specifies a CSS class which can be used to display an icon next to renderings of the command.
  148. Each of ``isEnabled``, ``isToggled``, and ``isVisible`` can be either
  149. a boolean value or a function that returns a boolean value, in case you want
  150. to do some logic in order to determine those conditions.
  151. Likewise, each of ``label`` and ``iconClass`` can be either
  152. a string value or a function that returns a string value.
  153. There are several more options which can be passed into the command registry when
  154. adding new commands. These are documented
  155. `here <http://jupyterlab.github.io/lumino/commands/interfaces/commandregistry.icommandoptions.html>`__.
  156. After a command has been added to the application command registry
  157. you can add them to various places in the application user interface,
  158. where they will be rendered using the metadata you provided.
  159. For example, you can add a button to the Notebook toolbar to run the command with the ``CommandToolbarButtonComponent``.
  160. Add a Command to the Command Palette
  161. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  162. In order to add an existing, registered command to the command palette, you need to request the
  163. ``ICommandPalette`` token in your extension.
  164. Here is an example showing how to add a command to the command palette (given by ``palette``):
  165. .. code:: typescript
  166. palette.addItem({
  167. command: commandID,
  168. category: 'my-category',
  169. args: {}
  170. });
  171. The command ID is the same ID that you used when registering the command.
  172. You must also provide a ``category``, which determines the subheading of
  173. the command palette in which to render the command.
  174. It can be a preexisting category (e.g., ``'notebook'``), or a new one of your own choosing.
  175. The ``args`` are a JSON object that will be passed into your command's functions at render/execute time.
  176. You can use these to customize the behavior of your command depending on how it is invoked.
  177. For instance, you can pass in ``args: { isPalette: true }``.
  178. Your command ``label`` function can then check the ``args`` it is provided for ``isPalette``,
  179. and return a different label in that case.
  180. This can be useful to make a single command flexible enough to work in multiple contexts.
  181. Context Menu
  182. ------------
  183. JupyterLab has an application-wide context menu available as
  184. ``app.contextMenu``. The application context menu is shown when the user right-clicks,
  185. and is populated with menu items that are most relevant to the thing that the user clicked.
  186. The context menu system determines which items to show based on
  187. `CSS selectors <https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors>`__.
  188. It propagates up the DOM tree and tests whether a given HTML element
  189. matches the CSS selector provided by a given command.
  190. Here is an example showing how to add a command to the application context
  191. menu. See the Lumino `docs
  192. <https://jupyterlab.github.io/lumino/widgets/interfaces/contextmenu.iitemoptions.html>`__
  193. for the item creation options.
  194. .. code:: typescript
  195. app.contextMenu.addItem({
  196. command: commandID,
  197. selector: '.jp-Notebook'
  198. })
  199. In this example, the command indicated by ``commandID`` is shown whenever the user
  200. right-clicks on a DOM element matching ``.jp-Notebook`` (that is to say, a notebook).
  201. The selector can be any valid CSS selector, and may target your own UI elements, or existing ones.
  202. A list of CSS selectors currently used by context menu commands is given in :ref:`css-selectors`.
  203. If you don't want JupyterLab's custom context menu to appear for your element, because you have
  204. your own right click behavior that you want to trigger, you can add the `data-jp-suppress-context-menu` data attribute
  205. to any node to have it and its children not trigger it.
  206. For example, if you are building a custom React element, it would look like this:
  207. .. code::
  208. function MyElement(props: {}) {
  209. return (
  210. <div data-jp-suppress-context-menu>
  211. <p>Hi</p>
  212. <p onContextMenu={() => {console.log("right clicked")}}>There</p>
  213. </div>
  214. )
  215. }
  216. Alternatively, you can use a 'contextmenu' event listener and
  217. call ``event.stopPropagation`` to prevent the application context menu
  218. handler from being called (it is listening in the bubble phase on the
  219. ``document``). At this point you could show your own Lumino
  220. `contextMenu <https://jupyterlab.github.io/lumino/widgets/classes/contextmenu.html>`__,
  221. or simply stop propagation and let the system context menu be shown.
  222. This would look something like the following in a ``Widget`` subclass:
  223. .. code:: javascript
  224. // In `onAfterAttach()`
  225. this.node.addEventListener('contextmenu', this);
  226. // In `handleEvent()`
  227. case 'contextmenu':
  228. event.stopPropagation();
  229. .. _copy_shareable_link:
  230. Copy Shareable Link
  231. -------------------
  232. The file browser provides a context menu item "Copy Shareable Link". The
  233. desired behavior will vary by deployment and the users it serves. The file
  234. browser supports overriding the behavior of this item.
  235. .. code:: typescript
  236. import {
  237. IFileBrowserFactory
  238. } from '@jupyterlab/filebrowser';
  239. import {
  240. JupyterFrontEnd, JupyterFrontEndPlugin
  241. } from '@jupyterlab/application';
  242. const shareFile: JupyterFrontEndPlugin<void> = {
  243. activate: activateShareFile,
  244. id: commandID,
  245. requires: [IFileBrowserFactory],
  246. autoStart: true
  247. };
  248. function activateShareFile(
  249. app: JupyterFrontEnd,
  250. factory: IFileBrowserFactory
  251. ): void {
  252. const { commands } = app;
  253. const { tracker } = factory;
  254. commands.addCommand('filebrowser:share-main', {
  255. execute: () => {
  256. const widget = tracker.currentWidget;
  257. if (!widget) {
  258. return;
  259. }
  260. const path = encodeURI(widget.selectedItems().next().path);
  261. // Do something with path.
  262. },
  263. isVisible: () =>
  264. tracker.currentWidget &&
  265. toArray(tracker.currentWidget.selectedItems()).length === 1,
  266. iconClass: 'jp-MaterialIcon jp-LinkIcon',
  267. label: 'Copy Shareable Link'
  268. });
  269. }
  270. Note that an extension providing a replacement plugin like this must either :ref:`automatically disable <disabledExtensions>` the replaced core plugin or the user must disable the core plugin manually:
  271. .. code-block:: bash
  272. jupyter labextension disable @jupyterlab/filebrowser-extension:share-file
  273. Icons
  274. -----
  275. See :ref:`ui_components`
  276. Keyboard Shortcuts
  277. ------------------
  278. There are two ways of adding keyboard shortcuts in JupyterLab.
  279. If you don't want the shortcuts to be user-configurable,
  280. you can add them directly to the application command registry:
  281. .. code:: typescript
  282. app.commands.addKeyBinding({
  283. command: commandID,
  284. args: {},
  285. keys: ['Accel T'],
  286. selector: '.jp-Notebook'
  287. });
  288. In this example ``my-command`` command is mapped to ``Accel T``,
  289. where ``Accel`` corresponds to ``Cmd`` on a Mac and ``Ctrl`` on Windows and Linux computers.
  290. The behavior for keyboard shortcuts is very similar to that of the context menu:
  291. the shortcut handler propagates up the DOM tree from the focused element
  292. and tests each element against the registered selectors. If a match is found,
  293. then that command is executed with the provided ``args``.
  294. Full documentation for the options for ``addKeyBinding`` can be found
  295. `here <http://jupyterlab.github.io/lumino/commands/interfaces/commandregistry.ikeybindingoptions.html>`__.
  296. JupyterLab also provides integration with its settings system for keyboard shortcuts.
  297. Your extension can provide a settings schema with a ``jupyter.lab.shortcuts`` key,
  298. declaring default keyboard shortcuts for a command:
  299. .. code:: json
  300. {
  301. "jupyter.lab.shortcuts": [
  302. {
  303. "command": "my-command",
  304. "keys": ["Accel T"],
  305. "selector": ".jp-mod-searchable"
  306. }
  307. ]
  308. }
  309. Shortcuts added to the settings system will be editable by users.
  310. Launcher
  311. --------
  312. As with menus, keyboard shortcuts, and the command palette, new items can be added
  313. to the application launcher via commands.
  314. You can do this by requesting the ``ILauncher`` token in your extension:
  315. .. code:: typescript
  316. launcher.add({
  317. command: commandID,
  318. category: 'Other',
  319. rank: 0
  320. });
  321. In addition to providing a command ID, you also provide a category in which to put your item,
  322. (e.g. 'Notebook', or 'Other'), as well as a rank to determine its position among other items.
  323. .. _shell:
  324. Jupyter Front-End Shell
  325. -----------------------
  326. The Jupyter front-end
  327. `shell <../api/interfaces/application.jupyterfrontend.ishell.html>`__
  328. is used to add and interact with content in the application. The ``IShell``
  329. interface provides an ``add()`` method for adding widgets to the application.
  330. In JupyterLab, the application shell consists of:
  331. - A ``top`` area for things like top-level toolbars and information.
  332. - A ``menu`` area for top-level menus, which is collapsed into the ``top`` area in multiple-document mode and put below it in single-document mode.
  333. - ``left`` and ``right`` sidebar areas for collapsible content.
  334. - A ``main`` work area for user activity.
  335. - A ``bottom`` area for things like status bars.
  336. - A ``header`` area for custom elements.
  337. Left/Right Areas
  338. ^^^^^^^^^^^^^^^^
  339. The left and right sidebar areas of JupyterLab are intended to host more persistent user interface
  340. elements than the main area. That being said, extension authors are free to add whatever
  341. components they like to these areas. The outermost-level of the object that you add is expected
  342. to be a Lumino ``Widget``, but that can host any content you like (such as React components).
  343. As an example, the following code executes an application command to a terminal widget
  344. and then adds the terminal to the right area:
  345. .. code:: typescript
  346. app.commands
  347. .execute('terminal:create-new')
  348. .then((terminal: WidgetModuleType.Terminal) => {
  349. app.shell.add(terminal, 'right');
  350. });
  351. You can use a numeric rank to control the ordering of the left and right tabs:
  352. .. code:: typescript
  353. app.shell.add(terminal, 'left', {rank: 600});
  354. The recommended ranges for this rank are:
  355. * 0-500: reserved for first-party JupyterLab extensions.
  356. * 501-899: reserved for third-party extensions.
  357. * 900: The default rank if none is specified.
  358. * 1000: The JupyterLab extension manager.
  359. .. _mainmenu:
  360. Main Menu
  361. ---------
  362. There are three main ways to extend JupyterLab's main menu.
  363. 1. You can add your own menu to the menu bar.
  364. 2. You can add new commands to the existing menus.
  365. 3. You can register your extension with one of the existing semantic menu items.
  366. In all three cases, you should request the ``IMainMenu`` token for your extension.
  367. Adding a New Menu
  368. ^^^^^^^^^^^^^^^^^
  369. To add a new menu to the menu bar, you need to create a new
  370. `Lumino menu <https://jupyterlab.github.io/lumino/widgets/classes/menu.html>`__.
  371. You can then add commands to the menu in a similar way to the command palette,
  372. and add that menu to the main menu bar:
  373. .. code:: typescript
  374. const menu = new Menu({ commands: app.commands });
  375. menu.addItem({
  376. command: commandID,
  377. args: {},
  378. });
  379. mainMenu.addMenu(menu, { rank: 40 });
  380. As with the command palette, you can optionally pass in ``args`` to customize the
  381. rendering and execution behavior of the command in the menu context.
  382. Adding a New Command to an Existing Menu
  383. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  384. In many cases you will want to add your commands to the existing JupyterLab menus
  385. rather than creating a separate menu for your extension.
  386. Because the top-level JupyterLab menus are shared among many extensions,
  387. the API for adding items is slightly different.
  388. In this case, you provide a list of commands and a rank,
  389. and these commands will be displayed together in a separate group within an existing menu.
  390. For instance, to add a command group with ``firstCommandID`` and ``secondCommandID``
  391. to the File menu, you would do the following:
  392. .. code:: typescript
  393. mainMenu.fileMenu.addGroup([
  394. {
  395. command: firstCommandID,
  396. },
  397. {
  398. command: secondCommandID,
  399. }
  400. ], 40 /* rank */);
  401. Registering a Semantic Menu Item
  402. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  403. There are some commands in the JupyterLab menu system that are considered
  404. common and important enough that they are treated differently.
  405. For instance, we anticipate that many activities may want to provide a command
  406. to close themselves and perform some cleanup operation (like closing a console and shutting down its kernel).
  407. Rather than having a proliferation of similar menu items for this common operation
  408. of "closing-and-cleanup", we provide a single command that can adapt itself to this use case,
  409. which we term a "semantic menu item".
  410. For this example, it is the File Menu ``closeAndCleaners`` set.
  411. Here is an example of using the ``closeAndCleaners`` semantic menu item:
  412. .. code:: typescript
  413. mainMenu.fileMenu.closeAndCleaners.add({
  414. tracker,
  415. action: 'Shutdown',
  416. name: 'My Activity',
  417. closeAndCleanup: current => {
  418. current.close();
  419. return current.shutdown();
  420. }
  421. });
  422. In this example, ``tracker`` is a :ref:`widget-tracker`, which allows the menu
  423. item to determine whether to delegate the menu command to your activity,
  424. ``name`` is a name given to your activity in the menu label,
  425. ``action`` is a verb given to the cleanup operation in the menu label,
  426. and ``closeAndCleanup`` is the actual function that performs the cleanup operation.
  427. So if the current application activity is held in the ``tracker``,
  428. then the menu item will show ``Shutdown My Activity``, and delegate to the
  429. ``closeAndCleanup`` function that was provided.
  430. More examples for how to register semantic menu items are found throughout the JupyterLab code base.
  431. The available semantic menu items are:
  432. - ``IEditMenu.IUndoer``: an activity that knows how to undo and redo.
  433. - ``IEditMenu.IClearer``: an activity that knows how to clear its content.
  434. - ``IEditMenu.IGoToLiner``: an activity that knows how to jump to a given line.
  435. - ``IFileMenu.ICloseAndCleaner``: an activity that knows how to close and clean up after itself.
  436. - ``IFileMenu.IConsoleCreator``: an activity that knows how to create an attached code console for itself.
  437. - ``IHelpMenu.IKernelUser``: an activity that knows how to get a related kernel session.
  438. - ``IKernelMenu.IKernelUser``: an activity that can perform various kernel-related operations.
  439. - ``IRunMenu.ICodeRunner``: an activity that can run code from its content.
  440. - ``IViewMenu.IEditorViewer``: an activity that knows how to set various view-related options on a text editor that it owns.
  441. Status Bar
  442. ----------
  443. JupyterLab's status bar is intended to show small pieces of contextual information.
  444. Like the left and right areas, it only expects a Lumino ``Widget``,
  445. which might contain any kind of content. Since the status bar has limited space,
  446. you should endeavor to only add small widgets to it.
  447. The following example shows how to place a status item that displays the current
  448. "busy" status for the application. This information is available from the ``ILabStatus``
  449. token, which we reference by a variable named ``labStatus``.
  450. We place the ``statusWidget`` in the middle of the status bar.
  451. When the ``labStatus`` busy state changes, we update the text content of the
  452. ``statusWidget`` to reflect that.
  453. .. code:: typescript
  454. const statusWidget = new Widget();
  455. labStatus.busySignal.connect(() => {
  456. statusWidget.node.textContent = labStatus.isBusy ? 'Busy' : 'Idle';
  457. });
  458. statusBar.registerStatusItem('lab-status', {
  459. align: 'middle',
  460. item: statusWidget
  461. });
  462. .. _widget-tracker:
  463. Widget Tracker
  464. --------------
  465. Often extensions will want to interact with documents and activities created by other extensions.
  466. For instance, an extension may want to inject some text into a notebook cell,
  467. or set a custom keymap, or close all documents of a certain type.
  468. Actions like these are typically done by widget trackers.
  469. Extensions keep track of instances of their activities in ``WidgetTrackers``,
  470. which are then provided as tokens so that other extensions may request them.
  471. For instance, if you want to interact with notebooks, you should request the ``INotebookTracker`` token.
  472. You can then use this tracker to iterate over, filter, and search all open notebooks.
  473. You can also use it to be notified via signals when notebooks are added and removed from the tracker.
  474. Widget tracker tokens are provided for many activities in JupyterLab, including
  475. notebooks, consoles, text files, mime documents, and terminals.
  476. If you are adding your own activities to JupyterLab, you might consider providing
  477. a ``WidgetTracker`` token of your own, so that other extensions can make use of it.
  478. State Database
  479. --------------
  480. The state database can be accessed by importing ``IStateDB`` from
  481. ``@jupyterlab/statedb`` and adding it to the list of ``requires`` for
  482. a plugin:
  483. .. code:: typescript
  484. const id = 'foo-extension:IFoo';
  485. const IFoo = new Token<IFoo>(id);
  486. interface IFoo {}
  487. class Foo implements IFoo {}
  488. const plugin: JupyterFrontEndPlugin<IFoo> = {
  489. id,
  490. autoStart: true,
  491. requires: [IStateDB],
  492. provides: IFoo,
  493. activate: (app: JupyterFrontEnd, state: IStateDB): IFoo => {
  494. const foo = new Foo();
  495. const key = `${id}:some-attribute`;
  496. // Load the saved plugin state and apply it once the app
  497. // has finished restoring its former layout.
  498. Promise.all([state.fetch(key), app.restored])
  499. .then(([saved]) => { /* Update `foo` with `saved`. */ });
  500. // Fulfill the plugin contract by returning an `IFoo`.
  501. return foo;
  502. }
  503. };