How can I force a python toolbox to refresh?

7357
7
08-13-2014 11:46 AM
YuvalHadas
New Contributor

In order to ease the entry of the parameters, whenever the toolbox is opened, the latest parameters are being read from a table, and if changed are saved back. The problem is that the toolbox must be manually refreshed every time for the parameters to be displayed correctly.

def getParameterInfo(self):

cursor = arcpy.da.SearchCursor("Parameters", ['data_set', 'date_p', 'from_p', 'to_p', 'location_p', 'wait_p', 'distance_p'])

for row in cursor:

  ds=row[0]

  date_p=row[1]

  from_p=row[2]

  to_p=row[3]

  location_p=row[4]

  wait_p=row[5]

  distance_p=row[6]

  l_ds="results-"+ds

param0 = arcpy.Parameter(

  displayName="Destinations",

  name="in_destinations",

  datatype="GPString",

  parameterType="Required",

  direction="Input")

param0.filter.type = "ValueList"

values = [row[0] for row in arcpy.da.SearchCursor(l_ds, ("location"))]

param0.value=location_p

lst = list(set(values))

lst.sort()

param0.filter.list = lst

.

.

.

def execute(self, parameters, messages):

arcpy.env.workspace =""

location_p=parameters[0].valueAsText

date_p=parameters[1].valueAsText

from_p=parameters[2].valueAsText

to_p=parameters[3].valueAsText

wait_p=parameters[4].valueAsText

distance_p=parameters[5].valueAsText

cursor = arcpy.da.UpdateCursor("Parameters", ['date_p', 'from_p', 'to_p', 'location_p', 'wait_p', 'distance_p'])

for row in cursor:

  row[0]=date_p

  row[1]=from_p

  row[2]=to_p

  row[3]=location_p

  row[4]=wait_p

  row[5]=distance_p

  cursor.updateRow(row)

.

.

.

Tags (3)
0 Kudos
7 Replies
JasonScheirer
Occasional Contributor III

Do it in the updateParameters method.

def getParameterInfo(self):

    with arcpy.da.SearchCursor("Parameters", ['location_p']) as cursor:

      for row in cursor:

        location_p = row[0]

    param0 = arcpy.Parameter(

      displayName="Destinations",

      name="in_destinations",

      datatype="GPString",

      parameterType="Required",

      direction="Input")

    param0.filter.type = "ValueList"

    param0.filter.list = []

    param0.value = location_p

    return [param0]

def updateParametrs(self, parameters)

    param0 = parameters[0]

    with arcpy.da.SearchCursor("Parameters", ['data_set']) as cursor:

      for row in cursor:

        l_ds = "results-" + row[0]

    values = sorted(set([row[0] for row in arcpy.da.SearchCursor(l_ds, ("location"))]))

    param0.filter.list = values

YuvalHadas
New Contributor

I think I was not clear with the problem.

Parameter A's value in the file is "0" (from previous execution).

Toolbox is being refreshed from the catalog window.

First execution of the toolbox. Parameter A default value is being read from the file. On screen default value is "0". Parameter A is set to "1". The rest of execution is with value "1". Value "1" is updated to the file.

Second execution of the toolbox. Parameter A default value is being read from the file. On screen default value is "0" (should have been "1"). Parameter A is set to "2". The rest of execution is with value "2". Value "2" is updated to the file.

Toolbox is being refreshed from the catalog window.

Third execution of the toolbox. Parameter A default value is being read from the file. On screen default value is "2" (due to refresh)...

0 Kudos
JasonScheirer
Occasional Contributor III

This is not directly supported in the geoprocessing framework. You could get around it by writing the name of the data to a file in .execute and having the .getParameterInfo() read it when it loads.

YuvalHadas
New Contributor

I believe that this is what I've done. the .getParameterInfo() loads the data, while the .execute() saves the data. As I stated, the data is saved and executed correctly, but it does not assigned as default value, unless I refresh the toolbox.

class select_data(object):

  def __init__(self):

  """Define the tool (tool name is the name of the class)."""

  self.label = "Select Data"

  self.description = ""

  self.canRunInBackground = False

  def getParameterInfo(self):

  """Define parameter definitions"""

  cursor = arcpy.da.SearchCursor("Parameters", ['data_set'])

  for row in cursor:

  ds=row[0]

  param0 = arcpy.Parameter(

  displayName="File",

  name="in_file",

  datatype="GPString",

  parameterType="Required",

  direction="Input")

  param0.value=ds

  params = [param0]

  return params

  def isLicensed(self):

  """Set whether tool is licensed to execute."""

  return True

  def updateParameters(self, parameters):

  """Modify the values and properties of parameters before internal

  validation is performed.  This method is called whenever a parameter

  has been changed."""

  return

  def updateMessages(self, parameters):

  """Modify the messages created by internal validation for each tool

  parameter.  This method is called after internal validation."""

  return

  def execute(self, parameters, messages):

  """The source code of the tool."""

  arcpy.env.workspace =""

  cursor = arcpy.da.UpdateCursor("Parameters", ['data_set'])

  for row in cursor:

  row[0]=parameters[0].valueAsText

  cursor.updateRow(row)

  ds="-"+parameters[0].valueAsText

  stp1=arcpy.mapping.Layer("direct_xfer_trips")

  stp2=arcpy.mapping.Layer("direct_trips")

  stp3=arcpy.mapping.Layer("xfer_trips")

  stp4=arcpy.mapping.Layer("xfer_pot")

  mxd = arcpy.mapping.MapDocument("CURRENT")

  stp1.replaceDataSource("shape_files","SHAPEFILE_WORKSPACE","stops"+ds)

  stp2.replaceDataSource("shape_files","SHAPEFILE_WORKSPACE","stops"+ds)

  stp3.replaceDataSource("shape_files","SHAPEFILE_WORKSPACE","stops"+ds)

  stp4.replaceDataSource("shape_files","SHAPEFILE_WORKSPACE","stops"+ds)

  del mxd

  return

0 Kudos
BillDaigle
Occasional Contributor III

The problem is... the 'getParameters' function only runs once -- when the toolbox is refreshed.  In order to do what you want without refreshing, you will need to use the 'updateParameters' functions, as Jason Scheirer​ suggested.  This runs every time a parameter is updated. I'm not sure if it will run as soon as you open the tool.  If not, you could add another Boolean parameter that trigger the "Update from Table" and disable the parameter once the parameters have been updated. 

0 Kudos
CharlesGasse1
New Contributor

Here is a way you can refresh a python toolbox:

Python toolbox doesn't want to refresh

Have a good day.

Charles

Luke_Pinner
MVP Regular Contributor

The OP's question is referring to running a tool from ArcToolbox and my answer to your question about running a tool from a Python Addin using the pythonaddins.GPToolDialog won't work here.

0 Kudos