Skip to main content

A Python library for building dynamic, structured text templates using a declarative, compose-based approach

Project description

TextCompose

PyPI version License Tests Status Release Status

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:

  1. Template: Combines and renders components as a structured text block.
  2. Group: Groups multiple components and joins their output with a separator (sep).
  3. Text: Displays static text.
  4. 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

We welcome contributions to TextCompose. If you have suggestions or improvements, please open an issue or submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textcompose-0.6.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textcompose-0.6.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file textcompose-0.6.0.tar.gz.

File metadata

  • Download URL: textcompose-0.6.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for textcompose-0.6.0.tar.gz
Algorithm Hash digest
SHA256 5e26d93968178ee3f76b97df80a6d752cfec3fb4793c1e3278b9d7bd8bbf0592
MD5 f7c5614546274d8d8f6c5882be30b66e
BLAKE2b-256 9c30cad8c7997ecc66692a0944d85497bdea8e9d360f2010dcae0fcd1722bfc7

See more details on using hashes here.

File details

Details for the file textcompose-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for textcompose-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a25e1842cbd0bb85837bef246023158b78a2dde8140f6195c75854c5d046e97f
MD5 670845fe5459d9cde1cde25049850c17
BLAKE2b-256 291bf8f3dab2ba703051f39e699f83122ddfbdd86e1fcbc9ac7546a3c703cae9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page