A Python library for building dynamic, structured text templates using a declarative, compose-based approach
Project description
TextCompose
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.
Installation
You can install the library in two ways:
Using uv
If you are using the uv package manager, you can install it as follows:
uv add textcompose
Using pip
pip install textcompose
Usage
Components Overview
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.
Example
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)
Output:
Hello, John!
Your status: Online.
You have new notifications. Notification count: 3.
Contributing
For more detailed documentation, check the dev branch.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file textcompose-0.3.0.tar.gz.
File metadata
- Download URL: textcompose-0.3.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba2066da05189efbbd046e29b59f70fc733b35b979982e4fbefe635b02945799
|
|
| MD5 |
f1b7ecc0a63932a1587a42b2585c379c
|
|
| BLAKE2b-256 |
4fee07cba030db3b3008af277f9c405821b53cde908a3794d0ff80084b8f78d7
|
File details
Details for the file textcompose-0.3.0-py3-none-any.whl.
File metadata
- Download URL: textcompose-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11fc1baab2218263649131b2f2a2f746f84f73789ab85fbc5aa300782fdc3e30
|
|
| MD5 |
90d196d695fbcff1a0a7b1427f5a879c
|
|
| BLAKE2b-256 |
2d3b87dbe3404830b959c566d2f22b6273d0ae29855bd82f7629d4a796b29420
|