ui_helpers.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * ``getPassword`` : request a short password.
  22. All dialogs are built on the standard ``Dialog``. Therefore the helper functions each return
  23. a ``Promise`` resolving in a ``Dialog.IResult`` object.
  24. .. code:: typescript
  25. // Request a boolean
  26. InputDialog.getBoolean({ title: 'Check or not?' }).then(value => {
  27. console.log('boolean ' + value.value);
  28. });
  29. // Request a choice from a list
  30. InputDialog.getItem({
  31. title: 'Pick a choice',
  32. items: ['1', '2']
  33. }).then(value => {
  34. console.log('item ' + value.value);
  35. });
  36. // Request a choice from a list or specify your own choice
  37. InputDialog.getItem({
  38. title: 'Pick a choice or write your own',
  39. items: ['1', '2'],
  40. editable: true
  41. }).then(value => {
  42. console.log('editable item ' + value.value);
  43. });
  44. // Request a number
  45. InputDialog.getNumber({ title: 'How much?' }).then(value => {
  46. console.log('number ' + value.value);
  47. });
  48. // Request a text
  49. InputDialog.getText({ title: 'Provide a text' }).then(value => {
  50. console.log('text ' + value.value);
  51. });
  52. // Request a text
  53. InputDialog.getPassword({ title: 'Input password' }).then(value => {
  54. console.log('A password was input');
  55. });
  56. File Dialogs
  57. ^^^^^^^^^^^^
  58. Two helper functions to ask a user to open a file or a directory are
  59. available in the ``filebrowser`` package under the namespace ``FileDialog``.
  60. Here is an example to request a file.
  61. .. code:: typescript
  62. const dialog = FileDialog.getOpenFiles({
  63. manager, // IDocumentManager
  64. filter: model => model.type == 'notebook' // optional (model: Contents.IModel) => boolean
  65. });
  66. const result = await dialog;
  67. if(result.button.accept){
  68. let files = result.value;
  69. }
  70. And for a folder.
  71. .. code:: typescript
  72. const dialog = FileDialog.getExistingDirectory({
  73. manager // IDocumentManager
  74. });
  75. const result = await dialog;
  76. if(result.button.accept){
  77. let folders = result.value;
  78. }
  79. .. note:: The document manager can be obtained in a plugin by
  80. requesting ``IFileBrowserFactory`` token. The ``manager`` will be accessed through
  81. ``factory.defaultBrowser.model.manager``.