documents.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .. _documents:
  2. Documents
  3. ---------
  4. JupyterLab can be extended in several ways:
  5. - Extensions (top level): Application extensions extend the
  6. functionality of JupyterLab itself, and we cover them in the
  7. :ref:`developer_extensions`.
  8. - **Document widget extensions (lower level):** Document widget
  9. extensions extend the functionality of document widgets added to the
  10. application, and we cover them in this section.
  11. For this section, the term 'document' refers to any visual thing that
  12. is backed by a file stored on disk (i.e. uses Contents API).
  13. Overview of document architecture
  14. ---------------------------------
  15. A 'document' in JupyterLab is represented by a model instance implementing the `IModel <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.imodel.html>`__ interface. The model interface is intentionally fairly small, and concentrates on representing the data in the document and signaling changes to that data. Each model has an associated `context <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.icontext.html>`__ instance as well. The context for a model is the bridge between the internal data of the document, stored in the model, and the file metadata and operations possible on the file, such as save and revert. Since many objects will need both the context and the model, the context contains a reference to the model as its `.model` attribute.
  16. A single file path can have multiple different models (and hence different contexts) representing the file. For example, a notebook can be opened with a notebook model and with a text model. Different models for the same file path do not directly communicate with each other.
  17. `Document widgets <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.ireadywidget.html>`__ represent a view of a document model. There can be multiple document widgets associated with a single document model, and they naturally stay in sync with each other since they are views on the same underlying data model.
  18. The `Document
  19. Registry <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html>`__
  20. is where document types and factories are registered. Plugins can
  21. require a document registry instance and register their content types
  22. and providers.
  23. The `Document
  24. Manager <http://jupyterlab.github.io/jupyterlab/classes/_docmanager_src_manager_.documentmanager.html>`__
  25. uses the Document Registry to create models and widgets for documents.
  26. The Document Manager is only meant to be accessed by the File Browser
  27. itself.
  28. Document Registry
  29. ~~~~~~~~~~~~~~~~~
  30. *Document widget extensions* in the JupyterLab application can register:
  31. - file types
  32. - model factories for specific file types
  33. - widget factories for specific model factories
  34. - widget extension factories
  35. - file creators
  36. `Widget Factories <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html#addwidgetfactory>`__
  37. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  38. Create a widget for a given file.
  39. *Example*
  40. - The notebook widget factory that creates NotebookPanel widgets.
  41. `Model Factories <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html#addmodelfactory>`__
  42. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  43. Create a model for a given file.
  44. Models are generally differentiated by the contents options used to
  45. fetch the model (e.g. text, base64, notebook).
  46. `Widget Extension Factories <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html#addwidgetextension>`__
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. Adds additional functionality to a widget type. An extension instance is
  49. created for each widget instance, enabling the extension to add
  50. functionality to each widget or observe the widget and/or its context.
  51. *Examples*
  52. - The ipywidgets extension that is created for NotebookPanel widgets.
  53. - Adding a button to the toolbar of each NotebookPanel widget.
  54. `File Types <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html#addfiletype>`__
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. Intended to be used in a "Create New" dialog, providing a list of known
  57. file types.
  58. `File Creators <http://jupyterlab.github.io/jupyterlab/classes/_docregistry_src_registry_.documentregistry.html>`__
  59. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  60. Intended for create quick launch file creators.
  61. The default use will be for the "create new" dropdown in the file
  62. browser, giving list of items that can be created with default options
  63. (e.g. "Python 3 Notebook").
  64. `Document Models <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.imodel.html>`__
  65. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  66. Created by the model factories and passed to widget factories and widget
  67. extension factories. Models are the way in which we interact with the
  68. data of a document. For a simple text file, we typically only use the
  69. ``to/fromString()`` methods. A more complex document like a Notebook
  70. contains more points of interaction like the Notebook metadata.
  71. `Document Contexts <http://jupyterlab.github.io/jupyterlab/interfaces/_docregistry_src_registry_.documentregistry.icontext.html>`__
  72. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  73. Created by the Document Manager and passed to widget factories and
  74. widget extensions. The context contains the model as one of its
  75. properties so that we can pass a single object around.
  76. They are used to provide an abstracted interface to the session and
  77. contents API from ``@jupyterlab/services`` for the given model. They can
  78. be shared between widgets.
  79. The reason for a separate context and model is so that it is easy to
  80. create model factories and the heavy lifting of the context is left to
  81. the Document Manager. Contexts are not meant to be subclassed or
  82. re-implemented. Instead, the contexts are intended to be the glue
  83. between the document model and the wider application.
  84. Document Manager
  85. ~~~~~~~~~~~~~~~~
  86. The *Document Manager* handles:
  87. - document models
  88. - document contexts
  89. The *File Browser* uses the *Document Manager* to open documents and
  90. manage them.