Skip to content

LOOP_INDEX

Load the loop index from the LOOP node. A loop index in Flojoy starts at 1 and increases by 1 for each loop. Params: loop_node : str The LOOP node to track the loop index from. Returns: out : Scalar The loop index in Scalar form.
Python Code
from flojoy import (
    flojoy,
    Scalar,
    OrderedPair,
    SmallMemory,
    NodeReference,
)
from typing import Optional


memory_key = "LOOP_INDEX"


@flojoy
def LOOP_INDEX(
    loop_node: NodeReference,
    default: Optional[OrderedPair | Scalar] = None,
) -> Scalar:
    """Load the loop index from the LOOP node.

    A loop index in Flojoy starts at 1 and increases by 1 for each loop.

    Parameters
    ----------
    loop_node : str
        The LOOP node to track the loop index from.

    Returns
    -------
    Scalar
        The loop index in Scalar form.
    """

    ref_loop_node = loop_node.unwrap()

    if ref_loop_node == "" or "LOOP" not in ref_loop_node:
        raise ValueError("A LOOP node id must be given.")

    loop_info = SmallMemory().read_memory(ref_loop_node, "loop-info")
    if loop_info is None:
        c = 1
    else:
        c = loop_info.get("current_iteration")

    return Scalar(c=float(c))

Find this Flojoy Block on GitHub

Example

Having problems with this example app? Join our Discord community and we will help you out!
React Flow mini map

This example shows a simple way to create a loop with Flojoy. First, you’ll need to place these three nodes:

  • The LOOP node which will define the number of loops.

  • The LOOP_INDEX node tracks the loop index (the number of loops that has occured). The index starts at 1 in Flojoy.

  • The BIG_NUMBER node which is connected to the “end” output of the [LOOP] node, which serve, to terminate the program.

Then click on [LOOP] and change the number of loops to 100 (the default is -1 which causes the loop to repeat indefinitely). Click on [LOOP_INDEX] and change the referred node to [LOOP].

You can then run the app and watch the loop index increase to 100 as the loop continues.