Get started with notebooks in ArcGIS Pro

Set up a project and review datasets

Before you begin writing Python code, you will download the datasets, create a project, and review the datasets to be used.

  1. Download the data for this tutorial and extract the contents to a location your computer.

    The .zip file contains a folder named NotebookStart.

    In this tutorial the data is shown at C:\Tutorials\NotebookStart\. You can use a different folder, but be sure to adjust the paths in the instructions that follow.

  2. Start ArcGIS Pro. If prompted, sign in using your licensed ArcGIS organizational account.
    Note:

    If you don't have access to ArcGIS Pro or an ArcGIS organizational account, see options for software access.

  3. Click Open another project.

    Open another project

  4. In the Open Project window, browse to the folder you extracted from the NotebookStart.zip file, click Notebooks Getting Started.aprx to select it, and click OK.

    The project opens. The current map shows the boundary of the city of Toronto in Canada. You will add several feature classes to this map.

  5. If the Catalog pane is not already visible, click the View tab and click Catalog Pane.
  6. Dock the Catalog pane to the right of the map.
  7. In the Catalog pane, expand Folders and expand NotebookStart.
  8. Expand the Toronto.gdb geodatabase.

    Toronto geodatabase expanded to show feature classes

    The geodatabase contains several feature classes.

  9. Right-click the etobicoke feature class and click Add To Current Map.

    These polygons represent several communities within the city of Toronto.

  10. Add the fire_stations and greenspace feature classes to the same map.

    These feature classes represent the location of fire stations and green space within the city of Toronto, respectively. You will use these feature classes to run various geoprocessing tools using Python.

  11. The NotebookStart folder also contains a geodatabase named Notebooks Getting Started.gdb, which is the default geodatabase for the project and is currently empty.

Create a notebook and run Python code

In this section, you will create a notebook in ArcGIS Pro and use it to run some Python code.

  1. On the ribbon, click the Analysis tab, and in the Geoprocessing group, click the drop-down arrow for the Python button and click Python Notebook.

    Open a new Python notebook in ArcGIS Pro.

    Clicking the Python button also opens a new notebook, but the drop-down menu allows you to see that you have a choice between Python Notebook and Python Window. The Python Window is another way to run Python code in ArcGIS Pro.

    It may take a moment for the new notebook to appear, and you may see the message Initializing Kernel on the screen while you wait. This means ArcGIS Pro is getting ready to run code in the notebook. The kernel is software running in the background that will run the Python code you enter in the notebook.

    Once the notebook opens, it appears as a new view in the main window of ArcGIS Pro.

    The new notebook is stored as an .ipnyb file in your project home folder. The new notebook also appears under the Notebooks folder in the Catalog pane.

  2. Click inside the empty cell in the notebook.

    A new notebook appears in ArcGIS Pro and the cell turns green when you click it.

    The outline turns green.

  3. Type the following line of code:

    print("Hello Notebook!")

    Add code to print Hello Notebook!

    This code calls the print function on a single input parameter within the parentheses. This parameter is a string, because it is enclosed in quotation marks. Strings are an important and useful type of data that you can work with in Python.

  4. On the toolbar above the cell, click the Run button.

    Click the Run button.

    The code in the cell is run and the result is printed below the cell. The print function runs on the string value "Hello Notebook!" and prints it. The quotation marks are not printed, because they are not part of the string—they only identify it as a string. The number 1 appears in the brackets to the left of the cell. A new empty cell is added below.

    Hello Notebook appears below the cell, and a new cell is added.

    You can also run the currently selected cell by pressing Ctrl+Enter.

    You can add multiple lines of code within a single cell by pressing the Enter key after each line. This may be counterintuitive if you are used to running code in the Python window or in the interactive window of a Python editor, where pressing the Enter key results in running the line of code.

  5. In the cell below your Hello Notebook! code, type the following lines of code:

    a = 5
    b = 7
    c = 9
    print(a * b * c)

    This code creates three variables, a, b, and c, and assigns their values to be equal to the numbers 5, 7, and 9. The last line prints the result of multiplying the variables.

    Cell with three variables set and a line to multiply them

  6. Press Ctrl+Enter.

    Results after the second cell runs

    After the cell runs, the value 315 appears below it. The number 2 appears in brackets to the left of the cell to indicate that this is the second cell run.

  7. At the top of the notebook, click the Insert menu, and click Insert Cell Below.

    Insert Cell Below

    A new cell is added below the currently selected cell.

  8. In the new cell, type the following line of code:

    a * b * c

    What do you think this will do when you run the cell?

  9. Run the cell and look at the result.

    Did the result match your expectation?

    The values of the variables a, b, and c are stored in memory after you set them, so you can use them in another cell.

    A times b times c

    The result of multiplying the values stored in a, b, and c is printed when you run the cell.

    The print statement is not required because in a notebook, the Python window, and in the Python interpreter, Python evaluates simple expression statements and prints their value.

  10. If you ran the cell by pressing Ctrl+Enter, insert a new cell below it.

    If you ran the cell by pressing the run button, there should already be a new cell below it.

  11. In the new cell, type the following line of code:
    a * t

    What do you think this will do when you run the cell?

  12. Run the cell and look at the result.

    Did the result match your expectation?

    Variable a has been set to the numeric value of 5, but the new variable t hasn't been set yet. This causes an error. In Python, you can't use variables until you assign their values.

    The error states that name t is not defined.

  13. Edit the code in the cell as follows:

    a * "t"

    What do you think this will do when you run the cell?

  14. Run the cell and look at the result.

    Did the result match your expectation?

    Result of a times string t

    By putting t in quotation marks, you have identified it for Python as a string. Python evaluates the expression as the string t five times, resulting in the new string value 'ttttt'.

  15. Add a new cell and enter the following code:

    t = 10
    a * t

    What do you think this will do when you run the cell?

  16. Run the cell.

    Did the result match your expectation?

    Set t and multiply a by t

    Because the first line in the cell defined the variable t as being equal to the number 10, Python was able to multiply it by the value stored in the variable a.

