Avaiga / taipy

Turns Data and AI algorithms into production-ready web applications in no time.
https://www.taipy.io
Apache License 2.0
10.94k stars 775 forks source link

[🐛 BUG] `on_change` of selector not working #1422

Closed failable closed 2 months ago

failable commented 2 months ago

What went wrong? 🤔

The on_change function of selector is not working.

Expected Behavior

The on_change function of selector should work.

Steps to Reproduce Issue

Test code:

import taipy.gui.builder as tgb
from taipy.gui import Gui

def on_change(state, var, val):
    print(state)
    print(var)
    print(val)

if __name__ == "__main__":
    collection_names = ["collection1", "collection2", "collection3"]
    with tgb.Page() as page:
        tgb.selector(
            label="Collection",
            lov=";".join(collection_names),
            dropdown=True,
            on_change=on_change,
        )

    Gui(page=page).run(
        dark_mode=False,
        debug=True,
        use_reloader=True,
        port=5001,
    )

Solution Proposed

No response

Screenshots

No response

Runtime Environment

macOS 14.5 (23F79), Chrome Version 125.0.6422.142 (Official Build) (x86_64)

Browsers

Chrome

OS

Mac

Version of Taipy

taipy==3.1.1 taipy-config==3.1.1 taipy-core==3.1.1 taipy-gui==3.1.2 taipy-rest==3.1.1 taipy-templates==3.1.1

Additional Context

No response

Acceptance Criteria

Code of Conduct

FlorianJacta commented 2 months ago

Hi, your selector is not bound to any variable. This behavior is normal.

I have bound your selector to a variable called selected_collection. Try this code and tell me if this works:

import taipy.gui.builder as tgb
from taipy.gui import Gui

def on_change(state, var, val):
    print(var)
    print(val)

if __name__ == "__main__":
    collection_names = ["collection1", "collection2", "collection"]
    selected_collection = None
    with tgb.Page() as page:
        tgb.selector(
            value="{selected_collection}",
            label="Collection",
            lov=collection_names,
            dropdown=True,
            on_change=on_change,
        )

    Gui(page=page).run(
        dark_mode=False,
        debug=True,
        use_reloader=True,
        port=5001,
    )
failable commented 2 months ago

@FlorianJacta Thanks, that works!

FlorianJacta commented 2 months ago

Great to hear! You can now access your real-time variable selected_collection in any callback.