Skip to content

Commit

Permalink
Make ILayoutRestorer optional in the extension tutorial (#11677)
Browse files Browse the repository at this point in the history
* Make `ILayoutRestorer` optional in the extension tutorial

* Fix typos, add note

* Update docs/source/extension/extension_tutorial.rst

Co-authored-by: Frédéric Collonval <fcollonval@users.noreply.github.com>

Co-authored-by: Frédéric Collonval <fcollonval@users.noreply.github.com>
  • Loading branch information
jtpio and fcollonval committed Dec 20, 2021
1 parent a0a9112 commit 5950256
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions docs/source/extension/extension_tutorial.rst
Expand Up @@ -792,30 +792,43 @@ entire list of import statements looks like the following:
import { Widget } from '@lumino/widgets';
Then add the ``ILayoutRestorer`` interface to the ``JupyterFrontEndPlugin``
definition. This addition passes the global ``LayoutRestorer`` as the
definition as ``optional``. This addition passes the global ``LayoutRestorer`` as the
third parameter of the ``activate`` function.
.. code-block:: typescript
:emphasize-lines: 4
:emphasize-lines: 5
const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab_apod',
autoStart: true,
requires: [ICommandPalette, ILayoutRestorer],
requires: [ICommandPalette],
optional: [ILayoutRestorer],
activate: activate
};
Here ``ILayoutRestorer`` is specified as an ``optional`` token, as the corresponding service might
not be available in a customized JupyterLab distribution that does not provide layout restoration
functionalities. Having it ``optional`` make it a nice to have, and enable your extension to be loaded
in more JupyterLab based applications.
.. note::
You can learn more about ``requires`` and ``optional`` in the :ref:`tokens` section
of the Extension Developer Guide.
Finally, rewrite the ``activate`` function so that it:
1. Declares a widget variable, but does not create an instance
immediately.
2. Constructs a ``WidgetTracker`` and tells the ``ILayoutRestorer``
2. Adds the global ``LayoutRestorer`` as the third parameter of the ``activate`` function.
This parameter is declared as ``ILayoutRestorer | null`` since the token is specified as ``optional``.
3. Constructs a ``WidgetTracker`` and tells the ``ILayoutRestorer``
to use it to save/restore panel state.
3. Creates, tracks, shows, and refreshes the widget panel appropriately.
4. Creates, tracks, shows, and refreshes the widget panel appropriately.
.. code-block:: typescript
function activate(app: JupyterFrontEnd, palette: ICommandPalette, restorer: ILayoutRestorer) {
function activate(app: JupyterFrontEnd, palette: ICommandPalette, restorer: ILayoutRestorer | null) {
console.log('JupyterLab extension jupyterlab_apod is activated!');
// Declare a widget variable
Expand Down Expand Up @@ -857,10 +870,12 @@ Finally, rewrite the ``activate`` function so that it:
let tracker = new WidgetTracker<MainAreaWidget<APODWidget>>({
namespace: 'apod'
});
restorer.restore(tracker, {
command,
name: () => 'apod'
});
if (restorer) {
restorer.restore(tracker, {
command,
name: () => 'apod'
});
}
}
Rebuild your extension one last time and refresh your browser tab.
Expand Down

0 comments on commit 5950256

Please sign in to comment.