<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ESRI python toolbox how to pass variables between tools in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1359457#M69382</link>
    <description>&lt;P&gt;I've attached a simple example (counts the number of times UpdateParameters is called) of how I share data between tools in a python toolbox. My guess is that it is similar to what you are already doing - using globals. There probably is a more elegant way but I've run into the same problems that you describe and finally settled on this because it does work.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

import arcpy

tool_1_data = dict()
tool_2_data = dict()

class Toolbox(object):
    def __init__(self):
        self.label = "Toolbox"
        self.alias = "toolbox"
        self.tools = [Tool_1, Tool_2]


def get_tool_data (tool_data_dict, tool_data_key):
    if tool_data_key in tool_data_dict.keys():
        return tool_data_dict[tool_data_key]
    else:
        return None
    
    
class Tool_1(object):
    def __init__(self):
        self.label = "Tool_1"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        global tool_1_data
        if len(tool_1_data) == 0:
            tool_1_data = {'my_count': 0}
        my_count = arcpy.Parameter(displayName="My Count",  name="my_count", datatype="GPLong", parameterType="Optional", direction="Input")
        other_count = arcpy.Parameter(displayName="Other Count",  name="other_count", datatype="GPLong", parameterType="Optional", direction="Input")        
        return [my_count, other_count]

    def updateParameters(self, parameters):
        global tool_1_data
        global tool_2_data
        tool_1_data['my_count'] += 1
        parameters[0].value = get_tool_data(tool_1_data, 'my_count')
        parameters[1].value = get_tool_data(tool_2_data, 'my_count')
        return

    def execute(self, parameters, messages):
        return
   
class Tool_2(object):
    def __init__(self):
        self.label = "Tool_2"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        global tool_2_data
        if len(tool_2_data) == 0:
            tool_2_data = {'my_count': 0}
        my_count = arcpy.Parameter(displayName="My Count",  name="my_count", datatype="GPLong", parameterType="Optional", direction="Input")
        other_count = arcpy.Parameter(displayName="Other Count",  name="other_count", datatype="GPLong", parameterType="Optional", direction="Input")        
        return [my_count, other_count]

    def updateParameters(self, parameters):
        global tool_1_data
        global tool_2_data
        tool_2_data['my_count'] += 1
        parameters[0].value = get_tool_data(tool_2_data, 'my_count')
        parameters[1].value = get_tool_data(tool_1_data, 'my_count')
        return

    def execute(self, parameters, messages):
        return
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Dec 2023 23:20:34 GMT</pubDate>
    <dc:creator>DonMorrison1</dc:creator>
    <dc:date>2023-12-11T23:20:34Z</dc:date>
    <item>
      <title>ESRI python toolbox how to pass variables between tools</title>
      <link>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1357797#M69348</link>
      <description>&lt;P&gt;i made a python toolbox with various tools to interact with a Portal. i set it up using a global variable that would hold the portal info so i dont have to get it for every tool. This created issues as other tools not in this toolbox would raise errors that this global varibale is not defined.&lt;/P&gt;&lt;P&gt;i defined the variable the Toolbox class __init__, i am confused how the __init__ method of the Toolbox class is being called when that toolbox is not open&lt;/P&gt;&lt;P&gt;That aside i know its not great practice to use global variables and i cant control the above issues so i went down the road of trying to pass the now non-global variable between the tools in the Toolbox by setting it as an attribute of Toolbox in the __init__ method and then inheriting the Toolbox class in each Tool Class&lt;/P&gt;&lt;P&gt;This ran in to the problem&lt;/P&gt;&lt;P&gt;"TypeError: Cannot create a consistent method resolution&lt;BR /&gt;order (MRO) for bases object, Toolbox"&lt;/P&gt;&lt;P&gt;so i can use the inheritance method as both of these classes must be already inherited into some class when it runs.&lt;/P&gt;&lt;P&gt;How can i pass variables either from the Toolbox class to each tool or even just between tools?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Dec 2023 01:04:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1357797#M69348</guid>
      <dc:creator>AntEsk</dc:creator>
      <dc:date>2023-12-07T01:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: ESRI python toolbox how to pass variables between tools</title>
      <link>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1358337#M69360</link>
      <description>&lt;P&gt;i could not figure out a way to do this, not sure that it is possible.&lt;/P&gt;&lt;P&gt;instead i went back to the global but I am creating the variable in the init of each tool if it hasn't been created. i have concluded that the changes to the Toolbox class i made must be getting inherited into Toolbox class that all tools use.&lt;/P&gt;&lt;P&gt;if anyone knows how to pass the variables btw tools let me know as&amp;nbsp; i would prefer to not use the global&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Dec 2023 22:13:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1358337#M69360</guid>
      <dc:creator>AntEsk</dc:creator>
      <dc:date>2023-12-07T22:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: ESRI python toolbox how to pass variables between tools</title>
      <link>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1359457#M69382</link>
      <description>&lt;P&gt;I've attached a simple example (counts the number of times UpdateParameters is called) of how I share data between tools in a python toolbox. My guess is that it is similar to what you are already doing - using globals. There probably is a more elegant way but I've run into the same problems that you describe and finally settled on this because it does work.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -*- coding: utf-8 -*-

