LogoLogo
GitHub
  • Elsa Workflows 3
  • Getting Started
    • Concepts
      • Outcomes
      • Correlation ID
    • Hello World
    • Prerequisites
    • Packages
    • Containers
      • Docker
      • Docker Compose
        • Elsa Server + Studio
        • Elsa Server + Studio - Single Image
        • Persistent Database
        • Traefik
  • Application Types
    • Elsa Server
    • Elsa Studio
    • Elsa Server + Studio (WASM)
  • Guides
    • HTTP Workflows
      • Programmatic
      • Designer
    • External Application Interaction
    • Loading Workflows from JSON
    • Running Workflows
      • Using Elsa Studio
      • Using a Trigger
      • Dispatch Workflow Activity
  • Activities
    • Control Flow
      • Decision
    • MassTransit
      • Tutorial
  • Expressions
    • C#
    • JavaScript
    • Python
    • Liquid
  • Extensibility
    • Custom Activities
  • Reusable Triggers (3.5-preview)
  • Multitenancy
    • Introduction
    • Setup
  • Operate
    • Variables
    • Activation Strategies
    • Incidents
      • Strategies
      • Configuration
    • Alterations
      • Alteration Plans
        • REST API
      • Applying Alterations
        • REST API
        • Extensibility
  • Optimize
    • Log Persistence
    • Retention
  • Hosting
    • Distributed Hosting
Powered by GitBook
On this page
  • Installing Python
  • Windows
  • MacOS
  • Configure Environment Variables
  • Installing the Python Feature
  • Configuration
  • Globals
  • output
  • input
  • variables
  • execution_context
Edit on GitHub
  1. Expressions

Python

PreviousJavaScriptNextLiquid

Last updated 5 months ago

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

  • Visit the .

  • Click on the "Download Windows installer" link for the latest version.

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

  • Navigate to the .

  • Click on the "Download macOS 64-bit installer" link for the latest version.

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 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

official Python downloads page
official Python downloads page
Pythonnet GitHub project
output
input
variables
execution_context