Skip to main content

Dynamic Text Renderer

Project description

DTR banner

DTR — Dynamic Text Renderer

DTR is a Python library built in Rust (PyO3) for rendering template strings with custom function calls, file includes, and variables. Use it to generate dynamic text from templates while keeping logic in Python.

Python 3.8+ License: MIT Rust

Features

  • Function calls — Register Python callables and invoke them from templates (~fn:name(args)).
  • File includes — Pull in external template files (~include:path).
  • Variables — Resolve $variables inside function arguments.
  • Custom syntax — Optional prefixes and name patterns via Syntax.
  • Fast core — Parsing and rendering run in Rust; Python is used for your functions.

Installation

pip install dtrlib

Install from source

Requires Rust and maturin:

git clone https://github.com/naginagiyev/dtr.git
cd dtr
pip install maturin
maturin develop

Quick start

from dtr import Compiler

compiler = Compiler()

def repeat(args):
    text = str(args[0]) if args else ""
    count = int(args[1]) if len(args) > 1 else 1
    return text * count

compiler.add_function("str.repeat", repeat)

template = "Result: ~fn:str.repeat('ha', 3)"
print(compiler.compile(template))
# Result: hahaha

Template syntax

Default syntax (configurable with Syntax):

Feature Syntax Example
Function call ~fn:name(args) ~fn:math.add(1, 2)
Include file ~include:path ~include:"footer.txt"
Variable (in function args) $name ~fn:greet($user)

Function calls

Register a function with its full dotted name, then call it in text:

compiler = Compiler()

def upper(args):
    return str(args[0]).upper() if args else ""

compiler.add_function("text.upper", upper)
print(compiler.compile("~fn:text.upper('hello')"))
# HELLO

Arguments support strings, numbers, booleans, lists, and dicts. Empty arguments become null.

Variables

Set values with set_arg, then reference them inside function arguments:

compiler.set_arg("name", "Ada")
print(compiler.compile("~fn:text.upper($name)"))
# ADA

File includes

Includes are resolved relative to the current file path (or the working directory when compiling from a string):

output = compiler.compile_with_file(template, "letters/template.txt")

Circular includes are detected; behavior depends on debug_mode (see below).

Custom syntax

from dtr import Compiler, Syntax

syntax = Syntax(
    function_prefix="@@",
    include_prefix="@@include:",
    variable_prefix="@",
)
compiler = Compiler(syntax)

API reference

Compiler

Method Description
compile(text) Render a string (virtual file name <input>).
compile_with_file(text, file_name) Render with a path used for includes and errors.
set_debug_mode(enabled) When True, errors raise; when False, failed calls/includes are left unchanged.
set_arg(name, value) Set a variable for use in function arguments.
clear_args() Remove all variables.
add_function(full_name, callable) Register a Python callable; receives a list of parsed argument values.

Syntax

Optional constructor arguments: function_prefix, include_prefix, variable_prefix, function_name_pattern, variable_name_pattern.

Defaults:

  • Function prefix: ~fn:
  • Include prefix: ~include:
  • Variable prefix: $

Development

pip install maturin
maturin develop
maturin build --release

Run a quick check:

from dtr import Compiler
assert Compiler().compile("plain text") == "plain text"

Publishing

maturin build --release
maturin publish

Use maturin publish --repository testpypi to test on TestPyPI first.

License

MIT — see LICENSE.

Links

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

dtrlib-0.1.2.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

dtrlib-0.1.2-cp310-cp310-win_amd64.whl (794.3 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file dtrlib-0.1.2.tar.gz.

File metadata

  • Download URL: dtrlib-0.1.2.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for dtrlib-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a6b80b6684d762cfddd8461a6b2b98d7719c426d20f729120543586f42b80bb1
MD5 c22f77845cd68cd1ee1dd48eb217b630
BLAKE2b-256 a0f6be9d4e5ee5d77d8c6766de9dac4acde953c3d0c0ef2b122d1faedc73faac

See more details on using hashes here.

File details

Details for the file dtrlib-0.1.2-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: dtrlib-0.1.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 794.3 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for dtrlib-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2d2238f6dd5aa340eeb2dd5ffaec63a15a7b7899de24bcde414598999a114876
MD5 382106c7e7adb0d679dbd46095bc8f69
BLAKE2b-256 abd9bd3f0d4809e1dec5c0e5b0db993ff0ae1569b0aabed381764da01099f309

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