krish-adi / barfi

Python Flow Based Programming environment that provides a graphical programming environment.
https://barfi.readthedocs.io
MIT License
659 stars 71 forks source link

How do I dynamically change the item type in add_option type=“select”? #38

Open hodakamori opened 5 days ago

hodakamori commented 5 days ago

I think block.add_option can set the type of element to select as follows. How can I receive this item candidate from node's input?

I would like to create a node that receives a data frame and selects a specific column within that data frame, as in the code example below. However, after the items are first rendered, they seem to remain the same regardless of the node's connection state.

def xy_selecter() -> Block:
    block = Block(name="XY Selecter")

    block.add_input(name="In(df)")
    block.add_output(name="Out(X, df)")
    block.add_output(name="Out(y, df)")

    df = block.get_interface(name="In(df)")
    if df is None:
        items = ["select y"]
    else:
        items = df.columns
    block.add_option(
        name="select y",
        type="select",
        items=items,
    )

    def compute_func(self: Any) -> None:
        df = self.get_interface(name="In(df)")
        y_col = self.get_interface(name="select y")
        y = df[y_col]
        X = df[df.columns != y_col]
        self.set_interface(name="Out(X, df)", value=X)
        self.set_interface(name="Out(y, df)", value=y)

    block.add_compute(compute_func)

    return block