File: ui_helpers.rst

package info (click to toggle)
jupyterlab 4.0.11%2Bds1%2B~cs11.25.27-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 43,496 kB
  • sloc: javascript: 18,395; python: 8,932; sh: 399; makefile: 95; perl: 33; xml: 1
file content (423 lines) | stat: -rw-r--r-- 11,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
.. Copyright (c) Jupyter Development Team.
.. Distributed under the terms of the Modified BSD License.

User Interface Helpers
======================

JupyterLab comes with helpers to show or request simple information from a user.
Those speed up development and ensure a common look and feel.

Dialogs
-------

Generic Dialog
^^^^^^^^^^^^^^

To display a generic dialog, use ``showDialog`` function from ``@jupyterlab/apputils``.

The options available are:

.. code:: typescript

   showDialog({
     title: 'Dialog title', // Can be text or a react element
     body: 'Dialog body', // Can be text, a widget or a react element
     host: document.body, // Parent element for rendering the dialog
     buttons: [ // List of buttons
      {
        label: 'my button', // Button label
        caption: 'my button title', // Button title
        className: 'my-button', // Additional button CSS class
        accept: true, // Whether this button will discard or accept the dialog
        displayType: 'default' // applies 'default' or 'warn' styles
      }
     ],
     checkbox: { // Optional checkbox in the dialog footer
       label: 'check me', // Checkbox label
       caption: 'check me I\'magic', // Checkbox title
       className: 'my-checkbox', // Additional checkbox CSS class
       checked: true, // Default checkbox state
     },
     defaultButton: 0, // Index of the default button
     focusNodeSelector: '.my-input', // Selector for focussing an input element when dialog opens
     hasClose: false, // Whether to display a close button or not
     renderer: undefined // To define customized dialog structure
   })

.. note::

   If no options are specified, the dialog will only contain *OK* and *Cancel* buttons.

Message Dialogs
^^^^^^^^^^^^^^^

Helper functions to show a message to the user are available in the ``apputils`` package.
These dialogs return a ``Promise`` resolving when the user dismisses the dialog.

There is one helper:

* ``showErrorMessage`` : show an error message dialog.


Input Dialogs
^^^^^^^^^^^^^

Helper functions to request a single input from the user are available in the ``apputils``
package within the ``InputDialog`` namespace. There are four helpers:

* ``getBoolean`` : request a boolean through a checkbox.
* ``getItem`` : request a item from a list; the list may be editable.
* ``getNumber`` : request a number; if the user input is not a valid number, NaN is returned.
* ``getText`` : request a short text.
* ``getPassword`` : request a short password.

All dialogs are built on the standard ``Dialog``. Therefore the helper functions each return
a ``Promise`` resolving in a ``Dialog.IResult`` object.

.. code:: typescript

    // Request a boolean
    InputDialog.getBoolean({ title: 'Check or not?' }).then(value => {
      console.log('boolean ' + value.value);
    });

    // Request a choice from a list
    InputDialog.getItem({
      title: 'Pick a choice',
      items: ['1', '2']
    }).then(value => {
      console.log('item ' + value.value);
    });

    // Request a choice from a list or specify your own choice
    InputDialog.getItem({
      title: 'Pick a choice or write your own',
      items: ['1', '2'],
      editable: true
    }).then(value => {
      console.log('editable item ' + value.value);
    });

    // Request a number
    InputDialog.getNumber({ title: 'How much?' }).then(value => {
      console.log('number ' + value.value);
    });

    // Request a text
    InputDialog.getText({ title: 'Provide a text' }).then(value => {
      console.log('text ' + value.value);
    });

    // Request a text
    InputDialog.getPassword({ title: 'Input password' }).then(value => {
      console.log('A password was input');
    });


File Dialogs
^^^^^^^^^^^^

Two helper functions to ask a user to open a file or a directory are
available in the ``filebrowser`` package under the namespace ``FileDialog``.

Here is an example to request a file.

.. code:: typescript

    const dialog = FileDialog.getOpenFiles({
      manager, // IDocumentManager
      filter: model => model.type == 'notebook' // optional (model: Contents.IModel) => boolean
    });

    const result = await dialog;

    if(result.button.accept){
      let files = result.value;
    }

And for a folder.

.. code:: typescript

    const dialog = FileDialog.getExistingDirectory({
      manager // IDocumentManager
    });

    const result = await dialog;

    if(result.button.accept){
      let folders = result.value;
    }

.. note:: The document manager can be obtained in a plugin by
    requesting ``IFileBrowserFactory`` token. The ``manager`` will be accessed through
    ``factory.defaultBrowser.model.manager``.

Notifications
-------------

JupyterLab has a notifications manager that can add, update or dismiss notifications. That feature
is provided by the ``@jupyterlab/apputils`` package.

.. warning::

  It is a good practice to limit the number of notifications sent to respect the user's focus.
  Therefore by default, the notification won't be displayed to the user. But the status bar will
  indicate that a new notification arrived. So the user can click on the indicator to see all
  notifications.

  Try adding a button `Do not show me again` for recurrent notifications to allow users to quickly
  filter notifications that matters for them.

A notification is described by the following element:

