Reusable code snippets in JupyterLab
Tags: jupyter, python, datascience
Published: 2020-11-13 20:06:49

In my previous blog post _[Automate your machine learning workflow tasks using Elyra and Kubeflow Pipelines](https://dev.to/ibmdeveloper/automate-your-machine-learning-workflow-tasks-using-elyra-and-kubeflow-pipelines-5d6n)_ I've introduced Elyra and the Visual Pipeline Editor, which enables you to assemble machine learning pipelines from notebooks and Python scripts.

In this blog post I'll introduce the Elyra code snippet extension, which enables to to reuse arbitrary snippets of code in your 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

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

### Installing the extension using pip

Install all Elyra extensions:

```
$ pip install elyra && jupyter lab build
```

Install 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
```

Install 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:

![access_code_snippets](https://dev-to-uploads.s3.amazonaws.com/i/or7n0a6mnaok7gnafual.png)

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.

![manage_code_snippets](https://dev-to-uploads.s3.amazonaws.com/i/rhtdj83ywwija1684jg7.png)
  
 

### Creating a snippet

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

![add_snippet](https://dev-to-uploads.s3.amazonaws.com/i/zmnh3ggcafc6e9ahji69.png)

> If your code snippet language is not listed in the drop-down list, type it in the filter box to create a new entry.
 
In Elyra 2.0 we've added the ability to create a code snippet from text. Select the desired text, open the context menu, and choose `Save As Code Snippet`.

### Finding a snippet

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

![locate_snippet](https://dev-to-uploads.s3.amazonaws.com/i/491b8camqqaff85ehue8.png)
 

### 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. 

![insert_code_snippet](https://dev-to-uploads.s3.amazonaws.com/i/7d5ivl8ks6skru9m7tnt.png)
 

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.

### 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](https://jupyter.readthedocs.io/en/latest/use/jupyter-directories.html#id2), 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 Elyra or if you have code snippet editor feedback head on over to https://github.com/elyra-ai/elyra and get a conversation started.