DEV Community

loading...
IBM Developer

Reusable code snippets in JupyterLab

ptitzler profile image Patrick Titzler Originally published at developer.ibm.com Updated on ・3 min read

In my previous blog post Automate your machine learning workflow tasks using Elyra and Kubeflow Pipelines 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
Enter fullscreen mode Exit fullscreen mode

Install only the code snippet extension:

$ pip install elyra-code-snippet-extension && jupyter lab build
Enter fullscreen mode Exit fullscreen mode

Installing the extension using conda

Install all Elyra extensions:

$ conda install -c conda-forge elyra && jupyter lab build
Enter fullscreen mode Exit fullscreen mode

Install only the code snippet extension:

$ conda install -c conda-forge elyra-code-snippet-extension && jupyter lab build
Enter fullscreen mode Exit fullscreen mode

Managing and using code snippets

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

access_code_snippets

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

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

Finding a snippet

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

locate_snippet

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.

snippets_drag_and_drop

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, which you can identify by running the following command in a terminal window:

$ jupyter --data-dir
Enter fullscreen mode Exit fullscreen mode

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.

Discussion

pic
Editor guide