xkcd_extension_tutorial.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. .. _xkcd_extension_tutorial:
  2. Let's Make an xkcd JupyterLab Extension
  3. ---------------------------------------
  4. .. warning::
  5. The extension developer API is not stable and will evolve in JupyterLab
  6. releases in the near future.
  7. JupyterLab extensions add features to the user experience. This page
  8. describes how to create one type of extension, an *application plugin*,
  9. that:
  10. - Adds a "Random `xkcd <https://xkcd.com>`__ comic" command to the
  11. *command palette* sidebar
  12. - Fetches the comic image and metadata when activated
  13. - Shows the image and metadata in a tab panel
  14. By working through this tutorial, you'll learn:
  15. - How to setup an extension development environment from scratch on a
  16. Linux or OSX machine.
  17. - Windows users: You'll need to modify the commands slightly.
  18. - How to start an extension project from
  19. `jupyterlab/extension-cookiecutter-ts <https://github.com/jupyterlab/extension-cookiecutter-ts>`__
  20. - How to iteratively code, build, and load your extension in JupyterLab
  21. - How to version control your work with git
  22. - How to release your extension for others to enjoy
  23. |Completed xkcd extension screenshot|
  24. Sound like fun? Excellent. Here we go!
  25. Setup a development environment
  26. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. Install conda using miniconda
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. Start by opening your web browser and downloading the latest Python 3.x
  30. `Miniconda installer <https://conda.io/miniconda.html>`__ to your home
  31. directory. When the download completes, open a terminal and create a
  32. root conda environment by running this command.
  33. .. code:: bash
  34. bash Miniconda3*.sh -b -p ~/miniconda
  35. Now activate the conda environment you just created so that you can run
  36. the ``conda`` package manager.
  37. .. code:: bash
  38. source ~/miniconda/bin/activate
  39. For more detail on installing miniconda, see
  40. `Conda's installation documentation <https://conda.io/docs/user-guide/install/index.html>`__.
  41. .. _install-nodejs-jupyterlab-etc-in-a-conda-environment:
  42. Install NodeJS, JupyterLab, etc. in a conda environment
  43. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  44. Next create a conda environment that includes:
  45. 1. the latest release of JupyterLab
  46. 2. `cookiecutter <https://github.com/audreyr/cookiecutter>`__, the tool
  47. you'll use to bootstrap your extension project structure
  48. 3. `NodeJS <https://nodejs.org>`__, the JavaScript runtime you'll use to
  49. compile the web assets (e.g., TypeScript, CSS) for your extension
  50. 4. `git <https://git-scm.com>`__, a version control system you'll use to
  51. take snapshots of your work as you progress through this tutorial
  52. It's best practice to leave the root conda environment, the one created
  53. by the miniconda installer, untouched and install your project specific
  54. dependencies in a named conda environment. Run this command to create a
  55. new environment named ``jupyterlab-ext``.
  56. .. code:: bash
  57. conda create -n jupyterlab-ext nodejs jupyterlab cookiecutter git -c conda-forge
  58. Now activate the new environment so that all further commands you run
  59. work out of that environment.
  60. .. code:: bash
  61. source ~/miniconda/bin/activate jupyterlab-ext
  62. Note: You'll need to run the command above in each new terminal you open
  63. before you can work with the tools you installed in the
  64. ``jupyterlab-ext`` environment.
  65. Create a repository
  66. ~~~~~~~~~~~~~~~~~~~
  67. Create a new repository for your extension. For example, on
  68. `GitHub <https://help.github.com/articles/create-a-repo/>`__. This is an
  69. optional step but highly recommended if you want to share your
  70. extension.
  71. Create an extension project
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. Initialize the project from a cookiecutter
  74. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  75. Next use cookiecutter to create a new project for your extension.
  76. This will create a new folder for your extension in your current directory.
  77. .. code:: bash
  78. cookiecutter https://github.com/jupyterlab/extension-cookiecutter-ts
  79. When prompted, enter values like the following for all of the
  80. cookiecutter prompts.
  81. ::
  82. author_name []: Your Name
  83. extension_name [myextension]: jupyterlab_xkcd
  84. project_short_description [A JupyterLab extension.]: Show a random xkcd.com comic in a JupyterLab panel
  85. repository [https://github.com/my_name/jupyterlab_myextension]: https://github.com/my_name/jupyterlab_xkcd
  86. Note: if not using a repository, leave the field blank. You can come
  87. back and edit the repository links in the ``package.json`` file later.
  88. Change to the directory the cookiecutter created and list the files.
  89. .. code:: bash
  90. cd jupyterlab_xkcd
  91. ls
  92. You should see a list like the following.
  93. ::
  94. README.md package.json src style tsconfig.json
  95. Build and install the extension for development
  96. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  97. Your new extension project has enough code in it to see it working in
  98. your JupyterLab. Run the following commands to install the initial
  99. project dependencies and install it in the JupyterLab environment. We
  100. defer building since it will be built in the next step.
  101. .. note::
  102. This tutorial uses ``npm`` to install Javascript packages and
  103. run build commands. If you prefer, you can use another package
  104. manager like ``yarn`` or ``jlpm``, which is JupyterLab's bundled
  105. version of ``yarn``.
  106. .. code:: bash
  107. npm install
  108. jupyter labextension install . --no-build
  109. After the install completes, open a second terminal. Run these commands
  110. to activate the ``jupyterlab-ext`` environment and to start a JupyterLab
  111. instance in watch mode so that it will keep up with our changes as we
  112. make them.
  113. .. code:: bash
  114. source ~/miniconda/bin/activate jupyterlab-ext
  115. jupyter lab --watch
  116. See the initial extension in action
  117. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  118. After building with your extension, JupyterLab should open in your
  119. default web browser.
  120. In that window open the JavaScript console
  121. by following the instructions for your browser:
  122. - `Accessing the DevTools in Google
  123. Chrome <https://developer.chrome.com/devtools#access>`__
  124. - `Opening the Web Console in
  125. Firefox <https://developer.mozilla.org/en-US/docs/Tools/Web_Console/Opening_the_Web_Console>`__
  126. After you reload the page with the console open, you should see a message that says
  127. ``JupyterLab extension jupyterlab_xkcd is activated!`` in the console.
  128. If you do, congrats, you're ready to start modifying the the extension!
  129. If not, go back, make sure you didn't miss a step, and `reach
  130. out <https://github.com/jupyterlab/jupyterlab/blob/master/README.md#getting-help>`__ if you're stuck.
  131. Note: Leave the terminal running the ``jupyter lab --watch`` command
  132. open.
  133. Commit what you have to git
  134. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  135. Run the following commands in your ``jupyterlab_xkcd`` folder to
  136. initialize it as a git repository and commit the current code.
  137. .. code:: bash
  138. git init
  139. git add .
  140. git commit -m 'Seed xkcd project from cookiecutter'
  141. Note: This step is not technically necessary, but it is good practice to
  142. track changes in version control system in case you need to rollback to
  143. an earlier version or want to collaborate with others. For example, you
  144. can compare your work throughout this tutorial with the commits in a
  145. reference version of ``jupyterlab_xkcd`` on GitHub at
  146. https://github.com/jupyterlab/jupyterlab_xkcd.
  147. Add an xkcd widget
  148. ~~~~~~~~~~~~~~~~~~
  149. Show an empty panel
  150. ^^^^^^^^^^^^^^^^^^^
  151. The *command palette* is the primary view of all commands available to
  152. you in JupyterLab. For your first addition, you're going to add a
  153. *Random xkcd comic* command to the palette and get it to show an *xkcd*
  154. tab panel when invoked.
  155. Fire up your favorite text editor and open the ``src/index.ts`` file in
  156. your extension project. Add the following import at the top of the file
  157. to get a reference to the command palette interface.
  158. .. code:: typescript
  159. import {
  160. ICommandPalette
  161. } from '@jupyterlab/apputils';
  162. You will also need to install this dependency. Run the following command in the
  163. repository root folder install the dependency and save it to your
  164. `package.json`:
  165. .. code:: bash
  166. npm install --save @jupyterlab/apputils
  167. Locate the ``extension`` object of type ``JupyterLabPlugin``. Change the
  168. definition so that it reads like so:
  169. .. code:: typescript
  170. /**
  171. * Initialization data for the jupyterlab_xkcd extension.
  172. */
  173. const extension: JupyterLabPlugin<void> = {
  174. id: 'jupyterlab_xkcd',
  175. autoStart: true,
  176. requires: [ICommandPalette],
  177. activate: (app: JupyterLab, palette: ICommandPalette) => {
  178. console.log('JupyterLab extension jupyterlab_xkcd is activated!');
  179. console.log('ICommandPalette:', palette);
  180. }
  181. };
  182. The ``requires`` attribute states that your plugin needs an object that
  183. implements the ``ICommandPalette`` interface when it starts. JupyterLab
  184. will pass an instance of ``ICommandPalette`` as the second parameter of
  185. ``activate`` in order to satisfy this requirement. Defining
  186. ``palette: ICommandPalette`` makes this instance available to your code
  187. in that function. The second ``console.log`` line exists only so that
  188. you can immediately check that your changes work.
  189. Run the following to rebuild your extension.
  190. .. code:: bash
  191. npm run build
  192. JupyterLab will rebuild after the extension does. You can
  193. see it's progress in the ``jupyter lab --watch`` window. After that
  194. finishes, return to the browser tab that opened when you
  195. started JupyterLab. Refresh it and look in the console. You should see
  196. the same activation message as before, plus the new message about the
  197. ICommandPalette instance you just added. If you don't, check the output
  198. of the build command for errors and correct your code.
  199. ::
  200. JupyterLab extension jupyterlab_xkcd is activated!
  201. ICommandPalette: Palette {_palette: CommandPalette}
  202. Note that we had to run ``npm run build`` in order for the bundle to
  203. update, because it is using the compiled JavaScript files in ``/lib``.
  204. If you wish to avoid running ``npm run build`` after each change, you
  205. can open a third terminal, and run the ``npm run watch`` command from
  206. your extension directory, which will automatically compile the
  207. TypeScript files as they change.
  208. Now return to your editor. Add the following additional import to the
  209. top of the file.
  210. .. code:: typescript
  211. import {
  212. Widget
  213. } from '@phosphor/widgets';
  214. Install this dependency as well:
  215. .. code:: bash
  216. npm install --save @phosphor/widgets
  217. Then modify the ``activate`` function again so that it has the following
  218. code:
  219. .. code-block:: typescript
  220. activate: (app: JupyterLab, palette: ICommandPalette) => {
  221. console.log('JupyterLab extension jupyterlab_xkcd is activated!');
  222. // Create a single widget
  223. let widget: Widget = new Widget();
  224. widget.id = 'xkcd-jupyterlab';
  225. widget.title.label = 'xkcd.com';
  226. widget.title.closable = true;
  227. // Add an application command
  228. const command: string = 'xkcd:open';
  229. app.commands.addCommand(command, {
  230. label: 'Random xkcd comic',
  231. execute: () => {
  232. if (!widget.isAttached) {
  233. // Attach the widget to the main work area if it's not there
  234. app.shell.addToMainArea(widget);
  235. }
  236. // Activate the widget
  237. app.shell.activateById(widget.id);
  238. }
  239. });
  240. // Add the command to the palette.
  241. palette.addItem({command, category: 'Tutorial'});
  242. }
  243. The first new block of code creates a ``Widget`` instance, assigns it a
  244. unique ID, gives it a label that will appear as its tab title, and makes
  245. the tab closable by the user. The second block of code add a new command
  246. labeled *Random xkcd comic* to JupyterLab. When the command executes,
  247. it attaches the widget to the main display area if it is not already
  248. present and then makes it the active tab. The last new line of code adds
  249. the command to the command palette in a section called *Tutorial*.
  250. Build your extension again using ``npm run build`` (unless you are using
  251. ``npm run watch`` already) and refresh the browser tab. Open the command
  252. palette on the left side by clicking on *Commands* and type *xkcd* in
  253. the search box. Your *Random xkcd comic*
  254. command should appear. Click it or select it with the keyboard and press
  255. *Enter*. You should see a new, blank panel appear with the tab title
  256. *xkcd.com*. Click the *x* on the tab to close it and activate the
  257. command again. The tab should reappear. Finally, click one of the
  258. launcher tabs so that the *xkcd.com* panel is still open but no longer
  259. active. Now run the *Random xkcd comic* command one more time. The
  260. single *xkcd.com* tab should come to the foreground.
  261. |Empty xkcd extension panel|
  262. If your widget is not behaving, compare your code with the reference
  263. project state at the `01-show-a-panel
  264. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-01-show-a-panel>`__.
  265. Once you've got everything working properly, git commit your changes and
  266. carry on.
  267. .. code-block:: bash
  268. git add .
  269. git commit -m 'Show xkcd command on panel'
  270. Show a comic in the panel
  271. ^^^^^^^^^^^^^^^^^^^^^^^^^
  272. You've got an empty panel. It's time to add a comic to it. Go back to
  273. your code editor. Add the following code below the lines that create a
  274. ``Widget`` instance and above the lines that define the command.
  275. .. code-block:: typescript
  276. // Add an image element to the panel
  277. let img = document.createElement('img');
  278. widget.node.appendChild(img);
  279. // Fetch info about a random comic
  280. fetch('https:////egszlpbmle.execute-api.us-east-1.amazonaws.com/prod').then(response => {
  281. return response.json();
  282. }).then(data => {
  283. img.src = data.img;
  284. img.alt = data.title;
  285. img.title = data.alt;
  286. });
  287. The first two lines create a new HTML ``<img>`` element and add it to
  288. the widget DOM node. The next lines make a request using the HTML
  289. `fetch <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch>`__
  290. API that returns information about a random xkcd comic, and set the
  291. image source, alternate text, and title attributes based on the
  292. response.
  293. Rebuild your extension if necessary (``npm run build``), refresh your
  294. browser tab, and run the *Random xkcd comic* command again. You should
  295. now see a comic in the xkcd.com panel when it opens.
  296. |Single xkcd extension panel|
  297. Note that the comic is not centered in the panel nor does the panel
  298. scroll if the comic is larger than the panel area. Also note that the
  299. comic does not update no matter how many times you close and reopen the
  300. panel. You'll address both of these problems in the upcoming sections.
  301. If you don't see a comic at all, compare your code with the
  302. `02-show-a-comic
  303. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-02-show-a-comic>`__
  304. in the reference project. When it's working, make another git commit.
  305. .. code:: bash
  306. git add .
  307. git commit -m 'Show a comic in the panel'
  308. Improve the widget behavior
  309. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  310. Center the comic and add attribution
  311. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  312. Open ``style/index.css`` in our extension project directory for editing.
  313. Add the following lines to it.
  314. .. code-block:: css
  315. .jp-xkcdWidget {
  316. display: flex;
  317. flex-direction: column;
  318. overflow: auto;
  319. }
  320. .jp-xkcdCartoon {
  321. margin: auto;
  322. }
  323. .jp-xkcdAttribution {
  324. margin: 20px auto;
  325. }
  326. The first rule stacks content vertically within the widget panel and
  327. lets the panel scroll when the content overflows. The other rules center
  328. the cartoon and attribution badge horizontally and space them out
  329. vertically.
  330. Return to the ``index.ts`` file. Note that there is already an import of
  331. the CSS file in the ``index.ts`` file. Modify the the ``activate``
  332. function to apply the CSS classes and add the attribution badge markup.
  333. The beginning of the function should read like the following:
  334. .. code-block:: typescript
  335. :emphasize-lines: 9,13,16-22
  336. activate: (app: JupyterLab, palette: ICommandPalette) => {
  337. console.log('JupyterLab extension jupyterlab_xkcd is activated!');
  338. // Create a single widget
  339. let widget: Widget = new Widget();
  340. widget.id = 'xkcd-jupyterlab';
  341. widget.title.label = 'xkcd.com';
  342. widget.title.closable = true;
  343. widget.addClass('jp-xkcdWidget'); // new line
  344. // Add an image element to the panel
  345. let img = document.createElement('img');
  346. img.className = 'jp-xkcdCartoon'; // new line
  347. widget.node.appendChild(img);
  348. // New: add an attribution badge
  349. img.insertAdjacentHTML('afterend',
  350. `<div class="jp-xkcdAttribution">
  351. <a href="https://creativecommons.org/licenses/by-nc/2.5/" class="jp-xkcdAttribution" target="_blank">
  352. <img src="https://licensebuttons.net/l/by-nc/2.5/80x15.png" />
  353. </a>
  354. </div>`
  355. );
  356. // Keep all the remaining fetch and command lines the same
  357. // as before from here down ...
  358. Build your extension if necessary (``npm run build``) and refresh your
  359. JupyterLab browser tab. Invoke the *Random xkcd comic* command and
  360. confirm the comic is centered with an attribution badge below it. Resize
  361. the browser window or the panel so that the comic is larger than the
  362. available area. Make sure you can scroll the panel over the entire area
  363. of the comic.
  364. |Styled xkcd panel with attribution|
  365. If anything is misbehaving, compare your code with the reference project
  366. `03-style-and-attribute
  367. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-03-style-and-attribute>`__.
  368. When everything is working as expected, make another commit.
  369. .. code:: bash
  370. git add .
  371. git commit -m 'Add styling, attribution'
  372. Show a new comic on demand
  373. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  374. The ``activate`` function has grown quite long, and there's still more
  375. functionality to add. You should refactor the code into two separate
  376. parts:
  377. 1. An ``XkcdWidget`` that encapsulates the xkcd panel elements,
  378. configuration, and soon-to-be-added update behavior
  379. 2. An ``activate`` function that adds the widget instance to the UI and
  380. decide when the comic should refresh
  381. Start by refactoring the widget code into the new ``XkcdWidget`` class.
  382. Add the following additional import to the top of the file.
  383. .. code-block:: typescript
  384. import {
  385. Message
  386. } from '@phosphor/messaging';
  387. Install this dependency:
  388. .. code:: bash
  389. npm install --save @phosphor/messaging
  390. Then add the class just below the import statements in the ``index.ts``
  391. file.
  392. .. code-block:: typescript
  393. /**
  394. * An xckd comic viewer.
  395. */
  396. class XkcdWidget extends Widget {
  397. /**
  398. * Construct a new xkcd widget.
  399. */
  400. constructor() {
  401. super();
  402. this.id = 'xkcd-jupyterlab';
  403. this.title.label = 'xkcd.com';
  404. this.title.closable = true;
  405. this.addClass('jp-xkcdWidget');
  406. this.img = document.createElement('img');
  407. this.img.className = 'jp-xkcdCartoon';
  408. this.node.appendChild(this.img);
  409. this.img.insertAdjacentHTML('afterend',
  410. `<div class="jp-xkcdAttribution">
  411. <a href="https://creativecommons.org/licenses/by-nc/2.5/" class="jp-xkcdAttribution" target="_blank">
  412. <img src="https://licensebuttons.net/l/by-nc/2.5/80x15.png" />
  413. </a>
  414. </div>`
  415. );
  416. }
  417. /**
  418. * The image element associated with the widget.
  419. */
  420. readonly img: HTMLImageElement;
  421. /**
  422. * Handle update requests for the widget.
  423. */
  424. onUpdateRequest(msg: Message): void {
  425. fetch('https://egszlpbmle.execute-api.us-east-1.amazonaws.com/prod').then(response => {
  426. return response.json();
  427. }).then(data => {
  428. this.img.src = data.img;
  429. this.img.alt = data.title;
  430. this.img.title = data.alt;
  431. });
  432. }
  433. };
  434. You've written all of the code before. All you've done is restructure it
  435. to use instance variables and move the comic request to its own
  436. function.
  437. Next move the remaining logic in ``activate`` to a new, top-level
  438. function just below the ``XkcdWidget`` class definition. Modify the code
  439. to create a widget when one does not exist in the main JupyterLab area
  440. or to refresh the comic in the exist widget when the command runs again.
  441. The code for the ``activate`` function should read as follows after
  442. these changes:
  443. .. code-block:: typescript
  444. /**
  445. * Activate the xckd widget extension.
  446. */
  447. function activate(app: JupyterLab, palette: ICommandPalette) {
  448. console.log('JupyterLab extension jupyterlab_xkcd is activated!');
  449. // Create a single widget
  450. let widget: XkcdWidget = new XkcdWidget();
  451. // Add an application command
  452. const command: string = 'xkcd:open';
  453. app.commands.addCommand(command, {
  454. label: 'Random xkcd comic',
  455. execute: () => {
  456. if (!widget.isAttached) {
  457. // Attach the widget to the main work area if it's not there
  458. app.shell.addToMainArea(widget);
  459. }
  460. // Refresh the comic in the widget
  461. widget.update();
  462. // Activate the widget
  463. app.shell.activateById(widget.id);
  464. }
  465. });
  466. // Add the command to the palette.
  467. palette.addItem({ command, category: 'Tutorial' });
  468. };
  469. Remove the ``activate`` function definition from the
  470. ``JupyterLabPlugin`` object and refer instead to the top-level function
  471. like so:
  472. .. code-block:: typescript
  473. const extension: JupyterLabPlugin<void> = {
  474. id: 'jupyterlab_xkcd',
  475. autoStart: true,
  476. requires: [ICommandPalette],
  477. activate: activate
  478. };
  479. Make sure you retain the ``export default extension;`` line in the file.
  480. Now build the extension again and refresh the JupyterLab browser tab.
  481. Run the *Random xkcd comic* command more than once without closing the
  482. panel. The comic should update each time you execute the command. Close
  483. the panel, run the command, and it should both reappear and show a new
  484. comic.
  485. If anything is amiss, compare your code with the
  486. `04-refactor-and-refresh
  487. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-04-refactor-and-refresh>`__
  488. to debug. Once it's working properly, commit it.
  489. .. code:: bash
  490. git add .
  491. git commit -m 'Refactor, refresh comic'
  492. Restore panel state when the browser refreshes
  493. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  494. You may notice that every time you refresh your browser tab, the xkcd
  495. panel disappears, even if it was open before you refreshed. Other open
  496. panels, like notebooks, terminals, and text editors, all reappear and
  497. return to where you left them in the panel layout. You can make your
  498. extension behave this way too.
  499. Update the imports at the top of your ``index.ts`` file so that the
  500. entire list of import statements looks like the following:
  501. .. code-block:: typescript
  502. :emphasize-lines: 2,6,9-11
  503. import {
  504. JupyterLab, JupyterLabPlugin, ILayoutRestorer // new
  505. } from '@jupyterlab/application';
  506. import {
  507. ICommandPalette, InstanceTracker // new
  508. } from '@jupyterlab/apputils';
  509. import {
  510. JSONExt // new
  511. } from '@phosphor/coreutils';
  512. import {
  513. Message
  514. } from '@phosphor/messaging';
  515. import {
  516. Widget
  517. } from '@phosphor/widgets';
  518. import '../style/index.css';
  519. Install this dependency:
  520. .. code:: bash
  521. npm install --save @phosphor/coreutils
  522. Then, add the ``ILayoutRestorer`` interface to the ``JupyterLabPlugin``
  523. definition. This addition passes the global ``LayoutRestorer`` to the
  524. third parameter of the ``activate``.
  525. .. code:: typescript
  526. const extension: JupyterLabPlugin<void> = {
  527. id: 'jupyterlab_xkcd',
  528. autoStart: true,
  529. requires: [ICommandPalette, ILayoutRestorer],
  530. activate: activate
  531. };
  532. Finally, rewrite the ``activate`` function so that it:
  533. 1. Declares a widget variable, but does not create an instance
  534. immediately
  535. 2. Constructs an ``InstanceTracker`` and tells the ``ILayoutRestorer``
  536. to use it to save/restore panel state
  537. 3. Creates, tracks, shows, and refreshes the widget panel appropriately
  538. .. code-block:: typescript
  539. function activate(app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRestorer) {
  540. console.log('JupyterLab extension jupyterlab_xkcd is activated!');
  541. // Declare a widget variable
  542. let widget: XkcdWidget;
  543. // Add an application command
  544. const command: string = 'xkcd:open';
  545. app.commands.addCommand(command, {
  546. label: 'Random xkcd comic',
  547. execute: () => {
  548. if (!widget) {
  549. // Create a new widget if one does not exist
  550. widget = new XkcdWidget();
  551. widget.update();
  552. }
  553. if (!tracker.has(widget)) {
  554. // Track the state of the widget for later restoration
  555. tracker.add(widget);
  556. }
  557. if (!widget.isAttached) {
  558. // Attach the widget to the main work area if it's not there
  559. app.shell.addToMainArea(widget);
  560. } else {
  561. // Refresh the comic in the widget
  562. widget.update();
  563. }
  564. // Activate the widget
  565. app.shell.activateById(widget.id);
  566. }
  567. });
  568. // Add the command to the palette.
  569. palette.addItem({ command, category: 'Tutorial' });
  570. // Track and restore the widget state
  571. let tracker = new InstanceTracker<Widget>({ namespace: 'xkcd' });
  572. restorer.restore(tracker, {
  573. command,
  574. args: () => JSONExt.emptyObject,
  575. name: () => 'xkcd'
  576. });
  577. };
  578. Rebuild your extension one last time and refresh your browser tab.
  579. Execute the *Random xkcd comic* command and validate that the panel
  580. appears with a comic in it. Refresh the browser tab again. You should
  581. see an xkcd panel appear immediately without running the command. Close
  582. the panel and refresh the browser tab. You should not see an xkcd tab
  583. after the refresh.
  584. Refer to the `05-restore-panel-state
  585. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-05-restore-panel-state>`__
  586. if your extension is misbehaving. Make a commit when the state of your
  587. extension persists properly.
  588. .. code:: bash
  589. git add .
  590. git commit -m 'Restore panel state'
  591. Congrats! You've implemented all of the behaviors laid out at the start
  592. of this tutorial. Now how about sharing it with the world?
  593. .. _publish-your-extension-to-npmjsorg:
  594. Publish your extension to npmjs.org
  595. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  596. npm is both a JavaScript package manager and the de facto registry for
  597. JavaScript software. You can `sign up for an account on the npmjs.com
  598. site <https://www.npmjs.com/signup>`__ or create an account from the
  599. command line by running ``npm adduser`` and entering values when
  600. prompted. Create an account now if you do not already have one. If you
  601. already have an account, login by running ``npm login`` and answering
  602. the prompts.
  603. Next, open the project ``package.json`` file in your text editor. Prefix
  604. the ``name`` field value with ``@your-npm-username>/`` so that the
  605. entire field reads ``"name": "@your-npm-username/jupyterlab_xkcd"`` where
  606. you've replaced the string ``your-npm-username`` with your real
  607. username. Review the homepage, repository, license, and `other supported
  608. package.json <https://docs.npmjs.com/files/package.json>`__ fields while
  609. you have the file open. Then open the ``README.md`` file and adjust the
  610. command in the *Installation* section so that it includes the full,
  611. username-prefixed package name you just included in the ``package.json``
  612. file. For example:
  613. .. code:: bash
  614. jupyter labextension install @your-npm-username/jupyterlab_xkcd
  615. Return to your terminal window and make one more git commit:
  616. .. code:: bash
  617. git add .
  618. git commit -m 'Prepare to publish package'
  619. Now run the following command to publish your package:
  620. .. code:: bash
  621. npm publish --access=public
  622. Check that your package appears on the npm website. You can either
  623. search for it from the homepage or visit
  624. ``https://www.npmjs.com/package/@your-username/jupyterlab_xkcd``
  625. directly. If it doesn't appear, make sure you've updated the package
  626. name properly in the ``package.json`` and run the npm command correctly.
  627. Compare your work with the state of the reference project at the
  628. `06-prepare-to-publish
  629. tag <https://github.com/jupyterlab/jupyterlab_xkcd/tree/0.34-06-prepare-to-publish>`__
  630. for further debugging.
  631. |Extension page on npmjs.com|
  632. You can now try installing your extension as a user would. Open a new
  633. terminal and run the following commands, again substituting your npm
  634. username where appropriate
  635. (make sure to stop the existing ``jupyter lab --watch`` command first):
  636. .. code:: bash
  637. conda create -n jupyterlab-xkcd jupyterlab nodejs
  638. source activate jupyterlab-xkcd
  639. jupyter labextension install @your-npm-username/jupyterlab_xkcd
  640. jupyter lab
  641. You should see a fresh JupyterLab browser tab appear. When it does,
  642. execute the *Random xkcd comic* command to prove that your extension
  643. works when installed from npm.
  644. Learn more
  645. ~~~~~~~~~~
  646. You've completed the tutorial. Nicely done! If you want to keep
  647. learning, here are some suggestions about what to try next:
  648. - Assign a hotkey to the *Random xkcd comic* command.
  649. - Make the image a link to the comic on https://xkcd.com.
  650. - Push your extension git repository to GitHub.
  651. - Give users the ability to pin comics in separate, permanent panels.
  652. - Learn how to write `other kinds of
  653. extensions <./extension_dev.html>`__.
  654. .. |Completed xkcd extension screenshot| image:: xkcd_tutorial_complete.png
  655. .. |Empty xkcd extension panel| image:: xkcd_tutorial_empty.png
  656. .. |Single xkcd extension panel| image:: xkcd_tutorial_single.png
  657. .. |Styled xkcd panel with attribution| image:: xkcd_tutorial_complete.png
  658. .. |Extension page on npmjs.com| image:: xkcd_tutorial_npm.png