ui_helpers.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. User Interface Helpers
  2. ----------------------
  3. JupyterLab comes with helpers to show or request simple information from a user.
  4. Those speed up development and ensure a common look and feel.
  5. Dialogs
  6. ~~~~~~~
  7. Message Dialogs
  8. '''''''''''''''
  9. Helper functions to show a message to the user are available in the ``apputils`` package.
  10. These dialogs return a ``Promise`` resolving when the user dismisses the dialog.
  11. There is one helper:
  12. * ``showErrorMessage`` : show an error message dialog.
  13. Input Dialogs
  14. '''''''''''''
  15. Helper functions to request a single input from the user are available in the ``apputils``
  16. package within the ``InputDialog`` namespace. There are four helpers:
  17. * ``getBoolean`` : request a boolean through a checkbox.
  18. * ``getItem`` : request a item from a list; the list may be editable.
  19. * ``getNumber`` : request a number; if the user input is not a valid number, NaN is returned.
  20. * ``getText`` : request a short text.
  21. All dialogs are built on the standard ``Dialog``. Therefore the helper functions each return
  22. a ``Promise`` resolving in a ``Dialog.IResult`` object.
  23. .. code:: typescript
  24. // Request a boolean
  25. InputDialog.getBoolean({ title: 'Check or not?' }).then(value => {
  26. console.log('boolean ' + value.value);
  27. });
  28. // Request a choice from a list
  29. InputDialog.getItem({
  30. title: 'Pick a choice',
  31. items: ['1', '2']
  32. }).then(value => {
  33. console.log('item ' + value.value);
  34. });
  35. // Request a choice from a list or specify your own choice
  36. InputDialog.getItem({
  37. title: 'Pick a choice or write your own',
  38. items: ['1', '2'],
  39. editable: true
  40. }).then(value => {
  41. console.log('editable item ' + value.value);
  42. });
  43. // Request a number
  44. InputDialog.getNumber({ title: 'How much?' }).then(value => {
  45. console.log('number ' + value.value);
  46. });
  47. // Request a text
  48. InputDialog.getText({ title: 'Provide a text' }).then(value => {
  49. console.log('text ' + value.value);
  50. });
  51. File Dialogs
  52. ''''''''''''
  53. Two helper functions to ask a user to open a file or a directory are
  54. available in the ``filebrowser`` package under the namespace ``FileDialog``.
  55. Here is an example to request a file.
  56. .. code:: typescript
  57. const dialog = FileDialog.getOpenFiles({
  58. iconRegistry, // IIconRegistry
  59. manager, // IDocumentManager
  60. filter: model => model.type == 'notebook' // optional (model: Contents.IModel) => boolean
  61. });
  62. const result = await dialog;
  63. if(result.button.accept){
  64. let files = result.value;
  65. }
  66. And for a folder.
  67. .. code:: typescript
  68. const dialog = FileDialog.getExistingDirectory({
  69. iconRegistry, // IIconRegistry
  70. manager // IDocumentManager
  71. });
  72. const result = await dialog;
  73. if(result.button.accept){
  74. let folders = result.value;
  75. }
  76. .. note:: The document manager and the icon registry can be obtained in a plugin by
  77. requesting ``IFileBrowserFactory`` token. The ``manager`` will be accessed through
  78. ``factory.defaultBrowser.model.manager`` and the ``iconRegistry`` through
  79. ``factory.defaultBrowser.model.iconRegistry``.