Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to bind indexed variables #1785

Closed
1 of 6 tasks
AlexandreSajus opened this issue Sep 13, 2024 · 1 comment · Fixed by #1824
Closed
1 of 6 tasks

How to bind indexed variables #1785

AlexandreSajus opened this issue Sep 13, 2024 · 1 comment · Fixed by #1824
Assignees
Labels
💬 Discussion Requires some discussion and decision 🖰 GUI Related to GUI ✨New feature 🟨 Priority: Medium Not blocking but should be addressed ❓ Question Can be unblocked with an answer 🔒 Staff only Can only be assigned to the Taipy R&D team

Comments

@AlexandreSajus
Copy link
Contributor

Description

Check out this app where I generate selectors dynamically:
image

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

questions = ["Are you alive ?", "aRe yOU sUre ?"]
answers = [""] * len(questions)


def selector(state):
    print(state.answers)


with tgb.Page() as page:
    for i, question in enumerate(questions):
        tgb.text(f"#### {question}", mode="md")
        tgb.selector(
            value="{answers[" + str(i) + "]}",
            lov=["Yes", "No"],
            on_change=selector,
        )


Gui(page).run()

How do I store the answers (the values selected)? Here, I try to store the answers in the answers list, but instead of assigning the selected value to answers[0], it assigns it to answers, so I lose the other answers.

Solution Proposed

There are multiple solutions but one should be implemented and documented:

  • We could let the user bind a selector to an element of a list like I tried to do above
  • We could let the user not bind the selector and define a var_name property. This way the user could store answers at the on_change(state, var_name, value) level

Currently, the workaround is using exec to create variables on the fly and it is not elegant:

def selector(state, var_name, value):
    index = int(var_name[10:])
    state.answers[index] = value
    state.answers = state.answers
    print(state.answers)


with tgb.Page() as page:
    for i, question in enumerate(questions):
        tgb.text(f"#### {question}", mode="md")
        exec(f"selection_{i} = ''")
        tgb.selector(
            value="{selection_" + str(i) + "}",
            lov=["Yes", "No"],
            on_change=selector,
        )

Impact of Solution

No response

Additional Context

No response

Acceptance Criteria

  • Ensure new code is unit tested, and check code coverage is at least 90%.
  • Create related issue in taipy-doc for documentation and Release Notes.
  • Check if a new demo could be provided based on this, or if legacy demos could be benefit from it.
  • Ensure any change is well documented.

Code of Conduct

  • I have checked the existing issues.
  • I am willing to work on this issue (optional)
@AlexandreSajus AlexandreSajus added 🖰 GUI Related to GUI ❓ Question Can be unblocked with an answer 🟨 Priority: Medium Not blocking but should be addressed ✨New feature labels Sep 13, 2024
@jrobinAV jrobinAV added the 💬 Discussion Requires some discussion and decision label Sep 13, 2024
@FabienLelaquais FabienLelaquais changed the title How to bind variables to selectors generated dynamically How to bind indexed variables Sep 13, 2024
@jrobinAV jrobinAV added the 🔒 Staff only Can only be assigned to the Taipy R&D team label Sep 13, 2024
@AlexandreSajus
Copy link
Contributor Author

Florian explained to me that binding to dictionaries does work, but this approach is not documented as far as I have seen:

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

questions = ["Hello", "dqdqd"]
answers = {}

for i, question in enumerate(questions):
    answers[i] = ""


def selector(state, var_name, value):
    print(state.answers.Hello)


with tgb.Page() as page:
    for i, question in enumerate(questions):
        tgb.text(f"#### {question}", mode="md")
        tgb.selector(
            value="{answers." + question + "}",
            lov=["Yes", "No"],
            on_change=selector,
        )


Gui(page).run()

@FredLL-Avaiga FredLL-Avaiga self-assigned this Sep 23, 2024
FredLL-Avaiga pushed a commit that referenced this issue Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💬 Discussion Requires some discussion and decision 🖰 GUI Related to GUI ✨New feature 🟨 Priority: Medium Not blocking but should be addressed ❓ Question Can be unblocked with an answer 🔒 Staff only Can only be assigned to the Taipy R&D team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants