TextCompose is a Python library for creating dynamic, structured text templates. Inspired by aiogram-dialog, it provides a flexible and intuitive interface for composing text.
You can install the library in two ways:
If you are using the uv
package manager, you can install it as follows:
uv add textcompose
pip install textcompose
TextCompose
provides the following core components:
Template
: Combines and renders components as a structured text block.Group
: Groups multiple components and joins their output with a separator (sep
).Text
: Displays static text.Format
: Formats strings dynamically using a given context.
All components support the when
parameter for conditional rendering. If when
evaluates to True
, the component is rendered; otherwise, it is skipped.
Below is an example of how to use TextCompose
to create dynamic text templates with nested components and conditional rendering.
from textcompose import Template
from textcompose.container import Group
from textcompose.content import Format, Text
# Create a template using nested components
template = Template(
Group(
Format("Hello, {name}!"),
Format("Your status: {status}."),
Group(
Text("You have new notifications."),
Format("Notification count: {notifications}.", when=lambda ctx: ctx.get("notifications") > 0),
sep=" " # Separator for the nested group
),
sep="\n" # Separator for the main group
)
)
# Context for rendering
context = {
"name": "John",
"status": "Online",
"notifications": 3
}
# Render text
result = template.render(context)
print(result)
Hello, John!
Your status: Online.
You have new notifications. Notification count: 3.
We welcome contributions to TextCompose
. If you have suggestions or improvements, please open an issue or submit a pull request.