From d396692de571445537e617ac31627d7bd7ab84aa Mon Sep 17 00:00:00 2001 From: Christian Kolset Date: Tue, 9 Jun 2026 22:35:40 -0600 Subject: Fix custom_script execution + add templates for expression/function kinds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - custom_script now uses exec-style (assign to `result`) matching the built-in templates — previously auto-wrapped in def compute() which had no return statement, causing state and channel vars to silently fail - function kind unchanged: auto-wraps body as def compute(x,t); use return - Add SCRIPT_TEMPLATES for expression, function, custom_script kinds; shown automatically when user switches to a script kind (replacing stale code from previous kind) - New channels default to template when no saved code exists Co-Authored-By: Claude Sonnet 4.6 --- ui/code_templates.py | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'ui/code_templates.py') diff --git a/ui/code_templates.py b/ui/code_templates.py index 0863e62..32332e9 100644 --- a/ui/code_templates.py +++ b/ui/code_templates.py @@ -1,18 +1,24 @@ """ ui/code_templates.py -Python code templates for built-in derived channel kinds. -Shown as read-only previews in the UI; clicking "Edit as Custom Script" -converts the channel to custom_script kind with the template pre-filled. +Code templates for derived channel kinds. -Templates use the enhanced expression namespace: - x — list of source values in order - — each source's channel_id as a named variable +BUILTIN_TEMPLATES — shown as read-only previews for built-in kinds. + "Edit as Custom Script" copies the template into custom_script. + Uses exec-style: assign to `result`. + +SCRIPT_TEMPLATES — default starter code shown when user picks a script kind. + expression → single Python expression (no assignment) + function → auto-wrapped as def compute(x, t): ... use return + custom_script → exec-style block, assign to `result` + +Namespace available in all kinds: + x — list of source values in source order + — each source's channel_id as a named variable (same as x[i]) t — elapsed time (seconds) math — Python math module vars — shared SignalProcessor._script_vars dict (read/write) state — per-channel persistent dict (survives between evaluations) - result — assign the computed value here (expression-style exec) """ BUILTIN_TEMPLATES: dict = { @@ -68,3 +74,26 @@ result = x[0] - x[1] result = sum(x) """, } + + +# Default starter code shown when user selects a script kind +SCRIPT_TEMPLATES: dict = { + + "expression": "x[0]", + + "function": """\ +# Function body — use 'return' to output the value. +# Named source vars, t, math, vars, state all available. +offset = vars.get("offset", 0) +return x[0] - offset +""", + + "custom_script": """\ +# Assign computed value to 'result'. +# Named source vars, t, math, vars, state all available. + +# Example: running zero-offset +zero = vars.get("zero", 0) +result = x[0] - zero +""", +} -- cgit v1.2.3