extension_migration.rst 13 KB

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