[GIS] How to run a python script on the server, returning the results to the client

arcgis-javascript-apiarcpypython

Here's the end result I'm trying for:

  • the user clicks on a feature on an ArcGIS Server JS map
  • a Python script is triggered on the server
  • the Python script uses an attribute from the feature (eg its OID) to perform some calculations (using arcpy modules)
  • the results are returned to the infoWindow as HTML, for display

I guess I'd need to be able to trigger a Python script (containing arcpy commands) on the server, passing parameters between the client and server.

Assuming this is possible, what is the best approach:

  • via the ArcGIS Server geoprocessing framework
  • directly via an AJAX call to the Python script (and is that even possible)?
  • something else?

Best Answer

This is the precise use case for a geoprocessing service. Write your script and add it to a geoprocessing toolbox on the server as a script tool.

You can test it on the server while developing the script by hard coding the parameters. Once you're comfortable with your code, replace those hard coded parameters with arcpy.GetParameterAsText(), add to a geoprocessing toolbox as a script tool and publish to ArcGIS Server as either a synchronous (Execute Task) or asynchronous (Submit Job) service.

You can then use the geoprocessor (https://developers.arcgis.com/javascript/jshelp/intro_gp_overview.html) in the Javascript api to send a request to the service as well as handle the result.