Python Addin - Best Practices

511
0
07-17-2017 09:59 AM
NobbirAhmed
Esri Regular Contributor
2 0 511

A Python addin enables you to create custom button, tool, combobox on your custom toolbar and create your own extension in Python language. Use Esri's desktop and online documentation - it has all the information to get started, create, debug and maintain Python addins.

However, I have seen users make mistakes that are hard to find or debug. So, I'm listing few guidelines which may save you time and in many cases get you out of trouble.

Project Settings

There is no restrictions on setting Project properties while creating an addin, however, having a good name for the project and an identifiable image as icon will help.

Python Addin Wizard - project settings.

Setting Toolbar properties

Do not use any space in setting toolbar ID.

Setting toolbar properties

Setting Extension properties

Same rule applies for naming the ID variable. However, I suggest naming the class such that, when coding the script-behind, you know that it is a class - also, class name cannot contain space. Additionally, initially select only those methods that you'd need in your extension. 

Setting extension properties

Setting Button, Tool, ComboBox properties

 

ID (Variable Name) is important - portion of the ID right to dot is the actual ID. This is very important as you use this ID to communicate with other controls (or extension) of the same add-in. The wizard gives generic button1, button2 names but when you write code for the controls you can be confused which does what - it will be easier if the ID itself contains clue.

Setting properties for Button, Tool and ComboBox

Using Global variables for communication between or among controls

It is not well-advised to use global variables in Python - same advise applies to writing code for Python addins. Instead, you can use an Extension class with your own methods to facilitate communication. Or, you can have your own class to do the same.

Please feel free to comment if you have suggestions for improvements of these suggestions.

About the Author
Product Engineer, Geoprocessing and Analysis Team, Esri, Redlands.
Labels