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.

1

Download the Installer

2

Run the Installer

  • Locate the downloaded .exe file 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.

3

Verify the Installation

  • Open Command Prompt (cmd).

  • Type python --version and press Enter.

  • You should see the Python version number displayed.

4

Find the Python Installation Path

To locate the Python installation directory on Windows:

  • Open Command Prompt.

  • Type where python and 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.

1

Download the Installer

2

Run the Installer

  • Locate the downloaded .pkg file and double-click to start the installation.

  • Follow the on-screen instructions to complete the installation.

3

Verify the Installation

  • Open Terminal.

  • Type python3 --version and press Enter.

  • You should see the Python version number displayed.

4

Find the Python Installation Path

  • Open Terminal.

  • Type the following command and press Enter:

    which python3
  • This 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

Refer to the Pythonnet GitHub project for detailed documentation.

Installing the Python Feature

The Python Expressions feature is provided by the following package:

dotnet package add Elsa.Python

You can enable the feature as follows:

Program.cs
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:

Program.cs
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: string

Last updated