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?
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.
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.
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.
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.
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:
- Get started with notebooks in ArcGIS Pro tutorial
- Notebooks in ArcGIS Pro documentation
- Jupyter Notebooks in ArcGIS Pro video
Happy coding!