Skip To Content

Use Python code snippets

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:

  1. On the top ribbon of the notebook editor, click Code snippets.
  2. Click the Add button Add and choose Create library item.
  3. Enter a title and summary for your library, and optionally, add tags that represent the code snippets stored in the library.
  4. Optionally, select a folder in My Content in which to save your code snippet library.
  5. 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 More 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:

  1. On the top ribbon of the notebook editor, click Code snippets.
  2. Click the Add button Add and choose Add code snippet.
  3. 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.

  4. 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.

  5. Click Save.

You can make changes to your code snippets by clicking the More options button More 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:

  1. On the top ribbon of the notebook editor, click Code snippets.
  2. Browse to the code snippet library that contains the snippet you want to insert.
  3. Click View details for the snippet to see a preview of the code.
  4. When you have found the code snippet you want to insert, either click the Add button Add 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:

  1. 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
  2. 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
  3. 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