Open arcanaxion opened 1 week ago
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")
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
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?
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
@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
no unexpected behavior that I can see/imagine
Shall we close this @arcanaxion ?
@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
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:
I get the following error:
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:
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:
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
Acceptance Criteria
Code of Conduct