.. code:: typescript

   {
     /**
      * Notification message
      *
      * ### Notes
      * Message can only be plain text with a maximum length of 140 characters.
      */
     message: string;
     /**
      * Notification type
      */
     type?:  'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default';
     /**
      * Notification options
      */
     options?: {
       /**
        * Autoclosing behavior - false (not closing automatically)
        * or number (time in milliseconds before hiding the notification)
        *
        * Set to zero if you want the notification to be retained in the notification
        * center but not displayed as toast. This is the default behavior.
        */
       autoClose?: number | false;
       /**
        * List of associated actions
        */
       actions?: Array<IAction>;
       /**
        * Data associated with a notification
        */
       data?: T;
     };
   }

At creation, a notification will receive an unique identifier.

Actions can be linked to a notification but the interface depends on how the notification
is handled.

There are two ways of interacting with notifications: through an API or through commands. The only
difference is that actions linked to a notification can have an arbitrary callback when using the API.
But only a command can be set as an action when using the command call for creating a notification.

Using the API
^^^^^^^^^^^^^

To create notification, you need to provide a message and you can use the following helpers
to set the type automatically (or use ``notify`` to set the type manually):

.. code:: typescript

  /**
   * Helper function to emit an error notification.
   */
  Notification.error(message: string, options?: IOptions): string;

  /**
   * Helper function to emit an info notification.
   */
  Notification.info(message: string, options?: IOptions): string;

  /**
   * Helper function to emit a success notification.
   */
  Notification.success(message: string, options?: IOptions): string;

  /**
   * Helper function to emit a warning notification.
   */
  Notification.warning(message: string, options?: IOptions): string;

  /**
   * Helper function to emit a in-progress notification. Then
   * it will update it with a error or success notification
   * depending on the promise resolution.
   */
  Notification.promise(
    promise: Promise,
    {
      pending: { message: string, options?: IOptions },
      /**
       * If not set `options.data` will be set to the promise result.
       */
      success: { message: (result, data) => string, options?: IOptions },
      /**
       * If not set `options.data` will be set to the promise rejection error.
       */
      error: { message: (reason, data) => string, options?: IOptions }
    }
  ): string;

  /**
   * Helper function to emit a notification.
   */
  Notification.emit(
    message: string,
    type: 'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default' = 'default',
    options?: IOptions
  ): string;

When using the API, an action is defined by:

.. code:: typescript

  {
    /**
     * The action label.
     *
     * This should be a short description.
     */
    label: string;
    /**
     * Callback function to trigger
     *
     * ### Notes
     * By default execution of the callback will close the toast
     * and dismiss the notification. You can prevent this by calling
     * `event.preventDefault()` in the callback.
     */
    callback: (event: MouseEvent) => void;
    /**
     * The action caption.
     *
     * This can be a longer description of the action.
     */
    caption?: string;
    /**
     * The action display type.
     *
     * This will be used to modify the action button style.
     */
    displayType?: 'default' | 'accent' | 'warn' | 'link';
  }

You can update a notification using:

.. code:: typescript

  Notification.update({
    id: string;
    message: string;
    type?:  'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default';
    autoClose?: number | false;
    actions?: Array<IAction>;
    data?: ReadonlyJsonValue;
  }): boolean;

.. note::

   Once updated the notification will be moved at the begin of the notification stack.

And you can dismiss a notification (if you provide an ``id``) or all
notifications using:

.. code:: typescript

  Notification.dismiss(id?: string): void;

.. note::

  Dismissing a notification will remove it from the list of notifications without
  knowing if the user has seen it or not. Therefore it is recommended to not
  dismiss a notification.

Using commands
^^^^^^^^^^^^^^

There are three commands available.

``'apputils:notify'`` to create a notification:

.. code:: typescript

  commands.execute('apputils:notify', {
     message: string;
     type?: 'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default';
     options?: {
       autoClose?: number | false;
       actions?: Array<IAction>;
       data?: T;
     };
  });

The result is the notification unique identifier.

An action is defined by:

.. code:: typescript

  {
    /**
     * The action label.
     *
     * This should be a short description.
     */
    label: string;
    /**
     * Callback command id to trigger
     */
    commandId: string;
    /**
     * Command arguments
     */
    args?: ReadonlyJsonObject;
    /**
     * The action caption.
     *
     * This can be a longer description of the action.
     */
    caption?: string;
    /**
     * The action display type.
     *
     * This will be used to modify the action button style.
     */
    displayType?: 'default' | 'accent' | 'warn' | 'link';
  }

``'apputils:update-notification'`` to update a notification:

.. code:: typescript

   commands.execute('apputils:update-notification', {
     id: string;
     message: string;
     type?: 'info' | 'in-progress' | 'success' | 'warning' | 'error' | 'default';
     autoClose?: number | false;
     actions?: Array<IAction>;
     data?: T;
   });

The result is a boolean indicating if the update was successful. In particular,
updating an absent notification will fail.

``'apputils:dismiss-notification'`` to dismiss a notification:

.. code:: typescript

   commands.execute('apputils:dismiss-notification', {
     id: string;
   });

.. note::

  Dismissing a notification will remove it from the list of notifications without
  knowing if the user has seen it or not. Therefore it is recommended to not
  dismiss a notification.