extension_tutorial.rst 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. .. _extension_tutorial:
  2. Extension Tutorial
  3. ==================
  4. JupyterLab extensions add features to the user experience. This page
  5. describes how to create one type of extension, an *application plugin*,
  6. that:
  7. - Adds a "Random `Astronomy Picture <https://apod.nasa.gov/apod/astropix.html>`__" command to the
  8. *command palette* sidebar
  9. - Fetches the image and metadata when activated
  10. - Shows the image and metadata in a tab panel
  11. By working through this tutorial, you'll learn:
  12. - How to set up an extension development environment from scratch on a
  13. Linux or OSX machine. (You'll need to modify the commands slightly if you are on Windows.)
  14. - How to start an extension project from
  15. `jupyterlab/extension-cookiecutter-ts <https://github.com/jupyterlab/extension-cookiecutter-ts>`__
  16. - How to iteratively code, build, and load your extension in JupyterLab
  17. - How to version control your work with git
  18. - How to release your extension for others to enjoy
  19. .. figure:: images/extension_tutorial_complete.png
  20. :align: center
  21. :class: jp-screenshot
  22. :alt: The completed extension, showing the Astronomy Picture of the Day for 24 Jul 2015.
  23. The completed extension, showing the `Astronomy Picture of the Day for 24 Jul 2015 <https://apod.nasa.gov/apod/ap150724.html>`__.
  24. Sound like fun? Excellent. Here we go!
  25. Set up a development environment
  26. --------------------------------
  27. Install conda using miniconda
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. Start by installing miniconda, following
  30. `Conda's installation documentation <https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html>`__.
  31. .. _install-nodejs-jupyterlab-etc-in-a-conda-environment:
  32. Install NodeJS, JupyterLab, etc. in a conda environment
  33. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  34. Next create a conda environment that includes:
  35. 1. the latest release of JupyterLab
  36. 2. `cookiecutter <https://github.com/audreyr/cookiecutter>`__, the tool
  37. you'll use to bootstrap your extension project structure (this is a Python tool
  38. which we'll install using conda below).
  39. 3. `NodeJS <https://nodejs.org>`__, the JavaScript runtime you'll use to
  40. compile the web assets (e.g., TypeScript, CSS) for your extension
  41. 4. `git <https://git-scm.com>`__, a version control system you'll use to
  42. take snapshots of your work as you progress through this tutorial
  43. It's a best practice to leave the root conda environment (i.e., the environment created
  44. by the miniconda installer) untouched and install your project-specific
  45. dependencies in a named conda environment. Run this command to create a
  46. new environment named ``jupyterlab-ext``.
  47. .. code:: bash
  48. conda create -n jupyterlab-ext --override-channels --strict-channel-priority -c conda-forge -c nodefaults jupyterlab=3 cookiecutter nodejs jupyter-packaging git
  49. Now activate the new environment so that all further commands you run
  50. work out of that environment.
  51. .. code:: bash
  52. conda activate jupyterlab-ext
  53. Note: You'll need to run the command above in each new terminal you open
  54. before you can work with the tools you installed in the
  55. ``jupyterlab-ext`` environment.
  56. Create a repository
  57. -------------------
  58. Create a new repository for your extension (see, for example, the
  59. `GitHub instructions <https://docs.github.com/en/get-started/quickstart/create-a-repo>`__. This is an
  60. optional step, but highly recommended if you want to share your
  61. extension.
  62. Create an extension project
  63. ---------------------------
  64. Initialize the project from a cookiecutter
  65. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  66. Next use cookiecutter to create a new project for your extension.
  67. This will create a new folder for your extension in your current directory.
  68. .. code:: bash
  69. cookiecutter https://github.com/jupyterlab/extension-cookiecutter-ts
  70. When prompted, enter values like the following for all of the cookiecutter
  71. prompts (``apod`` stands for Astronomy Picture of the Day, the NASA service we
  72. are using to fetch pictures).
  73. ::
  74. Select kind:
  75. 1 - frontend
  76. 2 - server
  77. 3 - theme
  78. Choose from 1, 2, 3 [1]: 1
  79. author_name []: Your Name
  80. author_email []: your@name.org
  81. labextension_name [myextension]: jupyterlab_apod
  82. python_name [myextension]: jupyterlab_apod
  83. project_short_description [A JupyterLab extension.]: Show a random NASA Astronomy Picture of the Day in a JupyterLab panel
  84. has_settings [n]: n
  85. has_binder [n]: y
  86. repository [https://github.com/github_username/myextension]: https://github.com/github_username/jupyterlab_apod
  87. Note: if not using a repository, leave the repository field blank. You can come
  88. back and edit the repository field in the ``package.json`` file later.
  89. Change to the directory the cookiecutter created and list the files.
  90. .. code:: bash
  91. cd jupyterlab_apod
  92. ls
  93. You should see a list like the following.
  94. ::
  95. binder CHANGELOG.md install.json jupyterlab_apod LICENSE MANIFEST.in package.json
  96. pyproject.toml README.md RELEASE.md setup.py src style tsconfig.json
  97. Commit what you have to git
  98. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  99. Run the following commands in your ``jupyterlab_apod`` folder to
  100. initialize it as a git repository and commit the current code.
  101. .. code:: bash
  102. git init
  103. git add .
  104. git commit -m 'Seed apod project from cookiecutter'
  105. Note: This step is not technically necessary, but it is good practice to
  106. track changes in version control system in case you need to rollback to
  107. an earlier version or want to collaborate with others. You
  108. can compare your work throughout this tutorial with the commits in a
  109. reference version of ``jupyterlab_apod`` on GitHub at
  110. https://github.com/jupyterlab/jupyterlab_apod.
  111. Build and install the extension for development
  112. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  113. Your new extension project has enough code in it to see it working in your
  114. JupyterLab. Run the following commands to install the initial project
  115. dependencies and install the extension into the JupyterLab environment.
  116. .. code:: bash
  117. pip install -ve .
  118. The above command copies the frontend part of the extension into JupyterLab.
  119. We can run this ``pip install`` command again every time we make a change to
  120. copy the change into JupyterLab. Even better, we can use
  121. the ``develop`` command to create a symbolic link from JupyterLab to our
  122. source directory. This means our changes are automatically available in
  123. JupyterLab:
  124. .. code:: bash
  125. jupyter labextension develop --overwrite .
  126. .. note::
  127. On Windows, symbolic links can be activated on Windows 10 for Python version 3.8 or higher
  128. by activating the 'Developer Mode'. That may not be allowed by your administrators.
  129. See `Activate Developer Mode on Windows <https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development>`__
  130. for instructions.
  131. See the initial extension in action
  132. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  133. After the install completes, open a second terminal. Run these commands to
  134. activate the ``jupyterlab-ext`` environment and start JupyterLab in your
  135. default web browser.
  136. .. code:: bash
  137. conda activate jupyterlab-ext
  138. jupyter lab
  139. In that browser window, open the JavaScript console
  140. by following the instructions for your browser:
  141. - `Accessing the DevTools in Google
  142. Chrome <https://developer.chrome.com/devtools#access>`__
  143. - `Opening the Web Console in
  144. Firefox <https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Opening_the_Web_Console>`__
  145. After you reload the page with the console open, you should see a message that says
  146. ``JupyterLab extension jupyterlab_apod is activated!`` in the console.
  147. If you do, congratulations, you're ready to start modifying the extension!
  148. If not, go back make sure you didn't miss a step, and `reach
  149. out <https://github.com/jupyterlab/jupyterlab/blob/3.4.x/README.md#getting-help>`__ if you're stuck.
  150. Note: Leave the terminal running the ``jupyter lab`` command open and running
  151. JupyterLab to see the effects of changes below.
  152. Add an Astronomy Picture of the Day widget
  153. ------------------------------------------
  154. Show an empty panel
  155. ^^^^^^^^^^^^^^^^^^^
  156. The *command palette* is the primary view of all commands available to
  157. you in JupyterLab. For your first addition, you're going to add a
  158. *Random Astronomy Picture* command to the palette and get it to show an *Astronomy Picture*
  159. tab panel when invoked.
  160. Fire up your favorite text editor and open the ``src/index.ts`` file in your
  161. extension project. Change the import at the top of the file to get a reference
  162. to the command palette interface and the `JupyterFrontEnd` instance.
  163. .. code:: typescript
  164. import {
  165. JupyterFrontEnd,
  166. JupyterFrontEndPlugin
  167. } from '@jupyterlab/application';
  168. import { ICommandPalette } from '@jupyterlab/apputils';
  169. Locate the ``extension`` object of type ``JupyterFrontEndPlugin``. Change the
  170. definition so that it reads like so:
  171. .. code:: typescript
  172. /**
  173. * Initialization data for the jupyterlab_apod extension.
  174. */
  175. const extension: JupyterFrontEndPlugin<void> = {
  176. id: 'jupyterlab-apod',
  177. autoStart: true,
  178. requires: [ICommandPalette],
  179. activate: (app: JupyterFrontEnd, palette: ICommandPalette) => {
  180. console.log('JupyterLab extension jupyterlab_apod is activated!');
  181. console.log('ICommandPalette:', palette);
  182. }
  183. };
  184. The ``requires`` attribute states that your plugin needs an object that
  185. implements the ``ICommandPalette`` interface when it starts. JupyterLab
  186. will pass an instance of ``ICommandPalette`` as the second parameter of
  187. ``activate`` in order to satisfy this requirement. Defining
  188. ``palette: ICommandPalette`` makes this instance available to your code
  189. in that function. The second ``console.log`` line exists only so that
  190. you can immediately check that your changes work.
  191. Now you will need to install these dependencies. Run the following commands in the
  192. repository root folder to install the dependencies and save them to your
  193. `package.json`:
  194. .. code:: bash
  195. jlpm add @jupyterlab/apputils
  196. jlpm add @jupyterlab/application
  197. Finally, run the following to rebuild your extension.
  198. .. code:: bash
  199. jlpm run build
  200. .. note::
  201. This tutorial uses ``jlpm`` to install Javascript packages and
  202. run build commands, which is JupyterLab's bundled
  203. version of ``yarn``. If you prefer, you can use another Javascript
  204. package manager like ``npm`` or ``yarn`` itself.
  205. After the extension build finishes, return to the browser tab that opened when
  206. you started JupyterLab. Refresh it and look in the console. You should see the
  207. same activation message as before, plus the new message about the
  208. ICommandPalette instance you just added. If you don't, check the output of the
  209. build command for errors and correct your code.
  210. ::
  211. JupyterLab extension jupyterlab_apod is activated!
  212. ICommandPalette: Palette {_palette: CommandPalette}
  213. Note that we had to run ``jlpm run build`` in order for the bundle to
  214. update. This command does two things: compiles the TypeScript files in `src/`
  215. into JavaScript files in ``lib/`` (``jlpm run build``), then bundles the
  216. JavaScript files in ``lib/`` into a JupyterLab extension in
  217. ``jupyterlab_apod/static`` (``jlpm run build:extension``). If you wish to avoid
  218. running ``jlpm run build`` after each change, you can open a third terminal,
  219. activate the ``jupyterlab-ext`` environment, and run the ``jlpm run watch``
  220. command from your extension directory, which will automatically compile the
  221. TypeScript files as they are changed and saved.
  222. Now return to your editor. Modify the imports at the top of the file to add a few more imports:
  223. .. code:: typescript
  224. import { ICommandPalette, MainAreaWidget } from '@jupyterlab/apputils';
  225. import { Widget } from '@lumino/widgets';
  226. Install this new dependency as well:
  227. .. code:: bash
  228. jlpm add @lumino/widgets
  229. Then modify the ``activate`` function again so that it has the following
  230. code:
  231. .. code-block:: typescript
  232. activate: (app: JupyterFrontEnd, palette: ICommandPalette) => {
  233. console.log('JupyterLab extension jupyterlab_apod is activated!');
  234. // Create a blank content widget inside of a MainAreaWidget
  235. const content = new Widget();
  236. const widget = new MainAreaWidget({ content });
  237. widget.id = 'apod-jupyterlab';
  238. widget.title.label = 'Astronomy Picture';
  239. widget.title.closable = true;
  240. // Add an application command
  241. const command: string = 'apod:open';
  242. app.commands.addCommand(command, {
  243. label: 'Random Astronomy Picture',
  244. execute: () => {
  245. if (!widget.isAttached) {
  246. // Attach the widget to the main work area if it's not there
  247. app.shell.add(widget, 'main');
  248. }
  249. // Activate the widget
  250. app.shell.activateById(widget.id);
  251. }
  252. });
  253. // Add the command to the palette.
  254. palette.addItem({ command, category: 'Tutorial' });
  255. }
  256. The first new block of code creates a ``MainAreaWidget`` instance with an
  257. empty content ``Widget`` as its child. It also assigns the main area widget a
  258. unique ID, gives it a label that will appear as its tab title, and makes the
  259. tab closable by the user. The second block of code adds a new command with id
  260. ``apod:open`` and label *Random Astronomy Picture* to JupyterLab. When the
  261. command executes, it attaches the widget to the main display area if it is not
  262. already present and then makes it the active tab. The last new line of code
  263. uses the command id to add the command to the command palette in a section
  264. called *Tutorial*.
  265. Build your extension again using ``jlpm run build`` (unless you are using
  266. ``jlpm run watch`` already) and refresh the browser tab. Open the command
  267. palette by clicking on *Commands* from the View menu or using the keyboard
  268. shortcut ``Command/Ctrl Shift C`` and type *Astronomy* in the search box. Your
  269. *Random Astronomy Picture* command should appear. Click it or select it with
  270. the keyboard and press *Enter*. You should see a new, blank panel appear with
  271. the tab title *Astronomy Picture*. Click the *x* on the tab to close it and
  272. activate the command again. The tab should reappear. Finally, click one of the
  273. launcher tabs so that the *Astronomy Picture* panel is still open but no
  274. longer active. Now run the *Random Astronomy Picture* command one more time.
  275. The single *Astronomy Picture* tab should come to the foreground.
  276. .. figure:: images/extension_tutorial_empty.png
  277. :align: center
  278. :class: jp-screenshot
  279. :alt: The in-progress extension, showing a blank panel.
  280. The in-progress extension, showing a blank panel.
  281. If your widget is not behaving, compare your code with the reference
  282. project state at the `01-show-a-panel
  283. tag <https://github.com/jupyterlab/jupyterlab_apod/tree/3.0-01-show-a-panel>`__.
  284. Once you've got everything working properly, git commit your changes and
  285. carry on.
  286. .. code-block:: bash
  287. git add package.json src/index.ts
  288. git commit -m 'Show Astronomy Picture command in palette'
  289. Show a picture in the panel
  290. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  291. You now have an empty panel. It's time to add a picture to it. Go back to
  292. your code editor. Add the following code below the lines that create a
  293. ``MainAreaWidget`` instance and above the lines that define the command.
  294. .. code-block:: typescript
  295. // Add an image element to the content
  296. let img = document.createElement('img');
  297. content.node.appendChild(img);
  298. // Get a random date string in YYYY-MM-DD format
  299. function randomDate() {
  300. const start = new Date(2010, 1, 1);
  301. const end = new Date();
  302. const randomDate = new Date(start.getTime() + Math.random()*(end.getTime() - start.getTime()));
  303. return randomDate.toISOString().slice(0, 10);
  304. }
  305. // Fetch info about a random picture
  306. const response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=${randomDate()}`);
  307. const data = await response.json() as APODResponse;
  308. if (data.media_type === 'image') {
  309. // Populate the image
  310. img.src = data.url;
  311. img.title = data.title;
  312. } else {
  313. console.log('Random APOD was not a picture.');
  314. }
  315. The first two lines create a new HTML ``<img>`` element and add it to
  316. the widget DOM node. The next lines define a function get a random date in the form ``YYYY-MM-DD`` format, and then the function is used to make a request using the HTML
  317. `fetch <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch>`__
  318. API that returns information about the Astronomy Picture of the Day for that date. Finally, we set the
  319. image source and title attributes based on the response.
  320. Now define the ``APODResponse`` type that was introduced in the code above. Put
  321. this definition just under the imports at the top of the file.
  322. .. code-block:: typescript
  323. interface APODResponse {
  324. copyright: string;
  325. date: string;
  326. explanation: string;
  327. media_type: 'video' | 'image';
  328. title: string;
  329. url: string;
  330. };
  331. And update the ``activate`` method to be ``async`` since we are now using
  332. ``await`` in the method body.
  333. .. code-block:: typescript
  334. activate: async (app: JupyterFrontEnd, palette: ICommandPalette) =>
  335. .. note::
  336. If you are new to JavaScript / TypeScript and want to learn more about ``async``, ``await``,
  337. and ``Promises``, you can check out the following `tutorial on MDN <https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises>`_
  338. Be sure to also refer to the other resources in the
  339. `See Also <https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises#see_also>`_
  340. section for more materials.
  341. Rebuild your extension if necessary (``jlpm run build``), refresh your browser
  342. tab, and run the *Random Astronomy Picture* command again. You should now see a
  343. picture in the panel when it opens (if that random date had a picture and not a
  344. video).
  345. .. figure:: images/extension_tutorial_single.png
  346. :align: center
  347. :class: jp-screenshot
  348. The in-progress extension, showing the `Astronomy Picture of the Day for 19 Jan 2014 <https://apod.nasa.gov/apod/ap140119.html>`__.
  349. Note that the image is not centered in the panel nor does the panel
  350. scroll if the image is larger than the panel area. Also note that the
  351. image does not update no matter how many times you close and reopen the
  352. panel. You'll address both of these problems in the upcoming sections.
  353. If you don't see a image at all, compare your code with the
  354. `02-show-an-image
  355. tag <https://github.com/jupyterlab/jupyterlab_apod/tree/3.0-02-show-an-image>`__
  356. in the reference project. When it's working, make another git commit.
  357. .. code:: bash
  358. git add src/index.ts
  359. git commit -m 'Show a picture in the panel'
  360. Improve the widget behavior
  361. ---------------------------
  362. Center the image, add attribution, and error messaging
  363. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  364. Open ``style/base.css`` in our extension project directory for editing.
  365. Add the following lines to it.
  366. .. code-block:: css
  367. .my-apodWidget {
  368. display: flex;
  369. flex-direction: column;
  370. align-items: center;
  371. overflow: auto;
  372. }
  373. This CSS stacks content vertically within the widget panel and lets the panel
  374. scroll when the content overflows. This CSS file is included on the page
  375. automatically by JupyterLab because the ``package.json`` file has a ``style``
  376. field pointing to it. In general, you should import all of your styles into a
  377. single CSS file, such as this ``index.css`` file, and put the path to that CSS
  378. file in the ``package.json`` file ``style`` field.
  379. Return to the ``index.ts`` file. Modify the ``activate``
  380. function to apply the CSS classes, the copyright information, and error handling
  381. for the API response.
  382. The beginning of the function should read like the following:
  383. .. code-block:: typescript
  384. :emphasize-lines: 6,16-17,28-50
  385. activate: async (app: JupyterFrontEnd, palette: ICommandPalette) => {
  386. console.log('JupyterLab extension jupyterlab_apod is activated!');
  387. // Create a blank content widget inside of a MainAreaWidget
  388. const content = new Widget();
  389. content.addClass('my-apodWidget'); // new line
  390. const widget = new MainAreaWidget({content});
  391. widget.id = 'apod-jupyterlab';
  392. widget.title.label = 'Astronomy Picture';
  393. widget.title.closable = true;
  394. // Add an image element to the content
  395. let img = document.createElement('img');
  396. content.node.appendChild(img);
  397. let summary = document.createElement('p');
  398. content.node.appendChild(summary);
  399. // Get a random date string in YYYY-MM-DD format
  400. function randomDate() {
  401. const start = new Date(2010, 1, 1);
  402. const end = new Date();
  403. const randomDate = new Date(start.getTime() + Math.random()*(end.getTime() - start.getTime()));
  404. return randomDate.toISOString().slice(0, 10);
  405. }
  406. // Fetch info about a random picture
  407. const response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=${randomDate()}`);
  408. if (!response.ok) {
  409. const data = await response.json();
  410. if (data.error) {
  411. summary.innerText = data.error.message;
  412. } else {
  413. summary.innerText = response.statusText;
  414. }
  415. } else {
  416. const data = await response.json() as APODResponse;
  417. if (data.media_type === 'image') {
  418. // Populate the image
  419. img.src = data.url;
  420. img.title = data.title;
  421. summary.innerText = data.title;
  422. if (data.copyright) {
  423. summary.innerText += ` (Copyright ${data.copyright})`;
  424. }
  425. } else {
  426. summary.innerText = 'Random APOD fetched was not an image.';
  427. }
  428. }
  429. // Keep all the remaining command lines the same
  430. // as before from here down ...
  431. Build your extension if necessary (``jlpm run build``) and refresh your
  432. JupyterLab browser tab. Invoke the *Random Astronomy Picture* command and
  433. confirm the image is centered with the copyright information below it. Resize
  434. the browser window or the panel so that the image is larger than the
  435. available area. Make sure you can scroll the panel over the entire area
  436. of the image.
  437. If anything is not working correctly, compare your code with the reference project
  438. `03-style-and-attribute
  439. tag <https://github.com/jupyterlab/jupyterlab_apod/tree/3.0-03-style-and-attribute>`__.
  440. When everything is working as expected, make another commit.
  441. .. code:: bash
  442. git add style/index.css src/index.ts
  443. git commit -m 'Add styling, attribution, error handling'
  444. Show a new image on demand
  445. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  446. The ``activate`` function has grown quite long, and there's still more
  447. functionality to add. Let's refactor the code into two separate
  448. parts:
  449. 1. An ``APODWidget`` that encapsulates the Astronomy Picture panel elements,
  450. configuration, and soon-to-be-added update behavior
  451. 2. An ``activate`` function that adds the widget instance to the UI and
  452. decide when the picture should refresh
  453. Start by refactoring the widget code into the new ``APODWidget`` class.
  454. Add the following additional import to the top of the file.
  455. .. code-block:: typescript
  456. import { Message } from '@lumino/messaging';
  457. Install this dependency:
  458. .. code:: bash
  459. jlpm add @lumino/messaging
  460. Then add the class just below the definition of ``APODResponse`` in the ``index.ts``
  461. file.
  462. .. code-block:: typescript
  463. class APODWidget extends Widget {
  464. /**
  465. * Construct a new APOD widget.
  466. */
  467. constructor() {
  468. super();
  469. this.addClass('my-apodWidget');
  470. // Add an image element to the panel
  471. this.img = document.createElement('img');
  472. this.node.appendChild(this.img);
  473. // Add a summary element to the panel
  474. this.summary = document.createElement('p');
  475. this.node.appendChild(this.summary);
  476. }
  477. /**
  478. * The image element associated with the widget.
  479. */
  480. readonly img: HTMLImageElement;
  481. /**
  482. * The summary text element associated with the widget.
  483. */
  484. readonly summary: HTMLParagraphElement;
  485. /**
  486. * Handle update requests for the widget.
  487. */
  488. async onUpdateRequest(msg: Message): Promise<void> {
  489. const response = await fetch(`https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=${this.randomDate()}`);
  490. if (!response.ok) {
  491. const data = await response.json();
  492. if (data.error) {
  493. this.summary.innerText = data.error.message;
  494. } else {
  495. this.summary.innerText = response.statusText;
  496. }
  497. return;
  498. }
  499. const data = await response.json() as APODResponse;
  500. if (data.media_type === 'image') {
  501. // Populate the image
  502. this.img.src = data.url;
  503. this.img.title = data.title;
  504. this.summary.innerText = data.title;
  505. if (data.copyright) {
  506. this.summary.innerText += ` (Copyright ${data.copyright})`;
  507. }
  508. } else {
  509. this.summary.innerText = 'Random APOD fetched was not an image.';
  510. }
  511. }
  512. /**
  513. * Get a random date string in YYYY-MM-DD format.
  514. */
  515. randomDate(): string {
  516. const start = new Date(2010, 1, 1);
  517. const end = new Date();
  518. const randomDate = new Date(start.getTime() + Math.random()*(end.getTime() - start.getTime()));
  519. return randomDate.toISOString().slice(0, 10);
  520. }
  521. }
  522. You've written all of the code before. All you've done is restructure it
  523. to use instance variables and move the image request to its own
  524. function.
  525. Next move the remaining logic in ``activate`` to a new, top-level
  526. function just below the ``APODWidget`` class definition. Modify the code
  527. to create a widget when one does not exist in the main JupyterLab area
  528. or to refresh the image in the existing widget when the command runs again.
  529. The code for the ``activate`` function should read as follows after
  530. these changes:
  531. .. code-block:: typescript
  532. /**
  533. * Activate the APOD widget extension.
  534. */
  535. function activate(app: JupyterFrontEnd, palette: ICommandPalette) {
  536. console.log('JupyterLab extension jupyterlab_apod is activated!');
  537. // Create a single widget
  538. const content = new APODWidget();
  539. const widget = new MainAreaWidget({content});
  540. widget.id = 'apod-jupyterlab';
  541. widget.title.label = 'Astronomy Picture';
  542. widget.title.closable = true;
  543. // Add an application command
  544. const command: string = 'apod:open';
  545. app.commands.addCommand(command, {
  546. label: 'Random Astronomy Picture',
  547. execute: () => {
  548. if (!widget.isAttached) {
  549. // Attach the widget to the main work area if it's not there
  550. app.shell.add(widget, 'main');
  551. }
  552. // Refresh the picture in the widget
  553. content.update();
  554. // Activate the widget
  555. app.shell.activateById(widget.id);
  556. }
  557. });
  558. // Add the command to the palette.
  559. palette.addItem({ command, category: 'Tutorial' });
  560. }
  561. Remove the ``activate`` function definition from the
  562. ``JupyterFrontEndPlugin`` object and refer instead to the top-level function
  563. like this:
  564. .. code-block:: typescript
  565. const extension: JupyterFrontEndPlugin<void> = {
  566. id: 'jupyterlab_apod',
  567. autoStart: true,
  568. requires: [ICommandPalette],
  569. activate: activate
  570. };
  571. Make sure you retain the ``export default extension;`` line in the file.
  572. Now build the extension again and refresh the JupyterLab browser tab.
  573. Run the *Random Astronomy Picture* command more than once without closing the
  574. panel. The picture should update each time you execute the command. Close
  575. the panel, run the command, and it should both reappear and show a new
  576. image.
  577. If anything is not working correctly, compare your code with the
  578. `04-refactor-and-refresh
  579. tag <https://github.com/jupyterlab/jupyterlab_apod/tree/3.0-04-refactor-and-refresh>`__
  580. to debug. Once it is working properly, commit it.
  581. .. code:: bash
  582. git add package.json src/index.ts
  583. git commit -m 'Refactor, refresh image'
  584. Restore panel state when the browser refreshes
  585. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  586. You may notice that every time you refresh your browser tab, the Astronomy Picture
  587. panel disappears, even if it was open before you refreshed. Other open
  588. panels, like notebooks, terminals, and text editors, all reappear and
  589. return to where you left them in the panel layout. You can make your
  590. extension behave this way too.
  591. Update the imports at the top of your ``index.ts`` file so that the
  592. entire list of import statements looks like the following:
  593. .. code-block:: typescript
  594. :emphasize-lines: 2,10
  595. import {
  596. ILayoutRestorer,
  597. JupyterFrontEnd,
  598. JupyterFrontEndPlugin
  599. } from '@jupyterlab/application';
  600. import {
  601. ICommandPalette,
  602. MainAreaWidget,
  603. WidgetTracker
  604. } from '@jupyterlab/apputils';
  605. import { Message } from '@lumino/messaging';
  606. import { Widget } from '@lumino/widgets';
  607. Then add the ``ILayoutRestorer`` interface to the ``JupyterFrontEndPlugin``
  608. definition. This addition passes the global ``LayoutRestorer`` as the
  609. third parameter of the ``activate`` function.
  610. .. code-block:: typescript
  611. :emphasize-lines: 4
  612. const extension: JupyterFrontEndPlugin<void> = {
  613. id: 'jupyterlab_apod',
  614. autoStart: true,
  615. requires: [ICommandPalette, ILayoutRestorer],
  616. activate: activate
  617. };
  618. Finally, rewrite the ``activate`` function so that it:
  619. 1. Declares a widget variable, but does not create an instance
  620. immediately.
  621. 2. Constructs a ``WidgetTracker`` and tells the ``ILayoutRestorer``
  622. to use it to save/restore panel state.
  623. 3. Creates, tracks, shows, and refreshes the widget panel appropriately.
  624. .. code-block:: typescript
  625. function activate(app: JupyterFrontEnd, palette: ICommandPalette, restorer: ILayoutRestorer) {
  626. console.log('JupyterLab extension jupyterlab_apod is activated!');
  627. // Declare a widget variable
  628. let widget: MainAreaWidget<APODWidget>;
  629. // Add an application command
  630. const command: string = 'apod:open';
  631. app.commands.addCommand(command, {
  632. label: 'Random Astronomy Picture',
  633. execute: () => {
  634. if (!widget || widget.isDisposed) {
  635. // Create a new widget if one does not exist
  636. // or if the previous one was disposed after closing the panel
  637. const content = new APODWidget();
  638. widget = new MainAreaWidget({content});
  639. widget.id = 'apod-jupyterlab';
  640. widget.title.label = 'Astronomy Picture';
  641. widget.title.closable = true;
  642. }
  643. if (!tracker.has(widget)) {
  644. // Track the state of the widget for later restoration
  645. tracker.add(widget);
  646. }
  647. if (!widget.isAttached) {
  648. // Attach the widget to the main work area if it's not there
  649. app.shell.add(widget, 'main');
  650. }
  651. widget.content.update();
  652. // Activate the widget
  653. app.shell.activateById(widget.id);
  654. }
  655. });
  656. // Add the command to the palette.
  657. palette.addItem({ command, category: 'Tutorial' });
  658. // Track and restore the widget state
  659. let tracker = new WidgetTracker<MainAreaWidget<APODWidget>>({
  660. namespace: 'apod'
  661. });
  662. restorer.restore(tracker, {
  663. command,
  664. name: () => 'apod'
  665. });
  666. }
  667. Rebuild your extension one last time and refresh your browser tab.
  668. Execute the *Random Astronomy Picture* command and validate that the panel
  669. appears with an image in it. Refresh the browser tab again. You should
  670. see an Astronomy Picture panel reappear immediately without running the command. Close
  671. the panel and refresh the browser tab. You should then not see an Astronomy Picture tab
  672. after the refresh.
  673. .. figure:: images/extension_tutorial_complete.png
  674. :align: center
  675. :class: jp-screenshot
  676. :alt: The completed extension, showing the Astronomy Picture of the Day for 24 Jul 2015.
  677. The completed extension, showing the `Astronomy Picture of the Day for 24 Jul 2015 <https://apod.nasa.gov/apod/ap150724.html>`__.
  678. Refer to the `05-restore-panel-state
  679. tag <https://github.com/jupyterlab/jupyterlab_apod/tree/3.0-05-restore-panel-state>`__
  680. if your extension is not working correctly. Make a commit when the state of your
  681. extension persists properly.
  682. .. code:: bash
  683. git add src/index.ts
  684. git commit -m 'Restore panel state'
  685. Congratulations! You've implemented all of the behaviors laid out at the start
  686. of this tutorial.
  687. .. _packaging your extension:
  688. Packaging your extension
  689. ------------------------
  690. JupyterLab extensions for JupyterLab 3.0 can be distributed as Python
  691. packages. The cookiecutter template we used contains all of the Python
  692. packaging instructions in the ``pyproject.toml`` file to wrap your extension in a
  693. Python package. Before generating a package, we first need to install ``build``.
  694. .. code:: bash
  695. pip install build
  696. To create a Python source package (``.tar.gz``) in the ``dist/`` directory, do:
  697. .. code:: bash
  698. python -m build -s
  699. To create a Python wheel package (``.whl``) in the ``dist/`` directory, do:
  700. .. code:: bash
  701. python -m build
  702. Both of these commands will build the JavaScript into a bundle in the
  703. ``jupyterlab_apod/labextension/static`` directory, which is then distributed with the
  704. Python package. This bundle will include any necessary JavaScript dependencies
  705. as well. You may want to check in the ``jupyterlab_apod/labextension/static`` directory to
  706. retain a record of what JavaScript is distributed in your package, or you may
  707. want to keep this "build artifact" out of your source repository history.
  708. You can now try installing your extension as a user would. Open a new terminal
  709. and run the following commands to create a new environment and install your
  710. extension.
  711. .. code:: bash
  712. conda create -n jupyterlab-apod jupyterlab
  713. conda activate jupyterlab-apod
  714. pip install jupyterlab_apod/dist/jupyterlab_apod-0.1.0-py3-none-any.whl
  715. jupyter lab
  716. You should see a fresh JupyterLab browser tab appear. When it does,
  717. execute the *Random Astronomy Picture* command to check that your extension
  718. works.
  719. .. _extension_tutorial_publish:
  720. Publishing your extension
  721. -------------------------
  722. You can publish your Python package to the `PyPI <https://pypi.org>`_ or
  723. `conda-forge <https://conda-forge.org>`_ repositories so users can easily
  724. install the extension using ``pip`` or ``conda``.
  725. You may want to also publish your extension as a JavaScript package to the
  726. `npm <https://www.npmjs.com>`_ package repository for several reasons:
  727. 1. Distributing an extension as an npm package allows users to compile the
  728. extension into JupyterLab explicitly (similar to how was done in JupyterLab
  729. versions 1 and 2), which leads to a more optimal JupyterLab package.
  730. 2. As we saw above, JupyterLab enables extensions to use services provided by
  731. other extensions. For example, our extension above uses the ``ICommandPalette``
  732. and ``ILayoutRestorer`` services provided by core extensions in
  733. JupyterLab. We were able to tell JupyterLab we required these services by
  734. importing their tokens from the ``@jupyterlab/apputils`` and
  735. ``@jupyterlab/application`` npm packages and listing them in our plugin
  736. definition. If you want to provide a service to the JupyterLab system
  737. for other extensions to use, you will need to publish your JavaScript
  738. package to npm so other extensions can depend on it and import and require
  739. your token.
  740. Automated Releases
  741. ^^^^^^^^^^^^^^^^^^
  742. If you used the cookiecutter to bootstrap your extension, the repository should already
  743. be compatible with the `Jupyter Releaser <https://github.com/jupyter-server/jupyter_releaser>`_.
  744. The Jupyter Releaser provides a set of GitHub Actions Workflows to:
  745. - Generate a new entry in the Changelog
  746. - Draft a new release
  747. - Publish the release to ``PyPI`` and ``npm``
  748. For more information on how to run the release workflows,
  749. check out the documentation: https://github.com/jupyter-server/jupyter_releaser
  750. Learn more
  751. ----------
  752. You've completed the tutorial. Nicely done! If you want to keep
  753. learning, here are some suggestions about what to try next:
  754. - Add the image description that comes in the API response to the panel.
  755. - Assign a default hotkey to the *Random Astronomy Picture* command.
  756. - Make the image a link to the picture on the NASA website (URLs are of the form ``https://apod.nasa.gov/apod/apYYMMDD.html``).
  757. - Make the image title and description update after the image loads so that the picture and description are always synced.
  758. - Give users the ability to pin pictures in separate, permanent panels.
  759. - Add a setting for the user to put in their `API key <https://api.nasa.gov/#authentication>`__ so they can make many more requests per hour than the demo key allows.
  760. - Push your extension git repository to GitHub.
  761. - Learn how to write :ref:`other kinds of extensions <developer_extensions>`.