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

[🐛 BUG] Issue using lambda when passing state variable to a method #2212

Closed
1 of 7 tasks
arcanaxion opened this issue Nov 5, 2024 · 9 comments · Fixed by #2256
Closed
1 of 7 tasks

[🐛 BUG] Issue using lambda when passing state variable to a method #2212

arcanaxion opened this issue Nov 5, 2024 · 9 comments · Fixed by #2256
Assignees
Labels
Gui: Back-End 💥Malfunction Addresses an identified problem. 🟧 Priority: High Must be addressed as soon

Comments

@arcanaxion
Copy link

arcanaxion commented Nov 5, 2024

What went wrong? 🤔

I'm trying to use a lambda function in tgb.part.render with a role trait filter to hide something based on a user's credentials:

import taipy.gui.builder as tgb
from taipy import Gui
import taipy as tp
from taipy import Config
from taipy.auth import Credentials
from taipy.auth import AnyOf


Config.configure_authentication(protocol="taipy", roles={"Bob": ["admin"]})

is_admin = AnyOf(["admin"], True, False)
credentials = Credentials(user_name="guest", roles=[])


def on_init(state):
    state.credentials = tp.enterprise.gui.login(state, "Bob", "Bob")


with tgb.Page() as main_page:
    tgb.text(lambda credentials: is_admin.get_traits(credentials))
    with tgb.part(render=lambda credentials: is_admin.get_traits(credentials)):
        tgb.text("You are an admin")


if __name__ == "__main__":
    gui = Gui(main_page)
    gui.run(run_browser=False, debug=True, use_reloader=True)

I get the following error:

/home/project/.venv-enterprise-4.0/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py:369: TaipyGuiWarning: Exception raised evaluating __lambda_139622943681712_1_TPMDL_0(credentials):
Traceback (most recent call last):
  File "/home/project/.venv-enterprise-4.0/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py", line 366, in re_evaluate_expr
    expr_evaluated = eval(expr_string, ctx)
  File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not callable

  _warn(f"Exception raised evaluating {expr_string}", e)
/home/project/.venv-enterprise-4.0/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py:369: TaipyGuiWarning: Exception raised evaluating __lambda_139622943681712_2_TPMDL_0(credentials):
Traceback (most recent call last):
  File "/home/project/.venv-enterprise-4.0/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py", line 366, in re_evaluate_expr
    expr_evaluated = eval(expr_string, ctx)
  File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not callable

  _warn(f"Exception raised evaluating {expr_string}", e)

There is nothing shown on the webpage -- the tgb.text before the part block is also not shown.

Alternate community example

This code also does not work -- nothing is displayed on the webpage:

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


rng = np.random.default_rng()
size = 1

with tgb.Page() as main_page:
    tgb.text(lambda size: rng.random(size).sum() > 0)
    with tgb.part(render=lambda size: rng.random(size).sum() > 0):
        tgb.text("Random sum is greater than 0")

if __name__ == "__main__":
    gui = Gui(main_page)
    gui.run(run_browser=False, debug=True, use_reloader=True)

With 4.0, the error is not printed in the console. I guess this is related to #2144.

With develop, I get the following error:

WARNING:root:
 - Cannot evaluate expression 'lambda size: Generator(PCG64) at 0x7FBAB6160C80.random(size).sum() > 0':
Traceback (most recent call last):
  File "/home/project/.venv-git-2024-11-05/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py", line 262, in evaluate_expr
    expr_evaluated = eval(not_encoded_expr if is_edge_case else expr_string, ctx)
  File "<string>", line 1
    lambda size: Generator(PCG64) at 0x7FBAB6160C80.random(size).sum() > 0
                                  ^^
SyntaxError: invalid syntax

 - Cannot evaluate expression '<lambda>(size)':
Traceback (most recent call last):
  File "/home/project/.venv-git-2024-11-05/lib/python3.10/site-packages/taipy/gui/utils/_evaluator.py", line 262, in evaluate_expr
    expr_evaluated = eval(not_encoded_expr if is_edge_case else expr_string, ctx)
  File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Expected Behavior

The lambda expressions should evaluate to true and the block should be rendered.

Browsers

Chrome

OS

Windows, Linux

Version of Taipy

taipy-enterprise==4.0.0; develop

Additional Context

NA

Acceptance Criteria

  • A unit test reproducing the bug is added.
  • Any new code is covered by a unit tested.
  • Check code coverage is at least 90%.
  • The bug reporter validated the fix.
  • Related issue(s) in taipy-doc are created for documentation and Release Notes are updated.

Code of Conduct

  • I have checked the existing issues.
  • I am willing to work on this issue (optional)
@arcanaxion arcanaxion added the 💥Malfunction Addresses an identified problem. label Nov 5, 2024
@FredLL-Avaiga FredLL-Avaiga self-assigned this Nov 13, 2024
@FredLL-Avaiga
Copy link
Member

In your community example, the lambda does not work because rng is part of the state and needs to be passed as an argument.
When it is not, the lambda is re-created on the fly with the value of rng at first render which does not work here

With this code, everything works

import numpy as np

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

rng = np.random.default_rng()
size = 1

with tgb.Page() as main_page:
    tgb.text(lambda size, rng: rng.random(size).sum() > 0)
    with tgb.part(render=lambda size, rng: rng.random(size).sum() > 0):
        tgb.text("Random sum is greater than 0")

if __name__ == "__main__":
    gui = Gui(main_page)
    gui.run(title="2212 [🐛 BUG] Issue using lambda when passing state variable to a method")

@FredLL-Avaiga
Copy link
Member

I guess it would be the same thing for your enterprise example, if you declare is_admin as a parameter of your lambda.
Can you confirm @arcanaxion ?

I would agree that a better error message would be good

@arcanaxion
Copy link
Author

I can confirm that adding is_admin as a parameter of my lambda works. Though it is a shame that I lose intellisense for is_admin too.

I noticed that I could have created a variable like rngrandom = rng.random and used that and it would have worked too. Are there any unexpected consequences of using this workaround to preserve intellisense?

@FredLL-Avaiga
Copy link
Member

FredLL-Avaiga commented Nov 14, 2024

I don't get it about rngrandom, can you copy your code ?
If it works and makes you happy, I'm fine with it :-D

@arcanaxion
Copy link
Author

@FredLL-Avaiga

Following (ugly) workaround works:

import numpy as np

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

rng = np.random.default_rng()
size = 1

with tgb.Page() as main_page:
    rngrandom = rng.random
    tgb.text(lambda size: rngrandom(size).sum())

if __name__ == "__main__":
    gui = Gui(main_page)
    gui.run()

It is ugly and I don't like it, but it gives intellisense for the rngrandom method. Compared to if I add rng as a lambda parameter, I lose intellisense and also have to rename it separately if I automatically (IDE) refactor/rename rng.

You mentioned "re-created on the fly with the value of rng at first render", but I don't really know what that means. I'm wondering if there may be unexpected behavior when doing this, as opposed to adding rng as a lambda parameter like you suggested

@FredLL-Avaiga
Copy link
Member

no unexpected behavior that I can see/imagine

@FredLL-Avaiga
Copy link
Member

Shall we close this @arcanaxion ?

@arcanaxion
Copy link
Author

@FredLL-Avaiga I would still like to see a better error message, if it makes sense to do so. I wouldn't have figured out what the problem was from the current error message

@FredLL-Avaiga
Copy link
Member

fixed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gui: Back-End 💥Malfunction Addresses an identified problem. 🟧 Priority: High Must be addressed as soon
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants