Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ILayoutRestorer optional in the extension tutorial #11677

Merged
merged 3 commits into from Dec 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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