import arcpy

tool_1_data = dict()
tool_2_data = dict()

class Toolbox(object):
    def __init__(self):
        self.label = "Toolbox"
        self.alias = "toolbox"
        self.tools = [Tool_1, Tool_2]


def get_tool_data (tool_data_dict, tool_data_key):
    if tool_data_key in tool_data_dict.keys():
        return tool_data_dict[tool_data_key]
    else:
        return None
    
    
class Tool_1(object):
    def __init__(self):
        self.label = "Tool_1"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        global tool_1_data
        if len(tool_1_data) == 0:
            tool_1_data = {'my_count': 0}
        my_count = arcpy.Parameter(displayName="My Count",  name="my_count", datatype="GPLong", parameterType="Optional", direction="Input")
        other_count = arcpy.Parameter(displayName="Other Count",  name="other_count", datatype="GPLong", parameterType="Optional", direction="Input")        
        return [my_count, other_count]

    def updateParameters(self, parameters):
        global tool_1_data
        global tool_2_data
        tool_1_data['my_count'] += 1
        parameters[0].value = get_tool_data(tool_1_data, 'my_count')
        parameters[1].value = get_tool_data(tool_2_data, 'my_count')
        return

    def execute(self, parameters, messages):
        return
   
class Tool_2(object):
    def __init__(self):
        self.label = "Tool_2"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        global tool_2_data
        if len(tool_2_data) == 0:
            tool_2_data = {'my_count': 0}
        my_count = arcpy.Parameter(displayName="My Count",  name="my_count", datatype="GPLong", parameterType="Optional", direction="Input")
        other_count = arcpy.Parameter(displayName="Other Count",  name="other_count", datatype="GPLong", parameterType="Optional", direction="Input")        
        return [my_count, other_count]

    def updateParameters(self, parameters):
        global tool_1_data
        global tool_2_data
        tool_2_data['my_count'] += 1
        parameters[0].value = get_tool_data(tool_2_data, 'my_count')
        parameters[1].value = get_tool_data(tool_1_data, 'my_count')
        return

    def execute(self, parameters, messages):
        return
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 23:20:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1359457#M69382</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2023-12-11T23:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: ESRI python toolbox how to pass variables between tools</title>
      <link>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1359970#M69397</link>
      <description>&lt;P&gt;Thansk for replying, that is basically what i have done, i just have the global defined in a function outside of the Tool class and called within the __inti__ of the tool so it only needs to be made once.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 21:33:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/esri-python-toolbox-how-to-pass-variables-between/m-p/1359970#M69397</guid>
      <dc:creator>AntEsk</dc:creator>
      <dc:date>2023-12-12T21:33:17Z</dc:date>
    </item>
  </channel>
</rss>

