Code Assist snippets

Code snippets are reusable bits of code that can be easily inserted into a notebook. Code snippets offer numerous benefits to developers, and can be used to:

  • automate repetitive tasks

  • save time

  • reduce errors

  • ensure consistent coding styles

The following sections will show you how to add, use, edit, and delete snippets in notebook workspaces.

Add and edit snippets

Code Assist looks for snippets in a snippets/ directory in the root of the repository or project. Any .py or .R files in this directory are loaded as snippets, and the sub-folder structure is maintained in the Snippets menu.

Here’s an example structure of the snippets/ folder:

snippets/
├── python
│   ├── general
│   │   └── sort-dictionary.py
│   └── models
│       ├── classification.py
│       └── regression.py
└── r
    ├── general
    │   └── list-to-data-frame.R
    └── markdown
        ├── letter.Rmd
        ├── proposal.Rmd
        └── report.Rmds

The following sections show the two ways to add new snippets:

  • Import from a Git repository

  • Manually add a snippet

Import snippets from a Git repository

A snippet library can be added by linking a Git repository with a snippets/ directory in its root to your Domino project.

Use standard Git workflows to edit snippets in your repository.

Manually add and edit snippets

You can also add snippets directly in a workspace.

  1. Enable snippet edit mode by clicking the pencil icon in the context menu:

    Click the pencil icon in the context menu
  2. Enter the snippet code and click on SAVE AS SNIPPET.

  3. Enter the name of the snippet and select the repository to store the snippet. Click the Add button.

  4. To sync the snippet back to Git, open File Changes in the left-hand sidebar and click Select and sync on the Imported Repositories section.

Use snippets

  1. Hover over the assistant icon icon to show a popup menu. Select the Insert snippet item from the menu.

    Select the Insert snippet item
  2. Navigate through the folders until you find the snippet you’re looking for. Select a snippet to see its contents.

  3. Click the RUN button to insert the snippet into the active cell.

The snippet was inserted

Refresh snippets with git pull

When snippets are updated in a Git repository, you must pull in the changes to see the updates in your workspace.

  1. Click on pull in the Imported Repositories section.

    Click pull under Imported Repositories
  2. Click refresh to reload snippets.

    Refresh

Delete snippets

  1. Choose Insert snippet in the Code Assist menu.

  2. Choose the snippet that you want to delete and click the DELETE button.