Python
When creating workflows, you'll often need to write dynamic expressions. This page provides an overview of enabling Python expressions and what functions and objects you can use.
Installing Python
Before we can use Python Expressions, we first need to have Python itself installed.
Windows
Follow these steps to install Python on Windows.
Download the Installer
Visit the official Python downloads page.
Click on the "Download Windows installer" link for the latest version.
Run the Installer
Locate the downloaded
.exefile and double-click to start the installation.In the installer window, ensure you check the box labeled "Add Python to PATH."
Click "Install Now" and follow the on-screen instructions.
Verify the Installation
Open Command Prompt (
cmd).Type
python --versionand press Enter.You should see the Python version number displayed.
Find the Python Installation Path
To locate the Python installation directory on Windows:
Open Command Prompt.
Type
where pythonand press Enter.This command will display the path to the Python executable.
The shared library (python38.dll) is typically located in the same directory as the Python executable.
Take note of the full path to the python38.dll file. We will need it shortly.
MacOS
Follow these steps to install Python on MacOS.
Download the Installer
Navigate to the official Python downloads page.
Click on the "Download macOS 64-bit installer" link for the latest version.
Run the Installer
Locate the downloaded
.pkgfile and double-click to start the installation.Follow the on-screen instructions to complete the installation.
Verify the Installation
Open Terminal.
Type
python3 --versionand press Enter.You should see the Python version number displayed.
Find the Python Installation Path
Open Terminal.
Type the following command and press Enter:
which python3This will display the path to the Python executable.
The shared library (libpython3.8.dylib) is typically located in the lib directory within the Python installation path.
Take note of the full path to the libpython3.8.dylib file. We will need it shortly.
Configure Environment Variables
Make sure to configure the PYTHONNET_PYDLL environment variable to point to the python DLL found in the previous step.
Also make sure to set PYTHONNET_RUNTIME to coreclr
Installing the Python Feature
The Python Expressions feature is provided by the following package:
dotnet package add Elsa.PythonYou can enable the feature as follows:
services.AddElsa(elsa =>
{
elsa.UsePython();
});Configuration
The UsePython extension provides an overload that accepts a delegate that lets you configure the PythonFeature, which itself exposes a delegate to configure PythonOptions.
For example:
services.AddElsa(elsa =>
{
elsa.UsePython(python =>
{
python.PythonOptions += options =>
{
options.AddScript(sb =>
{
sb.AppendLine("def greet():");
sb.AppendLine(" return \"Hello, welcome to Python!\"");
});
}
});
});Globals
The following functions and objects are available to all Python expressions:
output
The output object provides methods to access an activity's output.
# Get the output of the specified activity, optionally specifying a specific output.
get(string, string?): object?
# Gets the output of the last executed activity.
last_result(): object?input
variables
The variables object provides access to the workflow variables. For example, if your workflow has a variable called OrderId, you can get and set that workflow variable using the following Python expression:
import uuid
# Set the OrderId workflow variable.
variables.OrderId = uuid.uuid4();
# Get the OrderId workflow variable.
orderId = variables.OrderId;
# Set the OrderId workflow variable using the 'set' method.
variables.set("OrderId", uuid.uuid4())
# Get the OrderId workflow variable using the 'get' method.
orderId = variables.get("OrderId")The input object provides access to workflow input.
input.get(string): object?execution_context
The execution_context object provides access to the following information:
workflow_instance_id: string
correlation_id: stringLast updated