test_variable_binding.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. from taipy.gui import Gui, Markdown
  12. def test_variable_binding(helpers):
  13. """
  14. Tests the binding of a few variables and a function
  15. """
  16. def another_function(gui):
  17. pass
  18. x = 10
  19. y = 20
  20. z = "button label"
  21. gui = Gui()
  22. gui.add_page("test", Markdown("<|{x}|> | <|{y}|> | <|{z}|button|on_action=another_function|>"))
  23. gui.run(run_server=False, single_client=True)
  24. client = gui._server.test_client()
  25. jsx = client.get("/taipy-jsx/test").json["jsx"]
  26. for expected in ["<Button", 'defaultLabel="button label"', "label={tpec_TpExPr_z_TPMDL_0}"]:
  27. assert expected in jsx
  28. assert gui._bindings().x == x
  29. assert gui._bindings().y == y
  30. assert gui._bindings().z == z
  31. with gui.get_server_instance().app_context():
  32. assert callable(gui._get_user_function("another_function"))
  33. helpers.test_cleanup()
  34. def test_properties_binding(helpers):
  35. gui = Gui()
  36. modifier = "nice " # noqa: F841
  37. button_properties = {"label": "A {modifier}button"} # noqa: F841
  38. gui.add_page("test", Markdown("<|button|properties=button_properties|>"))
  39. gui.run(run_server=False)
  40. client = gui._server.test_client()
  41. jsx = client.get("/taipy-jsx/test").json["jsx"]
  42. for expected in ["<Button", 'defaultLabel="A nice button"']:
  43. assert expected in jsx
  44. helpers.test_cleanup()
  45. def test_dict_binding(helpers):
  46. """
  47. Tests the binding of a dictionary property
  48. """
  49. d = {"k": "test"} # noqa: F841
  50. gui = Gui("<|{d.k}|>")
  51. gui.run(run_server=False)
  52. client = gui._server.test_client()
  53. jsx = client.get("/taipy-jsx/TaiPy_root_page").json["jsx"]
  54. for expected in ["<Field", 'defaultValue="test"']:
  55. assert expected in jsx
  56. helpers.test_cleanup()