test_invoke_callback.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2021-2024 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 flask import g
  14. from taipy.gui import Gui, Markdown, State
  15. @contextlib.contextmanager
  16. def get_state(gui: Gui, state_id: str):
  17. with gui.get_flask_app().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. name = "World!" # noqa: F841
  23. btn_id = "button1" # noqa: F841
  24. val = 1 # noqa: F841
  25. def user_callback(state: State):
  26. state.val = 10
  27. # set gui frame
  28. gui._set_frame(inspect.currentframe())
  29. gui.add_page("test", Markdown("<|Hello {name}|button|id={btn_id}|>\n<|{val}|>"))
  30. gui.run(run_server=False)
  31. flask_client = gui._server.test_client()
  32. # client id
  33. cid = helpers.create_scope_and_get_sid(gui)
  34. # Get the jsx once so that the page will be evaluated -> variable will be registered
  35. flask_client.get(f"/taipy-jsx/test?client_id={cid}")
  36. gui.invoke_callback(cid, user_callback, [])
  37. with get_state(gui, cid) as state:
  38. assert state.val == 10
  39. def test_invoke_callback_sid(gui: Gui, helpers):
  40. name = "World!" # noqa: F841
  41. btn_id = "button1" # noqa: F841
  42. val = 1 # noqa: F841
  43. def user_callback(state: State):
  44. state.val = 10
  45. # set gui frame
  46. gui._set_frame(inspect.currentframe())
  47. gui.add_page("test", Markdown("<|Hello {name}|button|id={btn_id}|>\n<|{val}|>"))
  48. gui.run(run_server=False)
  49. flask_client = gui._server.test_client()
  50. # client id
  51. cid = helpers.create_scope_and_get_sid(gui)
  52. base_sid, _ = gui._bindings()._get_or_create_scope("base sid")
  53. # Get the jsx once so that the page will be evaluated -> variable will be registered
  54. flask_client.get(f"/taipy-jsx/test?client_id={cid}")
  55. with gui.get_flask_app().app_context():
  56. g.client_id = base_sid
  57. gui.invoke_callback(cid, user_callback, [])
  58. assert g.client_id == base_sid
  59. with get_state(gui, base_sid) as base_state:
  60. assert base_state.val == 1
  61. with get_state(gui, cid) as a_state:
  62. assert a_state.val == 10