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

[OTHER] Bind to dictionary key within class #2395

Open
1 of 2 tasks
JosuaCarl opened this issue Jan 13, 2025 · 1 comment
Open
1 of 2 tasks

[OTHER] Bind to dictionary key within class #2395

JosuaCarl opened this issue Jan 13, 2025 · 1 comment
Labels
🖰 GUI Related to GUI 📈 Improvement Improvement of a feature. 🟨 Priority: Medium Not blocking but should be addressed

Comments

@JosuaCarl
Copy link
Contributor

What would you like to share or ask?

I am failing to write info to a value that is bound to an entry in a dictionary, which is nested in a class. I saw an example of using a dictionary in #1785 , but am failing, when the dictionary is in a class, other than global.

Minimal example:

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

class Simple_Class:
    def __init__(self):
        x = 1

class Complex_Class:
    def __init__(self):
        self.dictionary = {}
        self.simple_class = Simple_Class()

complex_class = Complex_Class()
simple_dictionary = {}

def printer(state, var_name, value):
    print(state.complex_class.simple_class.x)
    print(state.complex_class.dictionary.items())
    print(state.simple_dictionary.items())

with tgb.Page() as page:
    tgb.input(
        value="{complex_class.dictionary.x}",
        on_change=printer,
    )
    tgb.input(
        value="{complex_class.simple_class.x}",
        on_change=printer,
    )
    tgb.input(
        value="{simple_dictionary.x}",
        on_change=printer,
    )

Gui(page).run(port=4999)

The following output is produced, when entering 1,2 and 3 into the resulting fields.

.../.venv/lib/python3.12/site-packages/taipy/gui/gui.py:700: TaipyGuiWarning: A problem occurred while resolving variable 'complex_class.dictionary.x' in module 'main'.
_warn(
.../.venv/lib/python3.12/site-packages/taipy/gui/utils/_evaluator.py:371: TaipyGuiWarning: Exception raised evaluating complex_class.dictionary.x:
'dict' object has no attribute 'x'
_warn(f"Exception raised evaluating {expr_string}", e)
2
dict_items([])
dict_items([])
2
dict_items([])
dict_items([('x', '3')])

As demonstrated, even deep nested properties are able to be used, so it seems possible in principle, but evaluated differently in praxis.

Do you know of a workaround and could this be covered in future releases?

Code of Conduct

  • I have checked the existing issues.
  • I am willing to work on this issue (optional)
@FlorianJacta
Copy link
Member

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


class Simple_Class:
    def __init__(self):
        self.x = 1


class Complex_Class:
    def __init__(self):
        self.dictionary = {"x": 1}
        self.simple_class = Simple_Class()


complex_class = Complex_Class()
simple_dictionary = {"x": 1}


def printer(state, var_name, value):
    print(state.complex_class.simple_class.x)
    print(state.complex_class.dictionary.items())
    print(state.simple_dictionary.items())


with tgb.Page() as page:
    tgb.input(
        value="{complex_class.dictionary.x}",
        on_change=printer,
    )
    tgb.input(
        value="{complex_class.simple_class.x}",
        on_change=printer,
    )
    tgb.input(
        value="{simple_dictionary.x}",
        on_change=printer,
    )

Gui(page).run(port=4999)

I have used this code and had the same issue. This is the error I get when updating complex_class.dictionary.x:

C:\Users\jacta\OneDrive\Bureau\Go\.venv\Lib\site-packages\taipy\gui\utils\_evaluator.py:371: TaipyGuiWarning: Exception raised evaluating complex_class.dictionary.x:
'dict' object has no attribute 'x'
  _warn(f"Exception raised evaluating {expr_string}", e)

@FlorianJacta FlorianJacta added 📈 Improvement Improvement of a feature. 🖰 GUI Related to GUI 🟨 Priority: Medium Not blocking but should be addressed labels Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖰 GUI Related to GUI 📈 Improvement Improvement of a feature. 🟨 Priority: Medium Not blocking but should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants