Introduced at ArcGIS Enterprise 11.0, ArcGIS Notebook Server allows you to browse and insert prewritten portions of reusable code, known as code snippets, into your notebook.
Create a code snippet
You can create a code snippet from your notebook. Snippets you create can be used in the same notebook or other notebooks. Code snippets you create are stored in notebook code snippet library items, which are used to organize your snippets, and can be shared.
To create a code snippet, you must first create a notebook code snippet library in which to store your snippets. A code snippet library can have a maximum of 100 snippets.
Create a notebook code snippet library
To create a code snippet library, complete the following steps:
- On the top ribbon of the notebook editor, click Code snippets.
- Click the Add button and choose Create library item.
- Enter a title and summary for your library, and optionally, add tags that represent the code snippets stored in the library.
- Optionally, select a folder in My Content in which to save your code snippet library.
- Click Create.
When you create a code snippet library, it is stored in your organization like any other item. You can manage your code snippet libraries by clicking the More options button in the Code snippets pane and clicking Manage library items. From the Manage code snippet library items window, you can create a new code snippet library, and for each code snippet library, you can share, edit, view the item details page, and mark a library as deprecated.
Add a code snippet
Once you have created a library, you can add code snippets to it. To add a code snippet, do the following:
- On the top ribbon of the notebook editor, click Code snippets.
- Click the Add button and choose Add code snippet.
- Enter a title for your code snippet, choose the library you want to add it to, and enter a description.
The Select a code snippet library item drop-down list displays the libraries that you can add your snippet to. The list also indicates the number of snippets in that library.
- Add the code for the snippet by doing one of the following:
- If the notebook uses runtime version 8.0 or later, select the cell you want to add as a code snippet and click Copy from active cell.
- If the notebook uses runtime version 7.0 or earlier, enter the code for the code snippet.
Note:
Copy and paste code from the notebook cell instead of directly typing in the code to avoid indentation or other syntax errors.
- Click Save.
You can make changes to your code snippets by clicking the More options button for the snippet. The More options drop-down menu provides the edit, deprecate, and delete options that allow you to modify a snippet.
Insert a code snippet
Using the Code snippets pane, complete the following steps to insert a code snippet:
- On the top ribbon of the notebook editor, click Code snippets.
- Browse to the code snippet library that contains the snippet you want to insert.
- Click View details for the snippet to see a preview of the code.
- When you have found the code snippet you want to insert, either click the Add button in the code snippets pane or click Insert into notebook in the code preview pane to insert the code snippet into your notebook as a new cell.
The following is an example of a code snippet that is added to your notebook:
# curated code snippet inserted from the Code snippets pane
from arcgis.gis import GIS
def check_org_credits_agol(gis: GIS):
if "admin" in gis.__dict__:
credits = gis.admin.credits.credits
print(f"This ArcGIS Online organization has {credits} credits.")
else:
print("Please connect to the ArcGIS Online organization as an administrator to check credits.")
# create a GIS instance to pass to the code snippet function
gis_agol = GIS("https://www.arcgis.com/", "<admin_username>")
# call the code snippet function with your GIS instance
check_org_credits_agol(gis_agol)
Esri code snippets
All available code snippets are curated by Esri. These curated code snippets allow you to implement common GIS workflows in your notebooks. The code snippets are written as Python functions that can be inserted into your notebook and called throughout it. A code snippet can also be edited once it has been added to your notebook.
Note:
At 11.0, the names and descriptions of curated code snippets are only available in English.
The following are available code snippets that can be inserted into your notebook:
- GIS administration—Code snippets that perform common GIS administration tasks.
Note:
Many of these code snippets require an administrator connection.
- Check remaining credits (ArcGIS Online only).
- Check the credits of a specific user (ArcGIS Online only).
- Get the base service URL of a layer item.
- Query failed notebook tasks
- Query portal logs and save as CSV (ArcGIS Enterprise only).
- Sign in to portal for ArcPy.
- Content management—Code snippets that help in managing the content in your GIS and the files in your notebook workspace directory.
- Copy a geodatabase in the workspace directory.
- Create a file geodatabase with an empty feature class.
- Create a folder in the workspace directory.
- Download a file item from portal to the workspace directory.
- List datasets.
- List files.
- Remove a file geodatabase in the workspace directory.
- Remove a folder in the workspace directory.
- Unzip a file to the workspace directory.
- Data science and analysis—Code snippets that can be implemented in your data science and analysis workflows using ArcGIS API for Python, ArcPy, and other Python libraries.
Note:
Use the advanced notebook runtime to run code snippets that require ArcPy. For more information on notebook runtimes, see Specify the runtime of a notebook.
- Convert a Feature Class to a Feature Layer.
- Convert a Feature Class to a Spatially Enabled DataFrame.
- Convert a Feature Layer to a Spatially Enable DataFrame.
- Convert a Spatially Enabled DataFrame to a Feature Class.
- Display an image from a URL.
- Display an image from the workspace directory.
- Fill missing values.
- List fields in a feature class.
- List fields in a layer.
- List layers in an item.
- Plot a Spatially Enable DataFrame (quickstart).
- Plot a Spatially Enable DataFrame with class break renderer.
- Plot a Spatially Enable DataFrame with simple renderer.
- Plot a Spatially Enable DataFrame with unique value renderer.
- Publish a Spatially Enabled DataFrame as a Feature Layer.