test_invoke_callback.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import contextlib
  12. import inspect
  13. from taipy.gui import Gui, Markdown, State
  14. from taipy.gui.servers import get_request_meta
  15. @contextlib.contextmanager
  16. def get_state(gui: Gui, state_id: str):
  17. with gui.get_app_context():
  18. client_id = gui._bindings()._get_or_create_scope(state_id)[0]
  19. gui._Gui__set_client_id_in_context(client_id) # type: ignore[attr-defined]
  20. yield gui._Gui__state # type: ignore[attr-defined]
  21. def test_invoke_callback(gui: Gui, helpers):
  22. val = 1 # noqa: F841
  23. def user_callback(state: State):
  24. state.val = 10
  25. # set gui frame
  26. gui._set_frame(inspect.currentframe())
  27. gui.add_page("test", Markdown("<|Hello|button|>\n<|{val}|>"))
  28. gui.run(run_server=False)
  29. server_client = gui._server.test_client()
  30. # client id
  31. cid = helpers.create_scope_and_get_sid(gui)
  32. # Get the jsx once so that the page will be evaluated -> variable will be registered
  33. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  34. gui.invoke_callback(cid, user_callback, [])
  35. with get_state(gui, cid) as state:
  36. assert state.val == 10
  37. def test_invoke_callback_sid(gui: Gui, helpers):
  38. val = 1 # noqa: F841
  39. def user_callback(state: State):
  40. state.val = 10
  41. # set gui frame
  42. gui._set_frame(inspect.currentframe())
  43. gui.add_page("test", Markdown("<|Hello|button|>\n<|{val}|>"))
  44. gui.run(run_server=False)
  45. server_client = gui._server.test_client()
  46. # client id
  47. cid = helpers.create_scope_and_get_sid(gui)
  48. base_sid, _ = gui._bindings()._get_or_create_scope("base sid")
  49. # Get the jsx once so that the page will be evaluated -> variable will be registered
  50. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  51. with gui.get_app_context():
  52. get_request_meta().client_id = base_sid
  53. assert get_request_meta().client_id == base_sid
  54. gui.invoke_callback(cid, user_callback, [])
  55. assert get_request_meta().client_id == base_sid
  56. with get_state(gui, base_sid) as base_state:
  57. assert base_state.val == 1
  58. with get_state(gui, cid) as a_state:
  59. assert a_state.val == 10