Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I created an ArcMap Python add-in using ArcGIS 10.1 to open a webpage after the map is clicked. It opens the page but then ArcGIS crashes.

import arcpy
import pythonaddins
import webbrowser
class ToolClass2(object):
    """Implementation for TEST_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor=3
    def onMouseDownMap(self, x, y, button, shift):

    mxd=arcpy.mapping.MapDocument("current")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    pt=arcpy.PointGeometry(arcpy.Point(x,y))
    pythonaddins.MessageBox("Long" + " " + str(x) + '\n'+ "Lat"+ " " + str(y), 'Coordinates', 0)
    path = 'http://pol.pictometry.com/en-us/php/default.php?lat=' +str(x) +'&lon=' + str(y)+'&v=p&o=n&type=or&level=n'
    webbrowser.open(path)
    pass
share|improve this question
1  
Your indentation looks astray (mxd at same level as def) - perhaps fix it in Question or code, whichever applies. When you say Arc crashes do you mean it bails with a Serious Application Error or something else? –  PolyGeo Feb 1 '13 at 8:04
    
The above threaded method works fine for me, but I have to wait some 20 seconds before the browser actually opens which to most users is a crash! Is this speed correct? –  user14853 Feb 4 '13 at 17:39
    
@TonyCollins, The browser opens within a few seconds on the computers I have tried. I am not sure what is going on in your situation. How long does it take to normally open a web browser? –  blah238 Feb 4 '13 at 17:51
add comment

3 Answers

@BMac's code also crashes my ArcMap 10.1 SP1 installation.

This code works properly however:

import arcpy
import pythonaddins
import webbrowser
from threading import Thread

def OpenBrowserURL():
    url = 'http://www.google.com'
    webbrowser.open(url,new=2)

class OpenWebBrowserButtonClass(object):
    """Implementation for WebBrowserAddIn.openWebBrowserButton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        t = Thread(target=OpenBrowserURL)
        t.start()
        t.join()

I call the same function from a new thread. Python add-in classes probably run on the UI thread by default and maybe there is some race condition or other issue causing ArcMap to crash, but if called from another thread it works.

Update: I just saw an ArcGIS Team Python blog post about this:

os.startfile and webbrowser.open are two very useful functions in the Python library. However, due to some conflicts in the way the Windows libraries expect to be called, they can fail or crash when called within ArcGIS for Desktop in an add-in script or geoprocessing script tool (see the Remarks section on this MSDN reference page).

import functools
import os
import threading
import webbrowser

# A decorator that will run its wrapped function in a new thread
def run_in_other_thread(function):
    # functool.wraps will copy over the docstring and some other metadata
    # from the original function
    @functools.wraps(function)
    def fn_(*args, **kwargs):
        thread = threading.Thread(target=function, args=args, kwargs=kwargs)
        thread.start()
        thread.join()
    return fn_

# Our new wrapped versions of os.startfile and webbrowser.open
startfile = run_in_other_thread(os.startfile)
openbrowser = run_in_other_thread(webbrowser.open)

The local functions startfile and openbrowser will be made available, which have the same parameters as the versions in the standard library but will run in another thread and therefore work as expected.

Somewhat related: Crashing ArcGIS 10.1 Add-ins Using Multiprocessing

share|improve this answer
1  
Hopefully the MSDN reference gives clarity. Not a race condition, but a COM environment issue in that thread. ArcGIS uses one COM threading model, the Windows Explorer component that gets called to open URLs/files uses another. –  Jason Scheirer Oct 28 '13 at 22:49
add comment

I opened the ArcMap python window and was able to run the following code successfully:

import webbrowser
url = 'http://www.google.com'
webbrowser.open(url,new=2)

However, I end up with the same issue when running as a python add-in. Closes/crashes ArcMap and opens the browser. Could be a bug in the add-in module or we need to rename our button to "Open browser and crash ArcMap". Sounds like you might need to call ESRI support to report a possible bug.

Sample Python add-in that crashes ArcMap:

import arcpy
import pythonaddins
import webbrowser

def OpenBrowserURL():
    url = 'http://www.google.com'
    webbrowser.open(url,new=2)

class OpenMetadata(object):
    """Implementation for TestAddins_addin.button1 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        OpenBrowserURL()
share|improve this answer
    
Odd thing...occured –  Kyle Feb 2 '13 at 1:46
add comment

I just ran across this issue. I wasn't able to stabilize the situation with blah238's remedy, but I did seem to find success with using Python's subprocess module. Here's my code:

#--THIS IS THE CALLING SCRIPT--

class OpenURL(object):
    """Implementation for OpenURL_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        url = "http://mywebsite.com"
        import subprocess

        #Set up the subprocess components
        python_path = r"C:\Python27\ArcGIS10.1\python.exe"
        python_script = r"C:\ArcGIS_AddIns\PythonAddIns\Tools\GIS_Locate\Install\OpenWebBrowser.py"

        #Now call the script and pass in the necessary variables
        subprocess.call([python_path, python_script, url]) 

#--THIS IS THE MODULE (OpenWebBrowser.py) THAT THE SUBPROCESS CALLS--
import sys
import webbrowser

url = sys.argv[1]
webbrowser.open(url,new=2)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.