Alteryx Python Functions
Important
The functions below are available as part of the Alteryx package when you run from ayx import Alteryx. The functions let you pass data between Alteryx and Jupyter.
Read Data
Alteryx.read("<input connection name>"): Returns the input data as a pandas DataFrame. Input anchors are 1-based: "#1", "#2", etc.
Examples
df = Alteryx.read("#1") returns SUCCESS: reading input data "#1".
Read Metadata
Alteryx.readMetadata("<input connection name>" or <pandas dataframe>): Returns the input field metadata as a dict. Input anchors are 1-based: "#1", "#2", etc.
Examples
md = Alteryx.readMetadata("#1"); print(md);returns {'Field1': {'type': 'Byte', 'length': 1, 'source': 'TextInput:', 'description': ''}}.md = Alteryx.readMetadata(df); print(md);returns {'Field1': {'type': 'Byte', 'length': 1, 'source': '', 'description': ''}}.
Write Data
Alteryx.write(<pandas dataframe>, <output anchor number>, [metadata list or dict]): Passes the full dataframe to Alteryx when the workflow is executed. You can pass in metadata optionally, either as a list (using column order) or dict (using column names). Output anchors are 1-based: "#1", "#2", etc.
Use Alteryx.write to output data from the Python tool. To send data to other tools on the canvas, use the method like this:
# Replace "PANDAS_DF" with your data, in DataFrame form. # Replace "OUTPUT_ANCHOR_NUM" with the number of your output anchor. Alteryx.write(PANDAS_DATAFRAME, OUTPUT_ANCHOR_NUM)
The Alteryx.write method accepts only Pandas DataFrames. If you have data in another format, use the Pandas library to convert it to a DataFrame. The Pandas library is already installed with Designer. Access it via import pandas.
You can send up to five DataFrames to the output anchors.
Examples
Alteryx.write(df, 1)returns SUCCESS: writing outgoing connection data 1.Alteryx.write(df, 2, Alteryx.readMetadata("#1"))returns SUCCESS: writing outgoing connection data 2.Alteryx.write(df, 3, {'Field1': {'name': 'Field1_as_double', 'type': 'Double'}})returns SUCCESS: writing outgoing connection data 3.Alteryx.write(df, 4, [{'name': 'Field1_as_string', 'type': 'V_String', 'length': 8}])returns SUCCESS: writing outgoing connection data 4.
List Incoming Connections
Alteryx.getIncomingConnectionNames( ): Returns a list that contains all incoming data connections. If the connections look out of sync, re-run the Alteryx workflow.
Examples
Alteryx.getIncomingConnectionNames() returns ["#1", "#2", "model"].
Install Packages
Alteryx.installPackages("<package name or list of package names>"): Installs packages from PyPI. An internet connection is required. Also, if using an admin install of Alteryx, Alteryx must be opened in admin mode to install packages. Non-admin installs do not have this restriction.
Examples
Alteryx.installPackages("tensorflow")Alteryx.installPackages(["keras","theano","gensim"])
Workflow Constants
Get Workflow Constant
Alteryx.getWorkflowConstant("<constant name>"): Returns the value of an Alteryx workflow constant (you can access these in the Workflow Configuration's Workflow tab in Designer).
Important
Avoid using % wrappers in workflow constants. Instead, for example, use this to call the Engine.WorkflowDirectory:
from ayx import Alteryx
Alteryx.getWorkflowConstant("Engine.WorkflowDirectory")Examples
Alteryx.getWorkflowConstant("Engine.TempFilePath") returns 'C:\Users\myuser\AppData\Local\Temp\Engine_17376_1afec4920f674e3a8c29be6225048da8_\'.
Get All Constants
Alteryx.getWorkflowConstants( ): Returns the value of all Alteryx workflow constants in a dict.
Examples
Alteryx.getWorkflowConstants() returns:
{
'Engine.GuiInteraction': '1',
'Engine.IterationNumber': 0,
'Engine.ModuleDirectory': 'C:\my_workflows\',
'Engine.TempFilePath': 'C:\Users\myuser\AppData\Local\Temp\Engine_17376_1afec4920f674e3a8c29be6225048da8_\',
'Engine.Version': '2018.3.0.9999',
'Engine.WorkflowDirectory': 'C:\my_workflows\',
'Engine.WorkflowFileName': 'my_workflow.yxmd'
}Import Python Modules
Alteryx.importPythonModule("<path>", [list of submodules]): Returns a module object that represents the Python file or directory specified. By default, if a directory is provided, all submodules are imported, but you can specify a list of submodules with the optional submodules argument.
Examples
myscript = Alteryx.importPythonModule("C:\\documents\\my_script.py")myscript.square(3)returns 9.mypkg = Alteryx.importPythonModule("C:\\documents\\my_package")mypkg.module1.half( mypkg.subpkg.max_value )returns 4
The above code snippets are based on the following files and folders:
C:\documents C:\documents\my_script.py ^^^ (contains a function square() that returns the input value squared) C:\documents\my_package C:\documents\my_package\__init__.py C:\documents\my_package\module1.py ^^^ (contains a function half() that returns half of the input value) C:\documents\my_package\subpkg C:\documents\my_package\subpkg\__init__.py ^^^ (contains a variable max_value = 8)
