This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.
Now that the interface and configuration files are set up, we can start creating the tool's logic in the Python script main.py (src > radarPlot > engine > src)!
First, add code to import the additional required libraries for our tool. Our tool will requirematplotlib, as well as the pi function from the math package, and re (for regex functionality).
# 3rd Party Libraries import AlteryxPythonSDK as sdk from snakeplane.plugin_factory import PluginFactory # Our added libraries import matplotlib.pyplot as plt from math import pi import re
To make sure these packages end up in our final tool, we should add them to the SnakePlane Pilot virtual environment.If you are using Pycharm, the easiest way to do this is to navigate to File > Settings > Projectand click the little plus button on the right-hand side to add a package. We will only need to add matplotlib.
When you build your tool, these packages will be included in the tool’s virtual environment, so it is important to keep tabs on what you add, and what really needs to be packaged with your tool.
Back in the Python code (main.py) we need to modify the plugin factory constructor to reference the name of our new tool. In the line of code that reads PluginFactory("ExampleStreamTool"), replace ExampleStreamTool with radarPlot.
# Initialization of the plug in factory, used for making the AyxPlugin class factory = PluginFactory("radarPlot")
Note: it is important that the name you provide the plugin factory constructor matches the directory name of the tool exactly. This is because the plugin factory uses this provided name to locate the configuration (XML)file for the plugin. If this argument is not set correctly, you will not be able to use your tool in Alteryx.
Next, we encounter our first decorator, initialize plugin!This is where we will specify the specific plugin initialization behavior.
Delete all of the code in the example tool between the comment """Initialize the example tool""" and return True.
In this section, we are going to add code that returns the configuration options set by the user in Alteryx to the Python script.In our GUI, there are two configuration options; the fields to be plotted on the radar chart (the listbox widget) and the field that contains the titles of the plots (a drop down).
Using the input_mgr.workflow_config()SnakePlane function, we can call the tool's configuration in a workflow by referencing the dataName we gave to the interface widgets in the Gui file (our listbox is named "fields" and our title drop-down widget is named "names").
We will save these configuration inputs as user_data.fields and user_data.names, respectively. Saving the input configurations inuser_data allows the information to be accessed later in the script.
# Get the selected value from the GUI and save it for later use in the user_data user_data.fields = (input_mgr.workflow_config["fields"]) user_data.names = (input_mgr.workflow_config["names"])
Next, we can add code to return error messages at initialization if the user does not configure the tool. We want to start by setting the variable initSuccess to True by default, and then write two if statements that check if the user_data variables are equal to None. If they are, we use the logger functionality to display an error message, and set the initSucess to False.
initSuccess = True if user_data.fields is None: logger.display_error_msg("Select Fields to Plot") initSuccess = False if user_data.names is None: logger.display_error_msg("Select the Field that Contains the Plot Labels") initSuccess = False
At the end of the factory.initialize_plugin, we want to return the variable initSuccess. If the interface has been configured correctly, initiSuccess will be equal to True, and we will be able to run the tool and move on to the next section of code. If initSuccess is False, the user will see an error message in Alteryx instructing them to fix their configuration.
The next decorator,@factory.process_datais (intuitively) for processing data. This section is where the bulk of your custom tool code will go.
The first thing to note about the factory.process_data() decorator are the two arguments mode and input_type.
Mode allows us to specify which mode we want the plugin to operate in. There are two possible modes; batch and stream. Batch mode brings in all of the input records all at once, which is handy if you are building a tool that needs to see all records simultaneously (like a predictive tool). Stream mode brings in the input records a single row at a time.
Input_type specifies the format of the input data. The two options are list (this is the default) and dataframe. List brings in data as a single list in stream mode or a list of lists in batch mode. Dataframe brings in the input as a pandas dataframe.
For this tool, we will set the mode to stream and the input_type to dataframe.