Reuse code snippets in Jupyterlab

In my previous article, Create AI pipelines using Elyra and Kubeflow pipelines, I introduced the Elyra open source project and its main feature — the Visual Pipelines editor. In this article, I review the Elyra code snippet extension, which enables you to reuse arbitrary snippets of code in your notebooks, source code, or Markdown files in JupyterLab.

Why is this important? Let’s say you are working on a notebook and want to include a standard header or footer (like licensing text) or code that you previously created. For me, this typically results in a hectic search. 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 (I admit, I sometimes use Slack for that), native support for reusable code or documentation assets in JupyterLab is a better option.

Let me show you how this works.

Install the code snippet extension

You can install the code snippet extension as part of Elyra or individually using pip or conda. At the time of this writing, a rebuild of JupyterLab is required to make the extension available.

Install the extension using pip

You can install the extension using pip in one of two ways:

Install all Elyra extensions using:

$ pip install elyra && jupyter lab build

Or, install only the code snippet extension:

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

Install the extension using Conda

Install all Elyra extensions using conda with this code:

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

Or, install only the code snippet extension:

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

Manage and use code snippets

Once you’ve installed Elyra or the code snippet extension, you can access the code snippets by opening the code snippets tab on the JupyterLab sidebar:


The code snippets UI is divided into two parts:

  • a quick search panel
  • the code snippet list, which provides access to code snippet specific actions, such as copy, insert, edit, and delete


Let’s look at some of the actions you can take using the code snippets UI.

Create a snippet

To create a snippet, click + above the search bar and provide the required information. Optionally, you can tag snippets to make them more easily discoverable.


Find a snippet

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


Copy a snippet to the clipboard

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

Insert a snippet

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.


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.

Edit a snippet

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

Delete a code snippet

To delete a code snippet, click the snippet’s trash icon.

Share 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. You can identify this directory by running the following command in a terminal window:

$ jupyter --data-dir

In my environment, the code snippets are stored in /Users/patti/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

If you have questions about the code snippet extension or run into any problems, please reach out on GitHub or ping us on Gitter. To learn about other Elyra features, check out the documentation.