documents.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  16. `IModel <../api/interfaces/docregistry.documentregistry.imodel.html>`__ interface.
  17. The model interface is intentionally fairly small, and concentrates on representing
  18. the data in the document and signaling changes to that data. Each model has an
  19. associated `context <../api/interfaces/docregistry.documentregistry.icontext.html>`__
  20. instance as well. The context for a model is the bridge between the internal data
  21. of the document, stored in the model, and the file metadata and operations possible
  22. on the file, such as save and revert. Since many objects will need both the context
  23. and the model, the context contains a reference to the model as its `.model` attribute.
  24. A single file path can have multiple different models (and hence different contexts)
  25. representing the file. For example, a notebook can be opened with a notebook model
  26. and with a text model. Different models for the same file path do not directly
  27. communicate with each other.
  28. Models contain an instance of `ModelDB <../api/classes/observables.modeldb-1.html>`__
  29. that acts as data storage for the model's content. In JupyterLab 3.1, we introduced
  30. the package ``@jupyterlab/shared-models`` to swap ``ModelDB`` as a data storage
  31. to make 'documents' collaborative. We implemented these shared models using
  32. `Yjs <https://docs.yjs.dev>`_, a high-performance CRDT for building collaborative applications
  33. that automatically sync.
  34. At the moment, models contain both a ``ModelDB`` and a ``Shared Model`` instance, so it is
  35. possible to access ``ModelDB`` yet.
  36. `Document widgets <../api/classes/docregistry.documentregistry-1.html>`__ represent
  37. a view of a document model. There can be multiple document widgets associated with
  38. a single document model, and they naturally stay in sync with each other since they
  39. are views on the same underlying data model.
  40. `Shared Models <../api/interfaces/shared_models.ishareddocument.html>`__ are models
  41. using Yjs’ shared types as a data structures instead of ``ModelDB``.
  42. The `Document Registry <../api/classes/docregistry.documentregistry-1.html>`__
  43. is where document types and factories are registered. Plugins can
  44. require a document registry instance and register their content types
  45. and providers.
  46. The `Document Manager <../api/classes/docmanager.documentmanager-1.html>`__
  47. uses the Document Registry to create models and widgets for documents.
  48. The Document Manager handles the lifecycle of documents for the application.
  49. The `Document Provider <../api/classes/docprovider.websocketproviderwithlocks-1.html>`__
  50. is a WebSocket provider that syncs documents through a new end-point (``api/yjs``)
  51. in the JupyterLab server. `Providers <https://docs.yjs.dev/ecosystem/connection-provider>`_
  52. abstract Yjs from the network technology your application uses. They sync Yjs
  53. documents through a communication protocol or a database. Most providers have
  54. in common that they use the concept of room names to connect Yjs documents.
  55. Document Registry
  56. -----------------
  57. *Document widget extensions* in the JupyterLab application can register:
  58. - file types
  59. - model factories for specific file types
  60. - widget factories for specific model factories
  61. - widget extension factories
  62. `Widget Factories <../api/classes/docregistry.documentregistry-1.html#addwidgetfactory>`__
  63. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  64. Create a widget for a given file.
  65. *Example*
  66. - The notebook widget factory that creates NotebookPanel widgets.
  67. `Model Factories <../api/classes/docregistry.documentregistry-1.html#addmodelfactory>`__
  68. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  69. Create a model for a given file.
  70. Models are generally differentiated by the contents options used to
  71. fetch the model (e.g. text, base64, notebook).
  72. `Widget Extension Factories <../api/classes/docregistry.documentregistry-1.html#addwidgetextension>`__
  73. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  74. Adds additional functionality to a widget type. An extension instance is
  75. created for each widget instance, enabling the extension to add
  76. functionality to each widget or observe the widget and/or its context.
  77. *Examples*
  78. - The ipywidgets extension that is created for NotebookPanel widgets.
  79. - Adding a button to the toolbar of each NotebookPanel widget.
  80. `File Types <../api/classes/docregistry.documentregistry-1.html#addfiletype>`__
  81. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  82. `Document Models <../api/interfaces/docregistry.documentregistry.imodel.html>`__
  83. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  84. Created by the model factories and passed to widget factories and widget
  85. extension factories. Models are the way in which we interact with the
  86. data of a document. For a simple text file, we typically only use the
  87. ``to/fromString()`` methods. A more complex document like a Notebook
  88. contains more points of interaction like the Notebook metadata.
  89. `Document Contexts <../api/interfaces/docregistry.documentregistry.icontext.html>`__
  90. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  91. Created by the Document Manager and passed to widget factories and
  92. widget extensions. The context contains the model as one of its
  93. properties so that we can pass a single object around.
  94. They are used to provide an abstracted interface to the session and
  95. Contents API from ``@jupyterlab/services`` for the given model. They can
  96. be shared between widgets.
  97. The reason for a separate context and model is so that it is easy to
  98. create model factories and the heavy lifting of the context is left to
  99. the Document Manager. Contexts are not meant to be subclassed or
  100. re-implemented. Instead, the contexts are intended to be the glue
  101. between the document model and the wider application.
  102. Shared Models
  103. -------------
  104. The shared-models package contains an `ISharedNotebook
  105. <../api/interfaces/shared_models.isharednotebook.html>`_ and an `ISharedFile
  106. <../api/interfaces/shared_models.isharedfile.html>`_ which are the abstract
  107. interfaces to work against if you want to manipulate a notebook or a file.
  108. These models wrap a `Yjs document (Y.Doc) <https://docs.yjs.dev/api/y.doc>`_ which represents
  109. a shared document between clients and hold multiple shared objects. They enable you
  110. to share different `data types like text, Array, Map or set
  111. <https://docs.yjs.dev/getting-started/working-with-shared-types>`_, to make different
  112. types of collaborative applications.
  113. In addition, a shared model has an `Awareness <https://docs.yjs.dev/getting-started/adding-awareness>`_
  114. attribute. This attribute is linked to the *Y.Doc* which means there is one *Awareness* object per document and is
  115. used for sharing cursor locations and presence information.
  116. Document Manager
  117. ----------------
  118. The *Document Manager* handles:
  119. - document models
  120. - document contexts
  121. The *File Browser* uses the *Document Manager* to open documents and
  122. manage them.