Python 2 or 3 Script
Use the Python Script plugins to quickly manipulate data. All Python plugins only have one action: run an arbitrary function that you configure. Feed the function any JSON objects, including output objects from previous steps in the workflow.
InsightConnect supports both Python 2 and 3, so you can work with whichever version you prefer or are more comfortable with. Both plugins include the following third-party Python libraries:
records. Any imports must be within the
Set Request Timeouts
Set a timeout value when making requests to external servers in case the server isn't responding in a timely manner. Without a timeout value, your code may hang for several minutes or more because requests don't time out unless you explicitly set a value. Hanging code can be an issue for frequently used plugins, and plugins used by multiple workflows. The plugin may become unresponsive to new jobs because it's stuck waiting for incomplete jobs. The following Requests documentation provides more details on how to set timeout values: https://2.python-requests.org/en/master/user/advanced/#timeouts.
Import Third Party Libraries
Depending on your usage, you may want to import third party libraries to use in your Python script. To use these third party libraries, include individual libraries in the connection of the Python plugin.
You can install any 3rd party module available publicly on PyPi. Other modules are not supported.
To configure the Python connection, select the Python plugin during a workflow building session or create the connection independently by choosing Plugins & Tools from the Settings tab on the left menu. On the Plugins & Tools page, select the Connections tab and click Add Connection in the upper-right corner.
In the Connection page:
- Input a unique and identifiable connection name and choose an orchestrator to run this plugin on.
- Navigate to the "Connection Parameters" section.
- In the Third-Party Modules field, add the third party libraries, enclosed in double quotes, that you wish to use. For example, your Third-Party modules field may look like
- Ensure the libraries are installed in the connection by adjusting the number of seconds to install the libraries in the Timeout field.
- Save this connection and verify the status by navigating to the Connections Page.
Setup a Python Plugin
When creating a new step in the Workflow Builder:
- Choose Action Step.
- Search for “Python Script 2” or “Python Script 3,” depending on your preference.
- Click on the plugin of your choice, then click Continue.
- Click on Run function, then click Continue.
- In the Configure Details panel, type in your desired Action name.
- Check the box if you want the workflow to continue running even if this step fails.
Configure the "run" Function
First, pass in JSON objects created by previous steps to the function through the Input field, then customize how the function manages input and output directly in the run function. Learn how to format JSON input here.
To customize the
- Import any needed libraries inside the
runfunction. Both plugins include the following libraries:
records. Other additional Python libraries can be specified in the connection of the plugin.
- Create any local function variables if needed. You can set these to JSON objects or values of other available data types.
- Pass in input variables with built-in method
runfunction uses the key names created by your JSON input to run Python script on your desired variables. To call these variables, add the key name enclosed by double quotes, like
- Create and format output variables in the return statement. Each return variable key must be enclosed in single quotes. To assign values to these variables, follow each key with a colon and the desired output value. These output values can be one of four things:
- Local function variables by name
- Strings, enclosed in double quotes
- Click anywhere outside the Function field to close the editor.
Logging messages To log messages with the Python plugin, import logging and call logging.info() or other logging methods in the run function. print or equivalent statements will not successfully log messages due to how output is handled by the plugin.