text
Displays a value as a static text.
Note that to create a text control when using the Markdown syntax, you don't need to specify the
control name in the text template:
<|Some content|text|>
<|Some content|>
Properties¶
| Name | Type | Default | Description |
|---|---|---|---|
(★) |
Anydynamic |
"" | The value displayed as text by this control. |
raw |
bool |
False | If set to True, the component renders as an HTML <span> element without any default style. |
mode |
str |
Define the way the text is processed:
|
|
format |
str |
The format to apply to the value. |
|
width |
Union[str, int] |
None | The width of the text element, in CSS units. |
id |
str |
The identifier assigned to the rendered HTML component. |
|
properties |
dict[str, Any] |
A dictionary of additional properties that can be set to the element. |
|
class_name |
strdynamic |
A space-separated list of CSS class names to be applied to the generated HTML element. |
|
hover_text |
strdynamic |
The text that is displayed when the user hovers over the element. |
(★)value is the default property for this visual element.
Details¶
The format property uses a format string like the ones used by the string format() function of Python.
If the value is a date or a datetime, then format can be set to a date/time
formatting string.
Usage¶
Display values¶
The property value can be set to any text that will be rendered.
This value must be a
Formatted string literals
so that if it includes Python expressions (potentially as simple as a variable name), the resulting
text is displayed.
You can represent a variable value as a simple, static text:
Definition
<|Hello {name}!|text|>
<taipy:text>Hello {name}!</taipy:text>
import taipy.gui.builder as tgb
...
tgb.text("Hello {name}!")
Presuming that the Python variable name is set to the value "Taipy", here is how this control displays:
Formatted output¶
If your value is a floating point value, you can use the format property to indicate what the output format should be used.
If we have the following Python definition:
pi = 3.14159265358979
To display this floating point value with three decimal places, we can create the text control as follows:
Definition
<|{pi}|text|format=%.3f|>
<taipy:text format="%.3f">{pi}</taipy:text>
import taipy.gui.builder as tgb
...
tgb.text("{pi}", format="%.3f")
Here is the resulting display:
Markdown formatting¶
You can use the Markdown markup language to add formatting indications to the text you want to
render.
You must set the mode property to "markdown" or "md" to do that.
Note that the Markdown syntax mentions that if a line ends with two white space characters, a line break is issued.
Here is an example of that feature.
Consider the following variable definition:
1 2 3 4 5 6 7 8 9 | |
You cannot see that line 7 ends with two white space characters.
Let's bind this variable to the value property of the control and set mode so that Markdown formatting is issued:
Definition
<|{markdown}|text|mode=markdown|>
<taipy:text mode="markdown">{markdown}</taipy:text>
import taipy.gui.builder as tgb
...
tgb.text("{markdown}", mode="markdown")
The control looks like this on the page:
Note that a line break was forced where we expected it.
Preformatted text¶
You may need to store text that is preformatted (where spaces and line skips are essential). This
is really useful dealing with source code, for example.
Set the mode property to "pre" to indicate such a situation. The content is displayed
in a fixed-width font.
Line skips and space characters are crucial for Python code to be correctly interpreted.
Say you have the following Python variable definition, showing some raw Python source code:
1 2 3 4 5 6 | |
We can use this variable in our text control and set mode to "pre":
Definition
<|{code}|text|mode=pre|>
<taipy:text mode="pre">{code}</taipy:text>
import taipy.gui.builder as tgb
...
tgb.text("{code}", mode="pre")
This is how the control is displayed:
Displaying LaTeχ¶
LaTeχ is a markup language that is great at typesetting documents and excels at rendering
complex math expressions. You can render LaTeχ in a text control by setting the
mode property to "latex".
Taipy uses the MathJax component from the
better-react-mathjax package for the
final rendering.
You can set a variable to some LaTeχ content:
latex = """
$$f'(x) = \\lim_{h \\to 0} \\frac{f(x+h) - f(x)}{h}$$
"""
Note that the double dollar sign ($) indicates that the control creates its own block, breaking the current text line.
Then use that in your control definition:
Definition
<|{latex}|text|mode=latex|>
<taipy:text mode="latex">{latex}</taipy:text>
import taipy.gui.builder as tgb
...
tgb.text("{latex}", mode="latex")
The controls appears like this:
Styling¶
All the text controls are generated with the "taipy-text" CSS class. You can use this class name to select the text controls on your page and apply style.