Dev Space

Customize and extend the power of Alteryx with SDKs, APIs, custom tools, and more.

How to get the user selected dropdown widget value in Python script of Alteryx Python SDK?

amitupadhyay
8 - Asteroid

Dear community members,

 

I am using a Drop down widget for the user to select a list of values. Here is my HTML code snippet  for the same:

<fieldset>
				<div class="leftCon">
					<label>XMSG("Select Hashing algorithm")</label>
						<ayx 
						data-ui-props = "{type: 'DropDown', widgetId: 'HashAlgos'}"
						data-item-props = "{dataName: 'HashingField'}"
						>
						</ayx>
				</div>
</fieldset>

Further I defined the label and values down below in the script and binded the widget.

<script type="text/javascript">                     
			Alteryx.Gui.BeforeLoad = (manager, AlteryxDataItems, json) => {
			// Drop Down for selecting Hashing algorithm
			var stringSelector = new AlteryxDataItems.StringSelector("HashingField", {
				optionList: [
					{label: "MD5", value: "md5"},
					{label: "SHA1", value: "sha1"},
					//{label: "CRC32", value: "crc32"},
					{label: "SHA256", value: "sha256"},
					{label: "SHA512", value: "sha512"}
				]
			})
			manager.addDataItem(stringSelector)
			manager.bindDataItemToWidget(stringSelector, "HashAlgos")
			}
			Alteryx.Gui.AfterLoad = (manager) => {
			}
		</script>

In AyxPlugin class,

class AyxPlugin:
    def __init__(self, n_tool_id: int, alteryx_engine: object, output_anchor_mgr: object):
        # Default properties
        self.n_tool_id: int = n_tool_id
        self.alteryx_engine: Sdk.AlteryxEngine = alteryx_engine
        self.output_anchor_mgr: Sdk.OutputAnchorManager = output_anchor_mgr
        self.label = "Encoded Signature Generator Tool (" + str(n_tool_id) + ")"

        # Custom properties
        self.HashingField: str = ''
        self.Output: Sdk.OutputAnchor = None

    def pi_init(self, str_xml: str):
        xml_parser = Et.fromstring(str_xml)
        self.HashingField = xml_parser.find('HashingField').text if 'HashingField' in str_xml else ''
        
        if self.HashingField == '':
            self.display_error_msg('Key and Secret or Hashing algorithm or both parameters were not provided.')
       
        # Getting the output anchor from Config.xml by the output connection name
        self.Output = self.output_anchor_mgr.get_output_anchor('Output')

In Incoming Interface class:

class IncomingInterface:
    def __init__(self, parent: AyxPlugin):
        # Default properties
        self.parent: AyxPlugin = parent

        # Custom properties
        self.InInfo: Sdk.RecordInfo = None
        
        self.HField: Sdk.Field = None
        
        self.OutInfo: Sdk.RecordInfo = None
        
        self.SignatureField: Sdk.Field = None
        
        self.Creator: Sdk.RecordCreator = None
        self.Copier: Sdk.RecordCopier = None

    
    
    def ii_init(self, record_info_in: Sdk.RecordInfo) -> bool:
        self.InInfo = record_info_in
        
        self.HField = self.InInfo.get_field_by_name(self.parent.HashingField)
        
        self.OutInfo = self.InInfo.clone()
        self.SignatureField = self.OutInfo.add_field('Signature', Sdk.FieldType.string, size=1000, source=self.parent.label)
        
        self.Creator = self.OutInfo.construct_record_creator()
        self.Copier = Sdk.RecordCopier(self.OutInfo, self.InInfo)

        index = 0
        while index < self.InInfo.num_fields:
            self.Copier.add(index, index)
            index += 1
        self.Copier.done_adding()
        self.parent.Output.init(self.OutInfo)
        return True

    def ii_push_record(self, in_record: Sdk.RecordRef) -> bool:
        Hash = self.HField.get_as_string(in_record)
        Signature = Hash

        self.SignatureField.set_from_string(self.Creator, Signature)
        
        self.Creator.reset()
        self.Copier.copy(self.Creator, in_record)
        out_record = self.Creator.finalize_record()
        self.parent.Output.push_record(out_record)
        return True

I do not get the values of 'Hash' as selected by the user from the drop down. Can anyone point to me what mistake I am doing? I would really appreciate if someone can correct me. I want the value of Hash = value selected by the user from the dropdown list.

2 REPLIES 2
tlarsen7572
11 - Bolide
11 - Bolide

Hi @amitupadhyay, I created a quick mock-up using your code snippets and I was able to retrieve the value of the drop-down. What do you get if you are not getting the hash values? What is the XML string being sent to the Python code?

 

In my code I'm sending a message to the engine with the XML received and the value of HashingField. I got both values as expected:

Screen Shot 2021-11-06 at 4.40.17 PM.png

amitupadhyay
8 - Asteroid

Thank you for responding. I was getting the hash values as Field Names. However, I solved the problem. In the incoming interface where I have declared a separate field for hashing algorithm value was a wrong step. Instead, I called the hashing value from the Alteryx Plugin class as parent.HashingField and it worked.