page.py 3.2 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 typing as t
  12. from .._renderers import _Renderer
  13. from ._context_manager import _BuilderContextManager
  14. from ._element import _Block, _DefaultBlock, _Element
  15. class Page(_Renderer):
  16. """Page generator for the Builder API.
  17. This class is used to create a page created with the Builder API.<br/>
  18. Instances of this class can be added to the application using `Gui.add_page()^`.
  19. This class is typically be used as a Python Context Manager to add the elements.<br/>
  20. Here is how you can create a single-page application, creating the elements with code:
  21. ```python
  22. from taipy.gui import Gui
  23. from taipy.gui.builder import Page, button
  24. def do_something(state):
  25. pass
  26. if __name__ == "__main__":
  27. with Page() as page:
  28. button(label="Press me", on_action=do_something)
  29. Gui(page).run()
  30. ```
  31. """
  32. def __init__(self, element: t.Optional[_Element] = None, **kwargs) -> None:
  33. """Initialize a new page.
  34. Arguments:
  35. element (*Element*): An optional element, defined in the `taipy.gui.builder^` module,
  36. that is created in the page.<br/>
  37. The default creates a `part` where several elements can be stored.
  38. The `Page` constructor supports the *style* parameter as explained in the
  39. [section on Styling](../../../../../../userman/gui/styling/index.md#style-sheets) and in the
  40. `(taipy.gui.Page.)set_style()^` method.
  41. """
  42. if element is None:
  43. element = _DefaultBlock()
  44. kwargs["content"] = element
  45. super().__init__(**kwargs)
  46. # Generate JSX from Element Object
  47. def render(self, gui) -> str:
  48. if self._base_element is None:
  49. return "<h1>No Base Element found for Page</h1>"
  50. return self._base_element._render(gui)
  51. def add(self, *elements: _Element):
  52. if not isinstance(self._base_element, _Block):
  53. raise RuntimeError("Can't add child element to non-block element")
  54. for element in elements:
  55. if element not in self._base_element._children:
  56. self._base_element._children.append(element)
  57. return self
  58. def __enter__(self):
  59. if self._base_element is None:
  60. raise RuntimeError("Can't use context manager with missing block element for Page")
  61. if not isinstance(self._base_element, _Block):
  62. raise RuntimeError("Can't add child element to non-block element")
  63. _BuilderContextManager().push(self._base_element)
  64. return self
  65. def __exit__(self, exc_type, exc_value, traceback):
  66. _BuilderContextManager().pop()