ArcGIS Notebooks allows you to browse, create, and insert prewritten and 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:
- Open a notebook.
- On the top ribbon of the notebook editor, click Code snippets.
- Click the Add button and choose Create library item.
- Provide a title and summary for the library, and optionally, add tags that represent the code snippets stored in the library.
- Optionally, choose a location under Folder 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 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:
- Open a notebook.
- On the top ribbon of the notebook editor, click Code snippets.
- Click the Add button and choose Add code snippet.
- Provide a title for the code snippet, choose the library you want to add it to, and provide 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.
- 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 options to edit, deprecate, and delete a snippet.
Insert a code snippet
Using the Code snippets pane, complete the following steps to insert a code snippet:
- Open a notebook.
- 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 the notebook as a new cell.
The following is an example of a code snippet that you can add to a 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.
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