Skip to content

Commit

Permalink
on_action params grouped (#2047)
Browse files Browse the repository at this point in the history
* on_action params grouped
resolves #2045

* with tests

---------

Co-authored-by: Fred Lefévère-Laoide <[email protected]>
  • Loading branch information
FredLL-Avaiga and Fred Lefévère-Laoide authored Oct 14, 2024
1 parent b0fa510 commit 48c86e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ def __on_action(self, id: t.Optional[str], payload: t.Any) -> None:
return
else: # pragma: no cover
_warn(f"on_action(): '{action}' is not a valid function.")
if hasattr(self, "on_action"):
if getattr(self, "on_action", None) is not None:
self.__call_function_with_args(action_function=self.on_action, id=id, payload=payload)

def __call_function_with_args(self, **kwargs):
Expand All @@ -1520,7 +1520,7 @@ def __call_function_with_args(self, **kwargs):
args = [self._get_real_var_name(id)[0], payload]
except Exception:
args = [id, payload]
self._call_function_with_state(t.cast(t.Callable, action_function), [args])
self._call_function_with_state(t.cast(t.Callable, action_function), args)
return True
except Exception as e: # pragma: no cover
if not self._call_on_exception(action_function, e):
Expand Down
21 changes: 21 additions & 0 deletions tests/gui/gui_specific/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,24 @@ def test__get_valid_adapter_result(gui: Gui):
res = gui._get_valid_adapter_result(("id", "label"))
assert isinstance(res, tuple)
assert res[0] == "id"

def test_on_action_call(gui:Gui):
an_id = "my_id"

a_non_action_payload = {"a": "b"}
def on_action(state, id, payload):
assert id == an_id
assert payload is a_non_action_payload

an_action_payload = {"action": "on_an_action"}
def on_an_action(state, id, payload):
assert id == an_id
assert payload is an_action_payload

# set gui frame
gui._set_frame(inspect.currentframe())

gui.run(run_server=False)
with gui.get_flask_app().app_context():
gui._Gui__on_action(an_id, a_non_action_payload) # type: ignore[reportAttributeAccessIssue]
gui._Gui__on_action(an_id, an_action_payload) # type: ignore[reportAttributeAccessIssue]

0 comments on commit 48c86e9

Please sign in to comment.