You've opened a new notebook in ArcGIS Pro and added and run some basic Python code. Next, you will use notebook functions to manage the code in the cells.

Manage code in cells

The code in Notebooks is run in cells. The order in which cells have been run is indicated by the numbers beside the cells after they are run. Notebooks have tools to manage cells. Now you'll explore these aspects of working with Python in a notebook.

  1. In the next empty cell (add a new one if you need to), type the following line of code and run the cell.

    mylist = [1, 2, 3, 4, 5]

    What happened?

    This code defined a new variable and set its value, but did not print anything.

    The variable mylist is defined.

    The variable mylist is a list, as indicated by the square brackets. Lists are an important data type in Python that consist of a sequence of elements. In this case, those elements are numbers, but lists can also contain other types of data. Elements in a list are separated by commas.

  2. In the next empty cell, type the following code and run the cell.

    mylist[-1]

    What happened?

    Variable mylist at index position -1

    Elements in a list are indexed, starting with index number zero. You can obtain specific elements in a list by using their index number. Index number -1 means the first elements starting from the end of the list—in other words, the last element. This returns the number 5.

    As you have seen, the cell input and output prompts show a number after the cell has been run. This number starts at 1 and increases for additional cells. The number increases every time you run a cell, including when you run a previously run cell again. The numbers help you keep track of the order in which the cells were run.

  3. Change the code in the cell defining the mylist variable to the following by adding more elements, but don’t run the cell.

    mylist = [1, 2, 3, 4, 5, 6, 7, 8]

    Code to redefine mylist not yet run.

  4. Click the cell below this one, with the code mylist[-1] and click the Run button.

    Does what happened match what you expected?

    Run the code returning the last item again.

    The result is the number 5. Why isn't it the number 8?

    Code in a notebook is entered cell by cell and any previously used variables are stored in memory.

    Until you run the cell with the code that redefines the mylist variable, the value of mylist is still the value stored in memory, [1, 2, 3, 4, 5], and the value at position -1 in that list is still 5.

  5. Click the cell with mylist = [1, 2, 3, 4, 5, 6, 7, 8] and run it.
  6. Click the cell with mylist[-1] and run it.

    Now the value in the last position of the list is 8.

    Update the list variable and print the last index position.

    An alternative to running individual cells is to select multiple cells and run them together, or to run all the cells in a notebook by clicking the Cell menu and clicking Run All.

    It is a good practice to organize lines of code that belong together in the same cell. For example, it would make sense for the two previous cells to be combined into a single cell. You can manually copy and paste code from one cell to another, but you can also combine cells.

  7. Click the cell that defines the mylist variable.

    That cell has mylist = [1, 2, 3, 4, 5, 6, 7, 8] in it.

  8. Click the Edit menu and click Merge Cell Below.

    Merge Cell Below

    The result is a single cell with the combined lines of code. The results below the cells have been removed. An empty line is added between the lines of code from the two merged cells, but you can edit the cell to remove it if you want.

  9. Run the merged cell.

    Run the merged cell.

    The Edit menu provides many other useful ways to manipulate the cells in your notebook. You can copy and paste cells, delete them, split and merge them, and move a selected cell up or down relative to the other cells.

    Additional tools are available under the View, Insert, and Cell menu options.

    Some of the most widely used tools are also available as buttons on the notebook toolbar.

    Notebook toolbar

    These include the following:

    • Insert cell below
    • Cut selected cells
    • Copy selected cells
    • Paste cells below
    • Move selected cells up
    • Move selected cells down

    More tools can be found on the Command Palette.

  10. Click the Command Palette button.

    Click the Command Palette button.

    A list of commands appears.

    You can run a command by clicking it. The command is applied to the selected cells in the notebook, or to all cells, depending on the command.

  11. In the Command Palette, click clear all cells output.

    Click clear all cells output.

    All code remains the same, but all outputs have been removed. The input and output prompts are blank, since none of the cells have been run. When you run a cell, the prompts start again at 1.

    The Command Palette also show shortcuts for many of the tasks.

  12. Click the Command Palette button and scroll down to insert cell below.

    Command Palette insert cell below

    The keyboard shortcut for this command is listed to the right of it. The shortcut is the letter B when the notebook cell is in command mode.

  13. Hide the Command Palette by clicking outside of the palette but inside the notebook.
  14. Click the space to the left of the second cell, so it turns blue.

    Click to the left of the cell to select in command mode.

    Be sure not to click inside the code section, which will turn the cell green.

    The cell border is blue to indicate that it is in command mode.

  15. Press the B key on your keyboard.

    A new cell is added below the selected cell.

    A new cell is inserted below the selected cell. If the cell had been green, you would add the letter b to the code in the cell.

    These shortcuts are not case sensitive, so b and B are the same.

    There is no need to memorize these commands, but experienced coders memorize and use some of them to speed up their work. For most basic tasks, the buttons and menu options in the notebook work well.

    If you are looking for the command for a specific task, you can search for it by using the search bar at the top of the Command Palette.

  16. Open the Command Palette and type run in the Search box.

    Search the Command Palette for run.

    This filters the list to the tools with run in their name. Some commands have a shortcut. For example, the shortcut for run selected cells is Ctrl +Enter.

  17. Hide the Command Palette by clicking outside of the palette but inside the notebook.
  18. Close the notebook.

    Close the notebook.

