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 Ask DeepWiki

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.


✨ Features

  • Flexible text composition from components
  • Conditional rendering support (when)
  • Grouping and repeating blocks
  • Formatting via f-string and Jinja2
  • Easily extensible with new components

🚀 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

General

  • Template — main class for combining and rendering components

Elements

Element — abstract base class for all element components

  • Text — outputs static text
  • Format — dynamic formatting via f-string
  • Jinja — rendering via Jinja2 templates

Containers

Container — abstract base class for all container components

  • Group — groups child components with a separator
  • List — repeats a template for a collection

Logic Components

Logic — abstract base class for all container components

  • If — conditional rendering (if_, then_, else_)

All components support the when parameter — it controls the display of the component and accepts a condition (expression, function or magic_filter).

📝 Example

All usage examples can be found in the example folder.

from magic_filter import F

from textcompose import Template
from textcompose.container import Group, List
from textcompose.elements import Format, Jinja, Text
from textcompose.logic import If

template = Template(
    Format("Hello, {name}!"),
    Format("Status: {status}"),  # or `lambda ctx: f"Status: {ctx['status']}"` with function
    If(
        F["notifications"] > 0,  # `if_` - condition to check if there are notifications
        Format("You have {notifications} new notifications."),  # `then_` - content to render if condition is True
        Format("You not have new notifications."),  # `else_` - content to render if condition is False
    ),
    Group(
        Jinja("\nTotal messages {{ messages|length }}:"),
        List(
            Format("Time - {item[time]}:"),
            Format("-  {item[text]}"),
            sep="\n",  # `sep` - separator between list items
            inner_sep="\n",  # `inner_sep` - separator between parts of a single item
            getter=lambda ctx: ctx["messages"],  # `getter` - function or F to extract the list of messages from context
        ),
        sep="\n",  # `sep` - separator between children of Group
        when=F["messages"].len() > 0,  # `when` - show this block only if there are messages
    ),
    Text("\nThank you for using our service!"),  # or "Recent messages:" without class
)

context = {
    "name": "Alexey",
    "status": "Online",
    "notifications": 2,
    "messages": [
        {"text": "Your package has been delivered.", "time": "09:15"},
        {"text": "Reminder: meeting tomorrow at 10:00.", "time": "18:42"},
    ],
}

print(template.render(context))

Output:

Hello, Alexey!
Status: Online
You have 2 new notifications.

Total messages 2:
Time - 09:15:
-  Your package has been delivered.
Time - 18:42:
-  Reminder: meeting tomorrow at 10:00.

Thank you for using our service!

👨‍💻 Contributing

Contributions are welcome! 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-1.0.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

textcompose-1.0.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for textcompose-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ddc54c347de043899d94223d3584257ab8d00e190889edd7b8e677a7c4819227
MD5 f75e1c0c26893f2d535d1f9f6ff2c38d
BLAKE2b-256 bd3666299623eb9b33b1e6c744221ecc9aff7bc8360144354b5fdb3f98aa8496

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for textcompose-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 960a2fe6773f5ad97a7298f2a03280bdb1ead7f64e57a638398b8321322e34c7
MD5 03be5847d9716e9f31c1ff30a2a42a88
BLAKE2b-256 517d142936f568bae59a0fe84b35f3ed03385f1b3292717ee3809778f0ef473b

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