How to re-use code snippets in JupyterLab

Patrick Titzler
IBM Data Science in Practice
4 min readNov 25, 2020

In my previous blog posts Creating notebook pipelines using Elyra and Kubeflow Pipelines and Running notebook pipelines in JupyterLab I’ve introduced Elyra and the Visual Pipeline Editor, which enables you to assemble machine learning pipelines from notebooks and Python scripts without the need for any coding.

In this blog post I’ll focus on the Elyra code snippet extension, which makes it possible to reuse arbitrary snippets of code in notebooks, source code or markdown files in JupyterLab.

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. For me 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 “scratch pad” might help, native support for reusable code or documentation assets in JupyterLab holds a lot more appeal.

Installing the code snippet extension

By popular demand from the community you can now install the code snippet extension as part of Elyra or individually, using pip or conda. Note that a rebuild of JupyterLab is (still) required but future versions of JupyterLab will hopefully remove the need.

Installing the extension using pip

Install either all Elyra extensions (Visual Pipeline editor, Python editor, etc):

$ pip install elyra && jupyter lab build

or only the code snippet extension:

$ pip install elyra-code-snippet-extension && jupyter lab build

Installing the extension using conda

Install all Elyra extensions:

$ conda install -c conda-forge elyra && jupyter lab build

or only the code snippet extension:

$ conda install -c conda-forge elyra-code-snippet-extension && jupyter lab build

Managing and using code snippets

Code snippets are accessed by opening the code snippets tab on the JupyterLab sidebar:

Code snippets are accessed via the JupyterLab sidebar

The code snippets UI is divided into two parts:

  • a quick search panel, and
  • the code snippet list, providing access to code snippet specific actions, such as copy, insert, edit, and delete.
The main code snippet interface

Creating a snippet

To create a snippet click + above the search bar and provide the required information — snippet language and the actual code. You can optionally tag snippets to make them more easily discoverable.

Defining a code snippet

In Elyra 2.0 and later you can also select lines of code in an editor, right click, and choose Save As Code Snippet to create a snippet.

Creating a code snippet from source code within the Notebook editor

Finding a snippet

To locate a code snippet, enter a search term and/or pick one of the listed tags.

Finding a code snippet using keyword search or tags

Copying a snippet to the clipboard

You copy the snippet content to the clipboard by clicking the snippet’s copy icon.

Inserting a snippet

Use drag and drop or click the snippet’s insert icon to paste the snippet content in the desired destination, such as cell in a notebook or an open editor window.

Inserting a code snippet using drag and drop

If you try to insert a code snippet (containing actual code, such as Python) into a destination cell of type markdown, the code is automatically escaped.

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 definition are therefore not reflected in the locations where the snippet was previously embedded.

Editing a snippet

You edit a snippet by clicking the snippet’s pencil icon.

Deleting a code snippet

You delete a code snippet by clicking the snippet’s trash icon.

Sharing code snippets

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.

The definitions are stored in the $JUPYTER_DATA_DIR/metadata/code-snippets directory, where $JUPYTER_DATA_DIR refers to the Jupyter data directory, which you can identify by running the following command in a terminal window:

$ jupyter --data-dir

In my environment, the code snippets are stored in /Users/ptitzler/Library/Jupyter/metadata/code-snippets/. To back up or share my snippets, I typically create an archive from the content of this directory.

Closing thoughts

To learn more about other Elyra extensions for JupyterLab visit https://github.com/elyra-ai/elyra or subscribe to our blog here on medium. We’ll keep you posted on the latest developments. There’s a lot more to come later this year and in early 2021.

--

--

Patrick Titzler
IBM Data Science in Practice

Developer Advocate at Center for Open-Source Data & AI Technologies