Power Fx Python Bridge

0.0.34 · active · verified Thu Apr 16

The `powerfx` Python library (current version 0.0.34) acts as a bridge to invoke the C# implementation of Microsoft Power Fx, a low-code, general-purpose programming language. It enables Python applications to evaluate Power Fx expressions. This library is actively maintained by Microsoft and sees releases periodically, often tied to updates within the broader Power Platform ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `powerfx.Engine`, register Python objects as host objects, and evaluate Power Fx expressions. It includes an example of string concatenation and a conditional statement. Note the crucial importance of a correctly installed and discoverable .NET runtime for `powerfx` to function.

import os
from powerfx import Engine

# Ensure DOTNET_ROOT is set if .NET runtime is not in standard paths
# Example: os.environ['DOTNET_ROOT'] = '/usr/local/share/dotnet'

# Initialize the Power Fx engine
engine = Engine()

# Define variables that can be used in Power Fx expressions
engine.add_host_object('Name', 'Alice')
engine.add_host_object('Age', 30)

# Evaluate a simple Power Fx expression
expression = '"Hello " & Name & ", you are " & Text(Age) & " years old!"'
result = engine.eval(expression)

print(f"Power Fx Expression: {expression}")
print(f"Result: {result.to_string()}")

# Example with a conditional expression
conditional_expression = 'If(Age >= 18, "Adult", "Minor")'
conditional_result = engine.eval(conditional_expression)

print(f"\nPower Fx Conditional Expression: {conditional_expression}")
print(f"Conditional Result: {conditional_result.to_string()}")

view raw JSON →