Creating a progress bar/progress dialog for a python addin tool

6366
8
Jump to solution
11-17-2015 05:17 AM
BenjaminSimpson
New Contributor III

Hi,

I have created a python addin toolbar which contains several buttons which run data management scripts in ArcMap. These scripts primarily append data from one or more personal geodatabases to an Oracle database. These scripts can take anywhere from a few seconds to, well longer, depending on the number of geodatabases selected and their size. I currently have several messages displayed using pythonaddins.MessageBox() at the beggining and the end of my scripts. What I am looking to include now is some sort of progres bar or progress dialog box which shows the user how the script is progressing. As it currently standing when the script is run, apart from the current message boxes it is not clear how far along the process the script has got. I would ideally like a progress dialog box which has a progress bar with the ability to print messages below.

The question I have is how do I go about this? I have seen mentions as to pythonaddins.ProgressDialog (The pythonaddins module—Help | ArcGIS for Desktop) but when I try and run the example code in the python window i get an error stating that the 'module' object has no attribute 'ProgressDialog'. I am using ArcGIS 10.2 so not sure if this ProgressDialog function is supported esspecially seeing as the 10.2 help doesnt not mention it.

I would be very greatful if someone could point me in the direction of some useful help documents or tutorials which will show me how to create some sort of progress dialog.

Thanks in advance for any help,

Ben.

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

The pythonaddins.ProgressDialog is not available in 10.2.

What you could do is use pythonaddins.GPToolDialog (note: gotchas if using a python pyt toolbox instead of a tbx and script tool) to kick off a script tool that does the actual long running operations and uses the arcpy.SetProgressor object to advance the result window progress bar.

import arcpy, time

arcpy.SetProgressor('step','Some description', 0, 100, 1)

for i in range(100):

    time.sleep(0.25) #Fake doing some stuff

    arcpy.SetProgressorPosition()

    arcpy.SetProgressorLabel("Step %s"%(i+1))

progressor.png

View solution in original post

8 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

I have not worked with this option yet (but plan to check it out), but my guess as why it didn't work in the python window is mentioned in the help link you provided....

note: The pythonaddins module can only be used within a Python add-in. It cannot be used in stand-alone scripts or geoprocessing script tools.

that may explain the error you received.

0 Kudos
BenjaminSimpson
New Contributor III

ahh yes, that may answer the error in the python window. The help page seems abit misleading as it suggests running the sample code through the python window which is contradicting that note at the top of the page.

I have tried including that sample code (below) in my python addin but unfortunately I got the same error "'module' object has no attribute 'ProgressDialog'" when I ran my tool in ArcMap. Now unless I am getting confused by what exactly a python add-in is. I am under the assumption that my custom toolbar with several buttons running python scripts is a python add-in. I have a feeling that this ProgressDialog function within pythonaddins is not supported.

import pythonaddins

with pythonaddins.ProgressDialog as dialog:

    dialog.title = "Progress Dialog"

    dialog.description = "Copying a large feature class."

    dialog.animation = "File"

    for i in xrange(100):

        dialog.progress = i

        time.sleep(0.125)

        if dialog.cancelled:

            raise Exception("Ooops")

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Benjamin Simpson wrote:

.... Now unless I am getting confused by what exactly a python add-in is. I am under the assumption that my custom toolbar with several buttons running python scripts is a python add-in. I have a feeling that this ProgressDialog function within pythonaddins is not supported.

.....

You may want to check out

What is a Python add-in?—Help | ArcGIS for Desktop

Python addins are actually a collection of files that are (semi) compiled into a file with .addin extension (if you change the .addin to .zip, you can view all the files).  Once you figure out how to set them up, they are actually really slick and can be installed easily on other machines (but not available in Pro).

there is a wizard to help set it all up, and I'll track it down and post.

RebeccaStrauch__GISP
MVP Esteemed Contributor

(having issues editing my posts this am...so had to reply to my previous post)

here is is the link to the addin wizard link

http://www.arcgis.com/home/item.html?id=5f3aefe77f6b4f61ad3e4c62f30bff3b

It's a bit hard to find with a search, but I did just noticed there is still a link in the help above.

i find it is easiest to get most of my scripts and toolbox setup first, then run the wizard.  I have a structure that I use for my wizards that seems to work. If interested I can share...or maybe find where I may have already posted it.

Luke_Pinner
MVP Regular Contributor

The pythonaddins.ProgressDialog is not available in 10.2.

What you could do is use pythonaddins.GPToolDialog (note: gotchas if using a python pyt toolbox instead of a tbx and script tool) to kick off a script tool that does the actual long running operations and uses the arcpy.SetProgressor object to advance the result window progress bar.

import arcpy, time

arcpy.SetProgressor('step','Some description', 0, 100, 1)

for i in range(100):

    time.sleep(0.25) #Fake doing some stuff

    arcpy.SetProgressorPosition()

    arcpy.SetProgressorLabel("Step %s"%(i+1))

progressor.png

View solution in original post

BenjaminSimpson
New Contributor III

Thanks for that answer.

Am I understanding it correctly that the arcpy.SetProgress function has to be run through a geoprocessing toolbox? I have tried including it in my python script, the script still runs fine but the progress dialog does not appear. The same happens when I run the progress dialog code through the python window. Would this mean that the only way for me to get the arcpy.SetProgress function to work with my script is to transfer my script into a toolbox and then call the toolbox using the arcpy.GPToolDialog function in my python addin?

If this is the case is it possible to store this toolbox within the Install folder of the python addin? I am thinking purely about ease of sharing this python addin.

Thanks again for your help, this arcpy.SetProgress function seems like the best chance I have of creating such a progress dialog.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Benjamin, I don't have anything to add right now, since I haven't used it, but just wanted to point out that it is Arcpy.SetProgressor       (You missed the "or" on the end in you post.  Just so others reading this thread have the correction)

Benjamin Simpson wrote:

...

If this is the case is it possible to store this toolbox within the Install folder of the python addin? I am thinking purely about ease of sharing this python addin...

Re this statement...what toolbox are you referring to in this case?  You can include any tools (including custom scripts) in a custom toolbar, and then include the toolbar/tools with in the python addin.   Here is a post I wrote a while back so I could find it again.... Tip: Python Addin - getting custom tools/toolbox to work - GPToolDialog

0 Kudos
Luke_Pinner
MVP Regular Contributor

Benjamin Simpson wrote:

Would this mean that the only way for me to get the arcpy.SetProgress function to work with my script is to transfer my script into a toolbox and then call the toolbox using the arcpy.GPToolDialog function in my python addin?

Yes, that's what I said.

Benjamin Simpson wrote:

If this is the case is it possible to store this toolbox within the Install folder of the python addin? I am thinking purely about ease of sharing this python addin.

Yes. Then in your add-in use something like:

arcpy.GPToolDialog(os.path.join(os.path.dirname(__file__), 'mytoolbox.tbx'), 'MyTool')