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.

Insert a code snippet

Using the Code snippets pane, insert a code snippet by following the steps below:

  1. In the top ribbon of the notebook editor, click Code snippets.
  2. Click the name of the code snippet you want to insert to see a preview of the code.
  3. When you have found the code snippet you want to insert, either click the Add button Add in the code snippets pane or the Add to notebook button 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 in to 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 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 you 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 the 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 layers in an item.
    • Plot a Spatially Enable DataFrame with class break renderer.
    • Plot a Spatially Enable DataFrame (quickstart).
    • 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.