Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Doc for html() builder class (#988)
Browse files Browse the repository at this point in the history
- Add doc to builder's html element
- Simplify tests (Gui without run())
  • Loading branch information
FabienLelaquais authored Oct 21, 2023
1 parent cd1ed66 commit 75274cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
45 changes: 40 additions & 5 deletions src/taipy/gui/builder/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,59 @@ def __init__(self, *args, **kwargs):
class html(_Block):
"""A visual element defined as an HTML tag.
Use this class to integrate raw HTML to your page.
This element can be used as a block element.
"""

def __init__(self, *args, **kwargs):
"""Create a new `html` block.
TODO
Arguments:
args (any[]): A list of one or two unnamed arguments:
- *args[0]* is the HTML tag name. If empty or None, this represents an HTML text
node.
- *args[1]* (optional) is the text of this element.<br/>
Note that special HTML characters (such as '&lt;' or '&amp;') do not need to be protected.
kwargs (dict[str, any]): the HTML attributes for this element.<br/>
These should be valid attribute names, with valid attribute values.
Examples:
- To generate `<br/>`, use:
```
html("br")
```
- To generate `<h1>My page title</h1>`, use:
```
html("h1", "My page title")
```
- To generate `<h1 id="page-title">My page title</h1>`, use:
```
html("h1", "My page title", id="page-title")
```
- To generate `<p>This is a <b>Taipy GUI</b> element.</p>`, use:
```
with html("p"):
html(None, "This is a ")
html("b", "Taipy GUI")
html(None, " element.")
```
"""
super().__init__(*args, **kwargs)
if not args:
raise RuntimeError("Can't render html element. Missing html tag name.")
self._ELEMENT_NAME = args[0]
self._ELEMENT_NAME = args[0] if args[0] else None
self._content = args[1] if len(args) > 1 else ""

def _render(self, gui: "Gui") -> str:
open_tag_attributes = " ".join([f'{k}="{str(v)}"' for k, v in self._properties.items()])
open_tag = f"<{self._ELEMENT_NAME} {open_tag_attributes}>"
return f"{open_tag}{self._content}{self._render_children(gui)}</{self._ELEMENT_NAME}>"
if self._ELEMENT_NAME:
attrs = ""
if self._properties:
attrs = " " + " ".join([f'{k}="{str(v)}"' for k, v in self._properties.items()])
return f"<{self._ELEMENT_NAME}{attrs}>{self._content}{self._render_children(gui)}</{self._ELEMENT_NAME}>"
else:
return self._content


class _Control(_Element):
Expand Down
2 changes: 2 additions & 0 deletions src/taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,8 @@ def _evaluate_holders(self, expr: str) -> t.List[str]:
return self.__evaluator.evaluate_holders(self, expr)

def _is_expression(self, expr: str) -> bool:
if self.__evaluator is None:
return False
return self.__evaluator._is_expression(expr)

# make components resettable
Expand Down
4 changes: 2 additions & 2 deletions tests/taipy/gui/builder/control/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_html_builder(gui: Gui, test_client, helpers):
'<h1 style="color:Tomato;">This is a header',
'<p style="color:green;">This is a paragraph.',
'<a href="https://www.w3schools.com" target="_blank">a text',
"<br >",
"<b >This is bold text inside the paragrah.",
"<br>",
"<b>This is bold text inside the paragrah.",
]
helpers.test_control_builder(gui, page, expected_list)

0 comments on commit 75274cd

Please sign in to comment.