Launch python toolbox from toolbar: can it be done?

2399
4
Jump to solution
10-17-2012 07:18 AM
deleted-user-zpcJw1u0IXiO
Occasional Contributor
I need to put a button on a toolbar to launch my new python toolbox tool that maps condos. I have found this to be impossible, and am hoping for help...

I've tried building an add-in to launch the pyt, but in testing it I get a pop-up error "Failed to open toolbox Condos.pyt". Perhaps this means add-ins simply do not support python toolboxes, or maybe I've incorrectly specified the path in the add-in's onClick def:

toolboxPath = os.path.join(os.path.dirname(__file__), toolboxName + ".pyt")


The reason I'm uncertain about the path is that the pyt is in "My Toolboxes", and I don't know whether this code actually points there (I'm very new to python). Or maybe a pyt is merely incompatible here, because the example code has ".tbx" in the above line. Any ideas?

To re-write my pyt as a regular python script in a tbx and set up params that way has certainly occurred to me and would probably work, but would be a huge disappointment given the time invested in the pyt that works so nicely. How can I launch from a toolbar button the CondoBuilderTool of Condos.pyt? Is that even possible? Please help me with some suggestions here. How can I resolve this?

Thanks in advance,
Justin
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AndrewChapkowski
Esri Regular Contributor
The .pyt files need to be placed in the install folder of the python add-in, and the makeaddin.py file needs to be re-run.

After that, you can then re-install the add-in by double clicking on the file.

Also, ensure that you close and restart arcmap or catalog.

Andrew

View solution in original post

0 Kudos
4 Replies
AndrewChapkowski
Esri Regular Contributor
You need to call the class name for a .pyt.  So I have a python tool called 'Tool', so I pass in Tool to the name parameter.
        relPath = os.path.dirname(__file__)
        pyt = relPath + os.sep + "customToolbox.pyt"
        pythonaddins.GPToolDialog(pyt, "Tool") # Tool is the class name 


Also, ensure that the .pyt is in the add-in file.  You can check that by renaming the extension .zip and examining the contents.

See http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000021000000 for more information.
0 Kudos
deleted-user-zpcJw1u0IXiO
Occasional Contributor
Also, ensure that the .pyt is in the add-in file.  You can check that by renaming the extension .zip and examining the contents.


Thank you Andrew. I have updated Tools_addin.py as you instructed, as follows:
    def onClick(self):

        # name of toolbox
        toolboxName = "Condos"

        # name of tool to be executed (the class in Condos.pyt) 
        toolName = "CondoTool"

        # create string with path to toolbox
        relPath = os.path.dirname(__file__)
        pyt = relPath + os.sep + toolboxName + ".pyt"

        # call geoprocessing tool
        pythonaddins.GPToolDialog(pyt, toolName)

        pass


Yes, I did check the add-in file and did NOT find the .pyt anywhere inside. The file contains two subfolders, so can I just insert the .pyt to the file as I would into a zip? If no, please explain the proper method. If yes, should the Install subfolder be the destination? Further, what more should I do to ensure the .pyt is included when I share the add-in (I feel I'm updating here the installed add-in, not the actual installer--please correct me if I'm wrong).
0 Kudos
AndrewChapkowski
Esri Regular Contributor
The .pyt files need to be placed in the install folder of the python add-in, and the makeaddin.py file needs to be re-run.

After that, you can then re-install the add-in by double clicking on the file.

Also, ensure that you close and restart arcmap or catalog.

Andrew
0 Kudos
deleted-user-zpcJw1u0IXiO
Occasional Contributor
Works like a charm now. I've learned a lot today :D. Thanks so much, Andrew!
0 Kudos