# Youseful Software - FileMaker sample script
# Script name: Youseful - Python totals
# Plug-in: YsflPython.fmx64 (Windows x64)
# Setup: Install Windows CPython. If discovery fails, add a separate python=<full path to python.exe> option line before param=. No fields needed. JSON is passed through fm.param.
# Expected: {"count":2,"total":37} (JSON spacing and key order may differ).
# Format: UTF-8 Script Workspace step listing. Add the steps manually; this is not an importable FileMaker file.
# Lines beginning with # are notes; optionally add them as Comment steps.
# Preserve calculation quotes, backslashes, and paragraph symbols exactly.

Set Variable [ $rows ; Value: "[{\"qty\":3,\"price\":10},{\"qty\":1,\"price\":7}]" ]
Set Variable [ $code ; Value:
    "import json¶" &
    "rows = json.loads(fm.param)¶" &
    "{'count': len(rows), 'total': sum(row['qty'] * row['price'] for row in rows)}"
]
Set Variable [ $json ; Value: YsflPy_Run ( $code ; "mode=process timeout=15¶param=" & $rows ) ]
Set Variable [ $error ; Value: YsflPy_LastError ]
If [ $error <> 0 ]
    Set Variable [ $detail ; Value: YsflPy_Error ]
    Show Custom Dialog [ "Python failed" ; $detail ]
    Exit Script [ Text Result: $json ]
End If
Show Custom Dialog [ "Summary" ; JSONFormatElements ( $json ) ]