You've seen how to enter and edit Python code in notebook cells, and how to interact with the notebook to run and manage the code. Next, you'll use a notebook to run geoprocessing tools in ArcGIS Pro.

Run geoprocessing tools in a notebook

Now that you've had some practice entering code in a notebook, it is time to use some geoprocessing tools. You will start with a new notebook.

  1. Click the Analysis tab, and in the Geoprocessing group, click Python.
    Click Analysis and click Python.

    The new notebook opens.

  2. In the Catalog pane, expand the Notebooks section.
  3. Right-click the new notebook, New Notebook (1).ipynb, and click Rename.

    Rename notebook.

  4. Type geoprocessing_demo and press Enter.

    The new notebook is renamed. In the Catalog pane, you can see that the .ipynb file extension was automatically added to the name. The notebook tab now says geoprocessing_demo.

    For the next steps, it is useful to see the map and the notebook side by side.

  5. Drag the geoprocessing_demo notebook tab to the docking target that appears below.

    Dock the notebook tab below the map.

    The notebook is docked below the map. Now you'll be able to see the results of your code as you use Python in the notebook to work with feature classes on the map.

  6. In the empty cell, type the following line of code and run the cell:

    import arcpy

    This line of code imports the ArcPy package. ArcPy is a Python package that makes much of the functionality of ArcGIS Pro available from within Python, including geoprocessing.

    Since you are using this notebook in ArcGIS Pro, code that uses geoprocessing tools will not produce an error if you have not imported ArcPy. However, it is good practice to always include import arcpy at the top of your geoprocessing code so it will also work when run outside of ArcGIS Pro.

  7. In the same cell, add a new line and type the following code:

    arcpy.GetCount_management("fire_stations")

    This code uses ArcPy to run the Get Count tool to determine the number of features in the fire_stations feature class.

  8. Run the cell.

    Get Count tool run in notebook

    GetCount is a function of ArcPy that runs the Get Count geoprocessing tool located in the Data Management Tools toolbox.

    The result appears below the code cell. There are 84 rows (features) in the feature class. These results are very similar to the messages you see after running a tool using the tool dialog box in ArcGIS Pro. Notebooks are integrated in the geoprocessing framework of ArcGIS Pro. This means that running a tool in a notebook is similar to running a tool using the tool dialog box. Any tool that you run in a notebook also appears in the History pane.

  9. On the ribbon, click the Analysis tab. In the Geoprocessing group, click History.

    The tool appears in the geoprocessing history.

    The tool appears in the geoprocessing history.

  10. Close the History pane.
  11. Edit the arcpy.GetCount code line to the following:

    arcpy.GetCount_management("ambulances")

  12. Run the cell.

    This code fails with an error message. At the end of the message, the following information appears:

    ExecuteError: Failed to execute. Parameters are not valid.
    ERROR 000732: Input Rows: Dataset ambulances does not exist or is not supported
    Failed to execute (GetCount).

    Why did the code fail, when the code to get the count of fire stations worked?

    The fire_stations feature class is a layer on the active map. In a notebook, you can refer to a dataset by the name of the layer in the active map, like you can when you run a geoprocessing tool interactively using its graphical user interface.

    The ambulances feature class is not present as a layer in the active map, and it is also not a feature class in the default geodatabase for the project. You can refer to a feature class that is not in the active map or default geodatabase by specifying the full path to it.

    Next, you'll look up the path to the ambulances feature class.

  13. In the Catalog pane, expand the Databases section and expand Toronto.gdb.
  14. Right-click ambulances and click Copy Path.

    Copy Path

    The file path to the ambulances feature class is copied. This path includes the feature class name as well.

    The path in this example is as follows:

    C:\Tutorials\NotebookStart\Toronto.gdb\ambulances

    The path on your computer will be different, depending on where and how you unzipped the .zip file with the data. You may not have put the data in a folder on your drive C, or inside a folder named Tutorials, for example. You should use the path on your computer in the next step.

  15. Click in the notebook cell and highlight ambulances. Paste the path that you copied.
    arcpy.GetCount_management("C:\Tutorials\NotebookStart\Toronto.gdb\ambulances")

    The cell is almost ready to run. The path looks right, but it isn't. There is something missing.

  16. Click immediately after the open parenthesis and before the first quotation mark, and type the letter r.
    arcpy.GetCount_management(r"C:\Tutorials\NotebookStart\Toronto.gdb\ambulances")

    You need to add the letter r to tell Python that this path is a raw string. Windows computers use the backslash character as a path separator. In Python, the backslash character is an escape character that, when next to some other characters in a string, encodes tab, new line, or other special characters. This means that where \N occurs beside \NotebookStart in the path, Python reads the string as having a new line character. Placing the r before the string tells Python to ignore the escape characters.

  17. Run the cell.

    The Get Count tool runs and returns a message that there are 48 features in the feature class.

    You can also use a forward slash (/) character as a path separator in Python code, or you can double the backslash characters.

    The following are all valid ways of writing this path in Python:

    r"C:\Tutorials\NotebookStart\Toronto.gdb\ambulances"
    "C:/Tutorials/NotebookStart/Toronto.gdb/ambulances"
    "C:\\Tutorials\\NotebookStart\\Toronto.gdb\\ambulances"

    If you use a forward slash (/) or double backslash (\\) as your path separator, you do not add the r before the path string.

    For someone who is used to Windows paths delimited by backslashes, this can look a little strange at first, but it is important to remember.

    One way to avoid having to specify full paths for tools is to set the workspace.

  18. Edit the code to add a new line between the two lines beginning with import arcpy and arcpy.GetCount. Add the following line:
    arcpy.env.workspace =

    Add line beginning to define the workspace.

    This line is setting a property of the environment class, arcpy.env, to be equal to some value. Next, you'll cut the path to Toronto.gdb and paste it after this code to set the path.

  19. Cut the path to the geodatabase, including the .gdb extension, from the arcpy.GetCount line and paste it after the equal sign.
    arcpy.env.workspace = r"C:\Tutorials\NotebookStart\Toronto.gdb

    Environment setting line almost done

    Remember that your path may be different from what is shown here. Use the path from your own computer.

    There are still some things that need to be changed. Can you find them?

    If you run the code now, you will get a syntax error like this one:

    EOL syntax error

    EOL means end of line, and the message means that Python reached the end of the line of code while processing the string on line 2. The path string needs another quotation mark to finish it.

  20. Add a quotation mark to finish the string.

    The line should now be as follows:

    arcpy.env.workspace = r"C:\Tutorials\NotebookStart\Toronto.gdb"

    String now closed

    The second line is now complete and correct. Can you see what else needs to be changed?

  21. Change the backslash character before ambulances to a quotation mark.

    Ready to run now that ambulances is a string.

    The code is now ready to run.

  22. Run the cell.

    It should run correctly and report the number of features in the ambulances feature class.

    There are several feature classes in the Toronto geodatabase. You can get counts for any of them now.

  23. Insert a new cell in the notebook, copy and paste the GetCount line to a new cell, change the name to bikeways, and then run the cell.
    arcpy.GetCount_management("bikeways")

    Get count of bikeways.

    You may want to know the count of each feature class in the geodatabase. You can copy the cell and edit the name of the feature class, but Python allows you to get a list of all of the feature classes and then run the GetCount function on them.

  24. Insert a new cell in the notebook and add the following code:
    # List the feature classes in a workspace
    # works on the current workspace or a path
    fc_list = arcpy.ListFeatureClasses()
    # Print the list
    print(fc_list)
    
    # use a loop to set the variable fc to be
    # equal to each feature class name in the list
    for fc in fc_list:
        # Run Get Count on the current fc from the list
        # Set the variable count equal to the result of Get Count
        count = arcpy.GetCount_management(fc)
        # Print out the name of the feature class and the count
        print(fc, count)

    Code to list feature classes in workspace and count their features

    This code includes comments to explain what each step does. Comments are text that appears after the # signs. The lines that are comments don't run; they are there to help you understand what the code does.

    You used a list and list indexing earlier. This code makes a new variable named fc_list and sets it equal to the result of running arcpy.ListFeatureClasses on the current workspace, Toronto.gdb. The earlier list contained integers. This time the list contains strings with the names of the feature classes.

    Next, the code prints that list. Printing the list isn't necessary, but it is a good way to ensure that you have the results that you expect in the variable. If something goes wrong, it is helpful to know what is in that variable.

    The next part of the code uses a for loop. For loops take a set of inputs and a block of indented code and run the code block on each of the inputs.

    In this case, the inputs are the names in the list, and the variable fc is set to each one of them, in turn, and the code in the indented block runs on it.

    The code block creates a variable named count and sets it equal to the result of calling GetCount on the current item from the list.

    The code block then prints the feature class name (the contents of the fc variable) and the count (the contents of the count variable).

    The code, without comments, is only a few lines.

    
    fc_list = arcpy.ListFeatureClasses()
    print(fc_list)
    for fc in fc_list:
        count = arcpy.GetCount_management(fc)
        print(fc, count)

  25. Run the cell.

    The results show the list of feature classes in the geodatabase and their feature counts.

    Results of running the code

