Select to view content in your preferred language

Run Model exported to Python from WebScript

4249
5
10-03-2014 11:44 AM
BrianO_keefe
Honored Contributor

I have a Model that I run once a month, per a request from our Police department, that queries and dumps a shapefile to a specific folder.

 

Currently, the officer calls in a ServiceTicket, tells me what month, and I run the model and close the ticket.

 

What I would like to do is start to Automate this process.

 

I have the Python script (exported from the Model) in a web folder. I have converted the folder to an Application (with an Application pool).

I have an HTML form I threw together...

 

<!DOCTYPE html> <html>   <head>   <meta charset="UTF-8">   <title>Crime Export Form</title>   </head>     <body>   <form action="CrimeExports.py" method="POST">   <input type="text" name="Start_Month" value="January"/>   <input type="text" name="Start_Year" value="2014" />   <input type="text" name="End_Month" value="February" />   <input type="text" name="End_Year" value="2014" />   <input type="text" name="Folder_to_store_export" value="\\path\to\where\I\want\to\save" />   <input type="submit" />   </form>   </body>   </html> 

 

It does absolutely nothing.

0 Kudos
5 Replies
BrianO_keefe
Honored Contributor

I should add I have never run a Python script on a web-server. I have a PHP background, lots of Javascript, and HTML (of course). So I'm clueless on this end for Python-ating...

0 Kudos
curtvprice
MVP Esteemed Contributor

This approach will not work, unfortunately.

A key thing to know about running things from an HTML form is that (for obvious reasons) web browsers can't normally access your file system. If they could, it would be very easy for people to inject malicious code into your http server to run on your machine and do all kinds of things you do not want.

The best way to make a model run on a server is to share as a geoprocessing service from ArcGIS Desktop, using ArcGIS Server or ArcGIS Online.

But if you want to automate this at all, you can write a little Python script to run a model without conversion. This way your model can have things like iterators that do not export...

import arcpy

arcpy.ImportToolbox("path to model", "tb")

arcpy.tb.MyModelTool(arg1, arg2, arg3)

At any rate, I'd test from a command line first so at least you know that is working.

0 Kudos
BrianO_keefe
Honored Contributor

Thanks for the input Curtis!

The problem is my model has a set of parameters.

I have an Internal and an External ArcGIS Web Server, using a Web Adaptor for External maps. Surely there is someway to get this Python script running on the Internal server?

0 Kudos
curtvprice
MVP Esteemed Contributor

> The problem is my model has a set of parameters.

If your model has parameters, you can pass them through python as follows

import arcpy

arg1 = arcpy.GetParameterAsText(0)

arg2 = arcpy.GetParameterAsText(0)

arg3 = arcpy.GetParameterAsText(0)

arcpy.ImportToolbox("path to model", "tb")

arcpy.tb.MyModelTool(arg1, arg2, arg3)

From the OS you'd run it like this:

C:\Python27\ArcGIS10.2\python.exe arg1 arg2 arg3

0 Kudos
PaulCrickard1
Deactivated User

not sure how safe it is, I am protected internally so here is my php and Python examples from my blog. One uses exec() and the other is a python server that does the work.

If you can reach the model from your server, you can leave it as a toolbox and use a python script to call the tool (sometimes the model doesnt run properly when exported. I put the code from ESRI on the bottom of the post.

Calling Python From PHP: A GIS Example | Architecture and Planning

Shapefile Creator Website Using Cherrypy | Architecture and Planning 

import arcpy

# Import custom toolbox
arcpy.ImportToolbox("c:/tools/My_Analysis_Tools.tbx")

try:
   # Run tool in the custom toolbox. The tool is identified by
   # the tool name and the toolbox alias.
   arcpy.GetPoints_myanalysis("c:/data/forest.shp")
except arcpy.ExecuteError:
   print(arcpy.GetMessages(2))