how can i call one script tool as many types as the input given by the user

597
11
03-13-2018 10:24 AM
sobhansai
New Contributor II

I created two script files one script file will take user input as a number and i have another script which has to be run those many times the number given by user.Please help me with this?I have tried to call that scrip by using subprocess.Popen method but i am not getting any output.It is getting terminated.

0 Kudos
11 Replies
DanPatterson_Retired
MVP Esteemed Contributor

The scripts would help to narrow down issues.

Why the need to run that way, when you can simply run a function (imported from another script/module) any number of times you want.

sobhansai
New Contributor II

Can u please elloborate or provide any example.Actually these scripts are the toolbox scripts for the creation of tools in arcmap

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I am using PRO as the demo, but the principle is the same... in pictoral form

The tool in action... allow the user to input many names into the toolbos

increment a counter, calling a function that is stored in the 'helper' script.  It worked

What you have to do.....

Assign a script to the tool

Set up the parameters... we have a single parameter that allows for multiple names to be input

Now everything... scripts and toolbox are stored in the same folder

Here is the code

Notice in line 16, I import a function from dummy_helper and use it to print some names with a counter

# -*- coding: UTF-8 -*-
"""
:Script:   dummy_main.py
:Author:   Dan.Patterson@carleton.ca
:Modified: 2018-xx-xx
:Purpose:  tools for working with numpy arrays
:Useage:
:
:References:
:
:---------------------------------------------------------------------:
"""

# ---- imports, formats, constants ----
import sys
import arcpy
from dummy_helper import dumb_demo

def tweet(msg):
    """Print a message for both arcpy and python.
    : msg - a text message
    """

    m = "\n{}\n".format(msg)
    arcpy.AddMessage(m)
    print(m)

if len(sys.argv) == 1:
    testing = True
    names = ['Hello There', 'How are you', 'Goodbye']
else:
    names = sys.argv[1].split(";")
    if names is None:
        tweet("no names")
cnt = 0
for i in names:
    cnt += 1
    tweet(dumb_demo(cnt, i))


# ----------------------------------------------------------------------
# __main__ .... code section
if __name__ == "__main__":
    """Optionally...
    : - print the script source name.
    : - run the _demo
    """

#    print("Script... {}".format(script))

Here is the helper script.... now!!! I tend to use a helper script that contains functions that I use all the time, so you don't have to replicate them in every toolbox that you use.

# -*- coding: UTF-8 -*-
"""
:Script:   dummy_helper.py
:Author:   Dan.Patterson@carleton.ca
:Modified: 2018-xx-xx
:Purpose:  tools for working with numpy arrays
:Useage:
:
:References:
:
:---------------------------------------------------------------------:
"""

# ---- imports, formats, constants ----
import sys

def dumb_demo(cnt, name):
    """
    : docs
    """

    return "({}) {}".format(cnt, name)


# ----------------------------------------------------------------------
# __main__ .... code section
if __name__ == "__main__":
    """Optionally...
    : - print the script source name.
    : - run the _demo
    """

#    print("Script... {}".format(script))

Hope you get the drift.

sobhansai
New Contributor II

Thank you so much for your help sir.I have a Graphical User Interface in the second script also.Here in your script it is executing a function,but i also need to a call the GUI in the second Script.Please help us.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Code needed... and if you aren't using a GUI that plays nice with arcmap or pro, then you will have some problems.

I would advise against it and either use the standard toolbox approach or use python toolboxes.

the subprocess module would be an option in pro, but I don't know what you would gain.

Perhaps a full description of your desired workflow with the functional code would help

sobhansai
New Contributor II

Actually we have designed the user Interface using script in arcmap toolbox(.tbx).So I am sending the screen shots and the code which has to be called many times.

INo of parameters needed and these many times the second UI need to be called Second UI which has to be run as many times the user needed

Input 1:It will take the number of parameters needed and this will be the count that the second image need to be called.

Input 2:It has to be executed, as per the input given in first UI.

    Code for Second UI(ie second image)

# Import arcpy module
import arcpy

# Local variables:
filename = arcpy.GetParameterAsText(0)
Field = arcpy.GetParameterAsText(1)
Select = arcpy.GetParameterAsText(2)
max = arcpy.GetParameterAsText(3)
min = arcpy.GetParameterAsText(4)
expression = "getClass(!{0}!,{1},{2})".format(Select,min,max)
codeblock = """
def getClass(i, min, max):
a = 123
if (i>min and i<max):
return 1
return 0"""

# Process: Add Field
arcpy.AddField_management(filename, Field, "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(filename, Field, expression, "PYTHON_9.3", codeblock)

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

your second dialog will not run X times with different inputs for each loop.

You can make your parameters all multivalue and assemble the inputs prior to running.  

I am not sure why your proposed workflow is any different that running the tool X times since you still have to provide the inputs.

I would suggest dumping your first dialog, so you can assemble the inputs, then run it

0 Kudos
sobhansai
New Contributor II

Thank you so much sir.I need one more help is there any way to get the values for each file of the multiple files in the second script.

for example:

if there are 3files:

for first files it has to take the values

and after that when the second file has been given as input it must take the other values.

and same for the third file also.

can u please help us with the logic or explain how to do it.

regards in advance.

0 Kudos
VinceAngelo
Esri Esteemed Contributor

Note that this is a cross-post of three additional questions on GIS StackExchange, all of which were closed for lack of code. The best way to get help with code is to provide code.

- V