GitHub user aglinxinyuan edited a discussion: Use a loop's variables in any operator inside the loop: type $K in a property
> **In one sentence:** inside a Loop Start ... Loop End block, type `$i` into a > property of any operator, and every iteration runs that operator with the > loop's current `i`. **Pull requests** (reviewed and merged separately): | Part | Pull request | Issue | |---|---|---| | Engine: binds `$i` in operator properties | [#8639 feat(amber): bind $i loop-variable references in operators](https://github.com/apache/texera/pull/8639) | #8635 | | Property panel: accepts and suggests `$i` | [#8640 feat(frontend): accept $variable references in loop blocks](https://github.com/apache/texera/pull/8640) | #8636 | | Python operators: `self.loop_state` in UDFs | [#8642 feat(pyamber): register loop_state before process_state](https://github.com/apache/texera/pull/8642) | #8641 | ## How it works ```mermaid flowchart LR R[Reviews] --> LS["Loop Start<br/>Initialization: min_stars = 1<br/>Output: table"] LS --> F["Filter<br/>stars >= $min_stars"] F --> LE["Loop End<br/>Update: min_stars += 1<br/>Condition: min_stars <= 5"] ``` Declare the variable in Loop Start, type `$min_stars` into a property of any operator in the loop, and run. Each iteration uses its own value, and the operator's code does not change. ## Screenshots | Outside a loop (as today) | Inside a loop | Undeclared name | |---|---|---| |  |  |  | A run of `Text Input (1..6) -> Loop Start (i = 0) -> Limit ($i) -> Loop End`: iteration `i` lets `i` rows through, so Loop End collects 0 + 1 + ... + 5 = 15 rows.  ## In a Python UDF ```python class ProcessTupleOperator(UDFOperatorV2): def process_state(self, state, port): self.min_stars = state["min_stars"] # the loop's variables arrive here return state def process_tuple(self, tuple_, port): if tuple_["stars"] >= self.min_stars: yield tuple_ ``` ## Not supported yet Python-generated operators (sklearn, charts), operators that also take input from outside the loop, properties that name a column, a literal `$...` text inside a loop, operators in an inner loop of nested loops, and the Form View. ## Feedback wanted 1. Is `$name` the right syntax, and how should a literal `$` be written inside a loop? 2. Inside a loop, true/false fields become text boxes. Would a checkbox with a "use a loop variable" toggle be better? 3. Which operators should come next? The sklearn K-means, with `$i` as its number of clusters, is the obvious one. GitHub link: https://github.com/apache/texera/discussions/8643 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected]