Run an analysis using a notebook

Next, you will do some GIS analysis work using the notebook. Suppose you are interested in finding out what areas within the Etobicoke administrative district are farthest from fire stations. You can use geoprocessing tools in a notebook to identify these areas.

  1. Add a new cell below the current one.
  2. Place the cursor inside the cell and start typing the following:

    arcpy.

  3. Press the Tab key.

    Press the Tab key to see options.

    A list of all of the available ArcPy options appears.

    You can scroll down and click an item to select it from this list, or you can continue typing.

  4. Type an uppercase B.

    The list is filtered to B options.

  5. Click Buffer_Analysis.

    The cell now says arcpy.Buffer_analysis.

    This process of beginning to type some code and then pressing the Tab key to see and choose from matching options is called tab completion, and it can help you find and more quickly access the commands you need.

  6. With your cursor still at the end of the line of code, press Shift+Tab.

    Buffer analysis signature

    A window with the syntax hints for the Buffer_analysis tool appears. You can click the arrow button to expand it to read the whole topic.

  7. Click the close button to close the Signature window.
  8. Type an open parenthesis.

    Buffer with parentheses.

    A close parenthesis is also added, and the cursor is placed between them. This is where you can add parameters for the Buffer_analysis tool.

  9. Type a quotation mark.

    Buffer with quotation marks.

    A second quotation mark is added. Python needs strings to be enclosed in quotation marks, so it adds a matching quotation mark, with the cursor between them.

    The three parameters that the Buffer_analysis tool requires are the input feature class, the output feature class, and the buffer distance. There are other optional parameters, but these are the only ones that are required.

    You'll buffer the fire_stations feature class, name the output feature class fire_buffer, and make the tool buffer the fire stations by a distance of 1000 meters.

  10. Complete the line of code as follows:
    arcpy.Buffer_analysis("fire_stations", "fire_buffer", "1000 METERS")

    Buffer by 1000 meters.

    The three parameters of the tool are strings. The tool is able to locate the fire_stations feature class using only its name because it is a layer on the map, and because you already set the workspace to Toronto.gdb. The tool is able to write an output feature class using only the name "fire_buffer" because the workspace is set. The tool has logic built in to detect the buffer distance value and the units of measure in the string "1000 METERS".

  11. Run the tool.

    Buffers by 1000 meters on the map

    The output features are added to the map.

    The results show which areas fall within 1000 meters, or 1 kilometer, of a fire station and which areas do not. Having both the notebook and the map open at the same time makes it easy to see the results of different choices.

  12. Change the buffer distance to 1750 meters and run the cell again.
    arcpy.Buffer_analysis("fire_stations", "fire_buffer", "1750 METERS")

    Buffers by 1750 meters on the map

    Note:

    If you get an error message, "ExecuteError: Failed to execute. Parameters are not valid", that mentions that fire_buffer already exists, then your ArcGIS Pro environment settings, geoprocessing options are not set to allow existing feature classes to be overwritten. To fix this issue, insert a new line in the cell before the arcpy.Buffer_analysis line. On the new line, add the following code:

    arcpy.env.overwriteOutput = True

    This will allow the Buffer tool to overwrite the previous output. The cell should now contain:

    arcpy.env.overwriteOutput = True
    arcpy.Buffer_analysis("fire_stations", "fire_buffer", "1750 METERS")

    Run the cell.

    The areas outside of these buffers are farther away from the fire stations, which may increase the time it takes for a fire engine to respond to a call. To find the areas that are affected, rather than the parts that are not, you will use the erase tool to remove the areas within the buffers from the Etobicoke administrative district.

  13. Add another cell and enter the following code:
    arcpy.PairwiseErase_analysis("etobicoke", "fire_buffer", "no_service")

    This calls the PairwiseErase_analysis tool on the etobicoke feature class, erasing the areas within the fire_buffer feature class from it, and writing the results to a new feature class named "no_service".

    Pairwise Erase is an alternate version of the Erase tool that uses a different set of libraries and handles the topological relationships of vertices differently than Erase. If there is any question as to the quality of your input datasets, before running Pairwise Erase you should run Pairwise Integrate on your data first. In this simple situation it is not necessary.

  14. Run the cell.
  15. In the Contents pane, uncheck the fire_buffer, greenspace, etobicoke, and boundary layers.
  16. Right-click the no_service layer and click Zoom To Layer.

    The no_service layer shows places that are farther from fire stations.

    The no_service layer shows places that are farther from fire stations.

  17. Click the arcpy.PairwiseErase_analysis cell, and in the Notebook pane, click the Edit menu and click Merge Cell Above.

    Merge current cell with the cell above it.

    The cells are merged.

    Cells are merged.

    A benefit of Notebooks, and Python code in general, is that you can quickly run a sequence of tools. In this case, the sequence only consists of two tools, but it illustrates the concept.

  18. Change the distance value from 1750 to 2500 and run the cell.

    Change distance and run again.

  19. Turn off the new fire_buffer layer to see the new no_service layer.

    You may need to zoom to the layer to see the remaining smaller areas.

    The resulting areas are potentially the most compromised in terms of fire service. You were able to obtain this updated result by running the multiple lines of code as a single cell in the notebook. If you had used the tools from their graphical user interfaces, you would have needed to run both the Buffer tool and the Pairwise Erase tool again to obtain the updated result.

    The time savings with only two tools is minor, but many workflow's consist of longer sequences of tools. In addition, the ability to run a tool, or multiple tools, within a loop makes Python useful when you need to run the same process on multiple inputs.

  20. Save your project.

