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.
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.
-
Enable snippet edit mode by clicking the pencil icon in the context menu:
-
Enter the snippet code and click on
. -
Enter the name of the snippet and select the repository to store the snippet. Click the
button. -
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.
-
Hover over the icon to show a popup menu. Select the Insert snippet item from the menu.
-
Navigate through the folders until you find the snippet you’re looking for. Select a snippet to see its contents.
-
Click the
button to insert the snippet into the active cell.
When snippets are updated in a Git repository, you must pull in the changes to see the updates in your workspace.
-
Click on pull in the Imported Repositories section.
-
Click refresh to reload snippets.