<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Jupyter Blog - Patrick Titzler</title><link href="https://jupyter.org/blog/" rel="alternate"/><link href="https://jupyter.org/blog/feeds/author-patrick-titzler.atom.xml" rel="self"/><id>https://jupyter.org/blog/</id><updated>2020-11-19T15:38:00+00:00</updated><subtitle>The Project Jupyter blog: news, releases, and community stories, archived from blog.jupyter.org.</subtitle><entry><title>Reusable code snippets in JupyterLab</title><link href="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/" rel="alternate"/><published>2020-11-18T18:08:00+00:00</published><updated>2020-11-19T15:38:00+00:00</updated><author><name>Patrick Titzler</name></author><id>tag:jupyter.org,2020-11-18:/blog/posts/2020/reusable-code-snippets-in-jupyterlab/</id><summary type="html">&lt;p&gt;In this blog post, we will introduce the Elyra code snippet extension, which enables us to reuse arbitrary snippets of code in your notebooks, source code, or markdown files in JupyterLab.&lt;/p&gt;</summary><content type="html">&lt;figure&gt;
&lt;img alt="Reusable code snippets in JupyterLab" src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/001-1_bF2CvMkAeI9QB_DD29M9Ow.webp" loading="lazy" data-body-image=""&gt;
&lt;figcaption&gt;Reusable code snippets in JupyterLab&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;In this blog post, we will introduce the &lt;a href="https://github.com/elyra-ai/elyra"&gt;Elyra&lt;/a&gt; &lt;a href="https://elyra.readthedocs.io/en/latest/user_guide/code-snippets.html"&gt;code snippet extension&lt;/a&gt;, which enables us to reuse arbitrary snippets of code in your notebooks, source code, or markdown files in JupyterLab.&lt;/p&gt;
&lt;p&gt;Let’s say you are working on a notebook and want to include a standard header or footer (like licensing text) or some code that you’ve previously created. This typically results in a hectic search. In which notebook did I use the code? Where’s the most current version of the disclaimer I need to add? While maintaining a “scratchpad” might help, native support for reusable code or documentation assets in JupyterLab holds a lot more appeal.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt="Code snippet ui" src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/002-1_cR1hInF2oXYu8hbosLx78w.mp4" loading="lazy" data-body-image=""&gt;
&lt;figcaption&gt;Code snippet ui&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id="installing-the-code-snippet-extension"&gt;Installing the code snippet extension&lt;/h2&gt;
&lt;p&gt;The code snippet extension can be installed as part of Elyra or individually using &lt;code&gt;pip&lt;/code&gt; or &lt;code&gt;conda&lt;/code&gt;. In the context of this article, see below how to install the code snippet extension in your existing JupyterLab deployment.&lt;/p&gt;
&lt;h3 id="installing-the-extension-using-pip"&gt;Installing the extension using pip&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;pip&lt;span class="w"&gt; &lt;/span&gt;install&lt;span class="w"&gt; &lt;/span&gt;elyra-code-snippet-extension&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;jupyter&lt;span class="w"&gt; &lt;/span&gt;lab&lt;span class="w"&gt; &lt;/span&gt;build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id="installing-the-extension-using-anaconda"&gt;Installing the extension using Anaconda&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;conda&lt;span class="w"&gt; &lt;/span&gt;install&lt;span class="w"&gt; &lt;/span&gt;-c&lt;span class="w"&gt; &lt;/span&gt;conda-forge&lt;span class="w"&gt; &lt;/span&gt;elyra-code-snippet-extension&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;jupyter&lt;span class="w"&gt; &lt;/span&gt;lab&lt;span class="w"&gt; &lt;/span&gt;build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id="managing-and-using-code-snippets"&gt;Managing and using code snippets&lt;/h2&gt;
&lt;p&gt;Code snippets are accessed by opening the &lt;code&gt;code snippets&lt;/code&gt; tab on the JupyterLab sidebar:&lt;/p&gt;
&lt;p class="standalone-image"&gt;&lt;img src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/003-0_Fie0v3ppwrmiRTGX.webp" alt="" loading="lazy" data-body-image=""&gt;&lt;/p&gt;
&lt;p&gt;The code snippets UI is divided into two parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a quick search panel, and&lt;/li&gt;
&lt;li&gt;the code snippet list, providing access to code snippet specific actions, such as copy, insert, edit, and delete.&lt;/li&gt;
&lt;/ul&gt;
&lt;p class="standalone-image"&gt;&lt;img src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/004-0_engxXB3Tz8HmkISW.webp" alt="" loading="lazy" data-body-image=""&gt;&lt;/p&gt;
&lt;h2 id="creating-a-snippet"&gt;Creating a snippet&lt;/h2&gt;
&lt;p&gt;To create a snippet click &lt;code&gt;+&lt;/code&gt; above the search bar and provide the required information. You can optionally tag snippets to make them more easily discoverable.&lt;/p&gt;
&lt;p class="standalone-image"&gt;&lt;img src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/005-0_yIlAqD2eSLLkNo-0.webp" alt="" loading="lazy" data-body-image=""&gt;&lt;/p&gt;
&lt;h2 id="finding-a-snippet"&gt;Finding a snippet&lt;/h2&gt;
&lt;p&gt;To locate a code snippet, enter a search term and/or pick one of the listed tags.&lt;/p&gt;
&lt;p class="standalone-image"&gt;&lt;img src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/006-0_oyIjiBDeFhi0y4It.webp" alt="" loading="lazy" data-body-image=""&gt;&lt;/p&gt;
&lt;h2 id="copying-a-snippet-to-the-clipboard"&gt;Copying a snippet to the clipboard&lt;/h2&gt;
&lt;p&gt;You copy the snippet content to the clipboard by clicking the snippet’s &lt;em&gt;copy&lt;/em&gt; icon.&lt;/p&gt;
&lt;h2 id="inserting-a-snippet"&gt;Inserting a snippet&lt;/h2&gt;
&lt;p&gt;Click the snippet’s insert icon to paste the snippet content in the desired destination, such as a cell in a notebook or an open editor window. Drag-and-Drop of the snippet has also been incorporated in the latest Elyra release.&lt;/p&gt;
&lt;p class="standalone-image"&gt;&lt;img src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/007-0_QdPljYYKAYvqaP0Q.webp" alt="" loading="lazy" data-body-image=""&gt;&lt;/p&gt;
&lt;p&gt;Do note that the editor does not keep track of where you’ve inserted a snippet. Also, note that snippets don’t act like macros and changes to a snippet are therefore not reflected in the locations where the snippet was embedded.&lt;/p&gt;
&lt;h2 id="editing-a-snippet"&gt;Editing a snippet&lt;/h2&gt;
&lt;p&gt;You edit a snippet by clicking the snippet’s pencil icon.&lt;/p&gt;
&lt;h2 id="deleting-a-code-snippet"&gt;Deleting a code snippet&lt;/h2&gt;
&lt;p&gt;You delete a code snippet by clicking the snippet’s trash icon.&lt;/p&gt;
&lt;h2 id="sharing-code-snippets"&gt;Sharing code snippets&lt;/h2&gt;
&lt;p&gt;The code snippet extension currently doesn’t provide any import or export capabilities. However, you can access the relevant metadata files that contain the definitions.&lt;/p&gt;
&lt;p&gt;The definitions are stored in the &lt;code&gt;$JUPYTER_DATA_DIR/metadata/code-snippets&lt;/code&gt; directory, where &lt;code&gt;$JUPYTER_DATA_DIR&lt;/code&gt; refers to the &lt;a href="https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html#id2"&gt;Jupyter data directory&lt;/a&gt;, which you can identify by running the following command in a terminal window:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;jupyter&lt;span class="w"&gt; &lt;/span&gt;--data-dir
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In my environment, the code snippets are stored in &lt;code&gt;/Users/ptitzler/Library/Jupyter/metadata/code-snippets/&lt;/code&gt;. To back up or share my snippets, I typically create an archive from the content of this directory.&lt;/p&gt;
&lt;h2 id="try-code-snippet-from-binder"&gt;Try Code Snippet from Binder&lt;/h2&gt;
&lt;p&gt;To experiment with the code snippet extension without installing it locally, just click on the binder link below and then look for the code-snippet icon on the JupyterLab side-bar :&lt;/p&gt;
&lt;figure&gt;
&lt;a href="https://mybinder.org/v2/gh/elyra-ai/elyra/v1.4.1?urlpath=lab/tree/binder-demo"&gt;&lt;img alt="Try the code snippet extension online on Binder" src="https://jupyter.org/blog/posts/2020/reusable-code-snippets-in-jupyterlab/images/008-0_BdqSU9blQk8YG6kC.webp" loading="lazy" data-body-image=""&gt;&lt;/a&gt;
&lt;figcaption&gt;Try the code snippet extension online on Binder&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id="acknowledgments"&gt;Acknowledgments&lt;/h2&gt;
&lt;p&gt;We’d like to thank &lt;a href="https://www.linkedin.com/in/zach-sailer-8a1472151/"&gt;Zach Sailer&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/tgeorgeux/"&gt;Timothy George&lt;/a&gt; for all his UX contributions in conjunction with &lt;a href="https://www.linkedin.com/in/jay-jaewook-ahn/"&gt;JaeWook (Jay) Ahn&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/kiran-pinnipati/"&gt;Kiran Pinnipati&lt;/a&gt;, and &lt;a href="https://www.linkedin.com/in/ai-vyndang/"&gt;Ai-Vy Dang&lt;/a&gt; the CalPoly interns that helped to implement these UX enhancements.&lt;/p&gt;
&lt;h2 id="closing-thoughts"&gt;Closing thoughts&lt;/h2&gt;
&lt;p&gt;Elyra is constantly making Jupyter Notebooks better aiming to help data scientists, machine learning engineers, and AI developers through the model development life cycle complexities, making JupyterLab even better for AI practitioners.&lt;/p&gt;
&lt;p&gt;Come and join our &lt;a href="https://github.com/elyra-ai/elyra#weekly-dev-community-meeting"&gt;weekly dev meetings&lt;/a&gt; to learn more about the most recent updates to Elyra and to ask your questions to the community, including the project core developers.&lt;/p&gt;
&lt;p&gt;We would also appreciate for you to get involved with the &lt;a href="https://github.com/elyra-ai/elyra/"&gt;Elyra project&lt;/a&gt;. Read our contributing guidelines, &lt;a href="https://github.com/elyra-ai/elyra/issues"&gt;create new issues&lt;/a&gt; if you have questions, suggestions for new features, or to report any bugs. We also welcome contributions via GitHub &lt;a href="https://github.com/elyra-ai/elyra/pulls"&gt;pull requests&lt;/a&gt;. And last but not least, don’t forget to star the project on Github.&lt;/p&gt;
</content><category term="Elyra"/><category term="JupyterLab"/><category term="machine learning"/></entry></feed>