02-04-2019 03:00 PM - edited 07-23-2021 05:13 PM
How To: Manage Metadata in the Python tool
Starting with Alteryx Designer 2018.4, the Python tool can now read and write metadata for better communication with the rest of the workflow. This is accomplished using twofunctions included in theayxPython library; readMetadata() and write() with the argument columns=. Like other functions related to input/output, one needs to run the workflow first in order to access data.
Remark: with 2018.4, metadata field description could not be retrievedor modified.
Prerequisites
1. Load moduleAlteryx from packageayx.
from ayx import Alteryx
2. Load input metadata with functionreadMetadata,specifying the input name as the argument.
#this example reads the connected input #1 Alteryx.readMetadata(incoming_connection_name='#1')
In this example, we use the data set found under %ALTERYX_DIRECTORY%\Samples\en\SampleData\CompetitorLocations.tab
Procedure: Writing Metadata
1. Load moduleAlteryx from packageayx.
from ayx import Alteryx
2. Describe the metadata using the following format:
new_metadata = {'PRMSIC' : {'type' : ' Int64'}}
Structure:
{ '%COLUMN NAME%' : { 'type': '%ALTERYX DATATYPE%', 'length': %DATATYPE LENGTH%, 'source': '%DATATYPE SOURCE%', 'description': '%DATATYPE DESCRIPTION%'} }
Where :
%COLUMN NAME% : Column to change (case insensitive).
%ALTERYX DATATYPE% : One of theAlteryx Designer datatypes.
%DATATYPE LENGTH% : Size of the field (integer or float for Fixed Decimalof the field (note that it is called sizein result window and lengthhere)).Length is mandatory for string datatypes and optional otherwise.
%DATATYPE SOURCE% : Source of Alteryx data (default value: PythonTool:)
%DATATYPE DESCRIPTION% : Free field to comment field (default value: empty)
3. Write the data out with the defined metadata.
#this example writes out to output anchor 1 Alteryx.write(pandas_df=input_df,columns=new_metadata,outgoing_connection_number=1)
Example:
Just a quick addition: you need to run the entire workflow (not just the jupyter notebook) before attempting
Alteryx.readMetadata(incoming_connection_name='#1')
or else Alteryx will look for the connection in \AppData\Local\Temp
Hi @nakamott,
Thanks for the comment! Post will be updated accordingly.
You are correct: one needs to run the workflow first, in order to be able to access tool input/output details. Temporary data and Jupyter notebook will be stored in engine temporary path for the time of the Designer session.
Note that further details could be obtained when debug parameter is set to True.
Example:
Alteryx.readMetadata('#1',debug=True)
Best,
Paul Noirel
Sr Customer Support Engineer, Alteryx