Skip to main content

Tips and Tricks for Using Python Notebooks in ArcGIS Pro

Learn more about the benefits of using Python notebooks in ArcGIS Pro to document your geospatial workflows.

I am a huge believer in doing all my spatial analysis in Python and ever since ArcGIS Notebooks were integrated into ArcGIS Pro starting at version 2.5, I have been keeping track of my ArcGIS Pro workflows in Python notebooks. There are many benefits to using a code notebook but a major one is the ability to keep track of your workflow in an easily interpretable format. With notebooks, it is easy to understand and repeat your workflow anytime in the future, even after you have forgotten why you did what you did! 

Have you ever gone through your project history just to see something that looks like this? 

Screen capture GIF of the ArcGIS Pro History pane with a long list of previously run geoprocessing tools.

The geoprocessing History pane is super useful, but it can get messy quick!

Sometimes I come back to my project just a day later and have no idea why I did what I did, let alone a week or more!

Once in a blue moon, I run a tool or set of tools in a workflow once and it works but more often I have to apply small tweaks and end up running the tools twice… three times… twenty times… before I get the result I am looking for. This is no different if I am writing the steps in code BUT it means that I have a written record of what worked that I can further annotate, which makes the world of difference when I come back to a project after a break. This is even better in a code notebook where you can provide lengthy text and context to your work, all in one document.

Whether you're new to using Python in ArcGIS Pro or are looking to add to your knowledge, here are some tips and tricks for getting the most out of Python notebooks in ArcGIS Pro. 

1. Markdown Cells

The primary benefit of notebooks as opposed to other IDEs are different cell types for code and markdown. Code cells work like normal bits of Python code and still allow for comments, but you can also include Markdown type cells which are non-code cells designed for adding context to your code documents. The markdown language (check out the Markdown Guide) is used for formatting rich text, so you can easily add formatted text, pictures, links and more to your document. Markdown cells can appear before, in between and after code cells to add information and context to your code.

GIF of ArcGIS Pro with a code cell in an ArcGIS Notebook being converted to a Markdown cell and then run, converting it to formatted text with headers, lists and images.

Markdown cells allow you to add context to your code with formatted text.

2. Drag-and-Drop Tools

Did you know you can drag and drop geoprocessing tools directly into code cells in ArcGIS Pro? No need to search online for the python code for each geoprocessing tool, you can drag and drop tools directly from the toolbox into your notebook. Even better? You can also drag and drop tools from your geoprocessing history into notebooks and it keeps all of the parameters the tool was run with! This is a fantastic way to use notebooks to keep a record of your process, even if you initially ran the tool in the geoprocessing pane.

GIF of a geoprocessing tool being dragged from the Geoprocessing pane in ArcGIS Pro into a code cell of an ArcGIS Pro notebook and then a geoprocessing tool history item being dragged from the History pane in ArcGIS Pro into the code cell of an ArcGIS Pro notebook and converted into its Python equivalent with all parameters.

You can drag-and-drop tools from the Geoprocessing pane and the History pane.

3. Export as HTML

A huge benefit of Notebooks is that you can share code along with markdown text in one single document. But what if the person you want to share it with doesn't have Pro or doesn't know how to open a Notebook .ipynb extension file? Luckily, you can export your notebooks as HTML files to be easily shared with anyone! The exported html files can't be run as code, but they include all the code and markdown cells, including results and can be opened in a browser. Simply go to the Notebook tab of the ribbon > Export > Export as HTML and ta-da, you have a document that can be shared with anyone.

Screen capture of the ArcGIS Pro top ribbon on the Notebook tab with the Export dropdown menu open and a yellow arrow pointing to the Export as HTML option

ArcGIS Notebooks can be exported as HTML files to share with anyone.

4. Interrupt Kernel

Have you ever clicked run on your python code only to then realize there is a mistake? Sometimes you'll get an error right away but sometimes your code will run for hours and monopolize your instance of ArcGIS Pro before the dreaded error message appears. Luckily, there is an escape hatch in Notebooks: the Interrupt Kernel button. This handy little button sends a signal to the kernel to stop the execution of the cell and save you from having to wait for your errant operation to time out or fail. 
 
Within ArcGIS Pro, Interrupt Kernel is a button in the Notebook ribbon at the top of the page. If you are running a Jupyter Notebook outside of Pro, you can interrupt the kernel using the stop button (black square) or the Interrupt command from the Kernel drop down menu. 

Screen capture of an ArcGIS Notebook open in ArcGIS Pro and a yellow arrow pointing to the Interrupt Kernel button on the Notebook ribbon at the top of the program

The Interrupt Kernel button stops the execution of the actively running and queued code cells.

For more information about using Notebooks in ArcGIS Pro, check out these resources:

Happy coding!