extension_migration.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. .. _extension_migration:
  2. Extension Migration Guide
  3. ================================================
  4. .. _extension_migration_2_3:
  5. JupyterLab 2.x to 3.x
  6. ---------------------
  7. Here are some helpful tips for migrating an extension from JupyterLab 2.x to JupyterLab 3.x.
  8. Upgrading library versions manually
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. To update the extensions so it is compatible with the 3.0 release, update the compatibility
  11. range of the ``@jupyterlab`` dependencies in the ``package.json``. The diff should be similar to:
  12. .. code:: diff
  13. index 6f1562f..3fcdf37 100644
  14. ^^^ a/package.json
  15. +++ b/package.json
  16. "dependencies": {
  17. - "@jupyterlab/application": "^2.0.0",
  18. + "@jupyterlab/application": "^3.0.0",
  19. Upgrading library versions using the upgrade script
  20. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  21. JupyterLab 3.0 provides a script to upgrade an existing extension to use the new extension system and packaging.
  22. First, make sure to update to JupyterLab 3.0 and install ``jupyter-packaging`` and ``cookiecutter``. With ``pip``:
  23. .. code:: bash
  24. pip install jupyterlab -U
  25. pip install jupyter-packaging cookiecutter
  26. Or with ``conda``:
  27. .. code:: bash
  28. conda install -c conda-forge jupyterlab=3 jupyter-packaging cookiecutter
  29. Then at the root folder of the extension, run:
  30. .. code:: bash
  31. python -m jupyterlab.upgrade_extension .
  32. The upgrade script creates the necessary files for packaging the JupyterLab extension as a Python package, such as
  33. ``setup.py`` and ``pyproject.toml``.
  34. The upgrade script also updates the dependencies in ``package.json`` to the ``^3.0.0`` packages. Here is an example diff:
  35. .. code:: diff
  36. index 6f1562f..3fcdf37 100644
  37. ^^^ a/package.json
  38. +++ b/package.json
  39. @@ -29,9 +29,13 @@
  40. "scripts": {
  41. - "build": "tsc",
  42. - "build:labextension": "npm run clean:labextension && mkdirp myextension/labextension && cd myextension/labextension && npm pack ../..",
  43. - "clean": "rimraf lib tsconfig.tsbuildinfo",
  44. + "build": "jlpm run build:lib && jlpm run build:labextension:dev",
  45. + "build:prod": "jlpm run build:lib && jlpm run build:labextension",
  46. + "build:lib": "tsc",
  47. + "build:labextension": "jupyter labextension build .",
  48. + "build:labextension:dev": "jupyter labextension build --development True .",
  49. + "clean": "rimraf lib tsconfig.tsbuildinfo myextension/labextension",
  50. + "clean:all": "jlpm run clean:lib && jlpm run clean:labextension",
  51. "clean:labextension": "rimraf myextension/labextension",
  52. "eslint": "eslint . --ext .ts,.tsx --fix",
  53. "eslint:check": "eslint . --ext .ts,.tsx",
  54. @@ -59,12 +63,12 @@
  55. ]
  56. },
  57. "dependencies": {
  58. - "@jupyterlab/application": "^2.0.0",
  59. - "@jupyterlab/apputils": "^2.0.0",
  60. - "@jupyterlab/observables": "^3.0.0",
  61. + "@jupyterlab/builder": "^3.0.0",
  62. + "@jupyterlab/application": "^3.0.0",
  63. + "@jupyterlab/apputils": "^3.0.0",
  64. + "@jupyterlab/observables": "^3.0.0",
  65. "@lumino/algorithm": "^1.2.3",
  66. "@lumino/commands": "^1.10.1",
  67. "@lumino/disposable": "^1.3.5",
  68. @@ -99,6 +103,13 @@
  69. - "typescript": "~3.8.3"
  70. + "typescript": "~4.0.1"
  71. },
  72. "jupyterlab": {
  73. - "extension": "lib/plugin"
  74. + "extension": "lib/plugin",
  75. + "outputDir": "myextension/labextension/"
  76. }
  77. }
  78. On the diff above, we see that additional development scripts are also added, as they are used by the new extension system workflow.
  79. The diff also shows the new ``@jupyterlab/builder`` as a ``devDependency``.
  80. ``@jupyterlab/builder`` is a package required to build the extension as a federated extension.
  81. It hides away internal dependencies such as ``webpack``, and produces the assets that can then be distributed as part of a Python package.
  82. Extension developers do not need to interact with ``@jupyterlab/builder`` directly, but instead can use the
  83. ``jupyter labextension build`` command. This command is run automatically as part of the ``build`` script
  84. (``jlpm run build``).
  85. For more details about the new file structure and packaging of the extension, check out the extension tutorial: :ref:`extension_tutorial`
  86. Publishing the extension to PyPI and conda-forge
  87. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  88. Starting from JupyterLab 3.0, extensions can be distributed as a Python package.
  89. The extension tutorial provides explanations to package the extension so it can be
  90. published on PyPI and conda forge: :ref:`extension_tutorial_publish`.
  91. .. note::
  92. While publishing to PyPI is the new recommended way for distributing extensions to users,
  93. it is still useful to continue publishing extensions to ``npm`` as well,
  94. so other developers can extend them in their own extensions.
  95. .. _extension_migration_1_2:
  96. JupyterLab 1.x to 2.x
  97. ---------------------
  98. Here are some helpful tips for migrating an extension from JupyterLab 1.x to
  99. JupyterLab 2.x. We will look at two examples of extensions that cover most of
  100. the APIs that extension authors might be using:
  101. - ``@jupyterlab/debugger`` migration pull request:
  102. https://github.com/jupyterlab/debugger/pull/337/files
  103. - ``@jupyterlab/shortcutui`` migration pull request:
  104. https://github.com/jupyterlab/jupyterlab-shortcutui/pull/53/files
  105. Upgrading library versions
  106. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  107. The ``@phosphor/*`` libraries that JupyterLab 1.x uses have been renamed to
  108. ``@lumino/*``. Updating your ``package.json`` is straightforward. The easiest
  109. way to do this is to look in the
  110. `JupyterLab core packages code base <https://github.com/jupyterlab/jupyterlab/tree/master/packages>`__
  111. and to simply adopt the versions of the relevant libraries that are used
  112. there.
  113. .. figure:: images/extension_migration_dependencies_debugger.png
  114. :align: center
  115. :class: jp-screenshot
  116. :alt: Updating the debugger extension's libraries in package.json
  117. Updating the debugger extension's libraries in ``package.json``
  118. .. figure:: images/extension_migration_dependencies_shortcuts.png
  119. :align: center
  120. :class: jp-screenshot
  121. :alt: Updating the shortcuts UI extension's libraries in package.json
  122. Updating the shortcuts UI extension's libraries in ``package.json``
  123. .. tip::
  124. In these examples, note that we are using the ``2.0.0-beta.x`` version of
  125. many libraries. This was to test the extensions against the JupyterLab 2.0
  126. beta release before the final version. For the final release, your
  127. ``package.json`` should depend on version ``^2.0.0`` of these packages.
  128. Migrating from ``@phosphor`` to ``@lumino``
  129. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
  130. The foundational packages used by JupyterLab are now all prefixed with the NPM
  131. namespace ``@lumino`` instead of ``@phosphor``. The APIs for these packages
  132. have not changed. The ``@phosphor`` namespaced imports need to be updated to
  133. the new ``@lumino`` namespaced packages:
  134. .. list-table:: Update from ``@phosphor/...`` to ``@lumino/...``
  135. * - ``@phosphor/application``
  136. - ``@lumino/application``
  137. * - ``@phosphor/collections``
  138. - ``@lumino/collections``
  139. * - ``@phosphor/commands``
  140. - ``@lumino/commands``
  141. * - ``@phosphor/coreutils``
  142. - ``@lumino/coreutils``
  143. * - ``@phosphor/datagrid``
  144. - ``@lumino/datagrid``
  145. * - ``@phosphor/datastore``
  146. - ``@lumino/datastore``
  147. * - ``@phosphor/default-theme``
  148. - ``@lumino/default-theme``
  149. * - ``@phosphor/disposable``
  150. - ``@lumino/disposable``
  151. * - ``@phosphor/domutils``
  152. - ``@lumino/domutils``
  153. * - ``@phosphor/dragdrop``
  154. - ``@lumino/dragdrop``
  155. * - ``@phosphor/keyboard``
  156. - ``@lumino/keyboard``
  157. * - ``@phosphor/messaging``
  158. - ``@lumino/messaging``
  159. * - ``@phosphor/properties``
  160. - ``@lumino/properties``
  161. * - ``@phosphor/signaling``
  162. - ``@lumino/signaling``
  163. * - ``@phosphor/virtualdom``
  164. - ``@lumino/virtualdom``
  165. * - ``@phosphor/widgets``
  166. - ``@lumino/widgets``
  167. .. warning::
  168. ``p-`` prefixed CSS classes, ``data-p-`` attributes and ``p-`` DOM events
  169. are deprecated. They will continue to work until the next major release of
  170. Lumino.
  171. - ``.p-`` CSS classes such as ``.p-Widget`` should be updated to ``.lm-``,
  172. e.g. ``.lm-Widget``
  173. - ``data-p-`` attributes such as ``data-p-dragscroll`` should be updated to
  174. ``data-lm-``, e.g. ``data-lm-dragscroll``
  175. - ``p-`` DOM events such as ``p-dragenter`` should be updated to ``lm-``,
  176. e.g. ``lm-dragenter``
  177. Updating former ``@jupyterlab/coreutils`` imports
  178. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
  179. JupyterLab 2.0 introduces several new packages with classes and tokens that
  180. have been moved out of ``@jupyterlab/coreutils`` into their own packages. These
  181. exports have been moved.
  182. .. tip::
  183. It might be helpful to delete ``node_modules`` and ``yarn.lock`` when
  184. updating these libraries.
  185. ============================ =================================
  186. Export Package
  187. ============================ =================================
  188. ``DataConnector`` ``@jupyterlab/statedb``
  189. ``Debouncer`` ``@lumino/polling``
  190. ``DefaultSchemaValidator`` ``@jupyterlab/settingregistry``
  191. ``IDataConnector`` ``@jupyterlab/statedb``
  192. ``IObjectPool`` ``@jupyterlab/statedb``
  193. ``IPoll`` ``@lumino/polling``
  194. ``IRateLimiter`` ``@lumino/polling``
  195. ``IRestorable`` ``@jupyterlab/statedb``
  196. ``IRestorer`` ``@jupyterlab/statedb``
  197. ``ISchemaValidator`` ``@jupyterlab/settingregistry``
  198. ``ISettingRegistry`` ``@jupyterlab/settingregistry``
  199. ``IStateDB`` ``@jupyterlab/statedb``
  200. ``nbformat`` ``@jupyterlab/nbformat``
  201. ``Poll`` ``@lumino/polling``
  202. ``RateLimiter`` ``@lumino/polling``
  203. ``RestorablePool`` ``@jupyterlab/statedb``
  204. ``SettingRegistry`` ``@jupyterlab/settingregistry``
  205. ``Settings`` ``@jupyterlab/settingregistry``
  206. ``StateDB`` ``@jupyterlab/statedb``
  207. ``Throttler`` ``@lumino/polling``
  208. ============================ =================================
  209. Using ``Session`` and ``SessionContext`` to manage kernel sessions
  210. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  211. .. note::
  212. For full API documentation and examples of how to use
  213. ``@jupyterlab/services``,
  214. `consult the repository <https://github.com/jupyterlab/jupyterlab/tree/master/packages/services#readme>`__.
  215. ``ConsolePanel`` and ``NotebookPanel`` now expose a
  216. ``sessionContext: ISessionContext`` attribute that allows for a uniform way to
  217. interact with kernel sessions.
  218. Any widget that matches the ``interface IDocumentWidget`` has a
  219. ``context: DocumentRegistry.IContext`` attribute with a
  220. ``sessionContext: ISessionContext`` attribute.
  221. For example, consider how the ``@jupyterlab/debugger`` extension's
  222. ``DebuggerService`` updated its ``isAvailable()`` method.
  223. .. figure:: images/extension_migration_session.png
  224. :align: center
  225. :class: jp-screenshot
  226. :alt: Updating the isAvailable method of the debugger service
  227. From the `PR migrating the debugger extension to JupyterLab 2.0 <https://github.com/jupyterlab/debugger/pull/337/files#diff-22ccf3ebb0cb6b300ee90a38b88edff8>`__
  228. .. note::
  229. ``await kernel.ready`` is no longer necessary before the kernel connection
  230. ``kernel`` can be used. Kernel messages will be buffered as needed while a
  231. kernel connection is coming online, so you should be able to use a kernel
  232. connection immediately. If you want to retrieve the kernel info (or if for
  233. some other reason you want to wait until at least one message has returned
  234. from a new kernel connection), you can do ``await kernel.info``.
  235. Using the new icon system and ``LabIcon``
  236. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  237. .. note::
  238. For full API documentation and examples of how to use
  239. the new icon support based on ``LabIcon`` from ``@jupyterlab/ui-components``,
  240. `consult the repository <https://github.com/jupyterlab/jupyterlab/tree/master/packages/ui-components#readme>`__.