Python code run in and outside of Notebooks

You have learned how to create a notebook and how to write and run Python code in a notebook. There are several benefits to using Notebooks, not all of which were demonstrated in this tutorial:

  • Notebooks are a fast way to get started writing Python code and do not require the installation or configuration of a Python editor.
  • You can interact with data and maps directly in ArcGIS Pro. This allows you to quickly see the results of running your code.
  • Notebooks are integrated into the geoprocessing framework of ArcGIS Pro. This makes the experience of running tools similar to running tools using their dialog box. Results are added the active map, and tools that have run appear in the geoprocessing history.
  • Notebooks in ArcGIS Pro include several code completion features to facilitate writing correct code.
  • Notebooks can also be run outside of ArcGIS Pro, including certain Python editors and Jupyter Notebook.
  • Notebooks include built-in visualization tools (for example, map widgets and others).
  • All contents are saved as an .ipynb file, which can be shared with others.
  • Notebooks can be hosted in ArcGIS Enterprise or ArcGIS Online. This makes it possible to make an .ipynb file available to others in your organization.

There are also some potential downside of running Python code in a Notebooks:

  • Notebooks lack some features found in other Python IDE's, such as syntax checking, debugging, and others.
  • Code completion assistance requires keyboard shortcuts and is not as intuitive or complete as in other Python editors.
  • Not all Jupyter Notebook functionality works in ArcGIS Pro (but continues to improve).

There are several other ways to run Python code, including running code in a Notebooks, running a script using a command line, or scheduling to run a script from the operating system. These are covered in other tutorials.

You have learned how to write and run Python code in a notebook in ArcGIS Pro. You learned about variables and loops, two important concepts in Python. You also learned how to set the environment in Python, and how to use standard ArcGIS geoprocessing tools in Python. This is the second in a series of tutorials on how to use Python in ArcGIS Pro, with more to come.

You may also be interested in Python Scripting for ArcGIS Pro and Advanced Python Scripting for ArcGIS Pro by Dr. Paul A. Zandbergen, published by Esri Press.