Skip to main content

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.

Register a Python file as a module

Load a .py file and expose its top-level def functions under a namespace:

compiler = Compiler()

compiler.add_module("random_number_generator.py", "generator")
print(compiler.compile("~fn:generator.roll(6)"))
  • module_path: path to the .py file (relative to the current working directory or absolute).
  • as_name: namespace used in templates (generator.func, not the filename).

Only module-level functions are registered; names starting with _ are skipped. Classes, variables, and other attributes are ignored. Re-registering the same namespace overwrites previous functions for matching names.

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.
add_module(module_path, as_name) Load a .py file and register its top-level functions as as_name.function.

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.

Metadata

Release files for dtrlib 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dtrlib 2.0.0
File Size Uploaded
dtrlib-2.0.0.tar.gz 18.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dtrlib 2.0.0
File Interpreter ABI Platform
dtrlib-2.0.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details

Total release size: 817.3 kB

Release files / dtrlib-2.0.0.tar.gz

Download URL dtrlib-2.0.0.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
89e83e1440ef4104ef4bf27cc7f79fada8eba6bae248939522f294c90e141381
BLAKE2b-256 checksum
How to use checksums
e1204eff003b940bccd507cb9039b4029f5fc0816998de38ce421ee019c8cf13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.10.2

Release files / dtrlib-2.0.0-cp310-cp310-win_amd64.whl

Download URL dtrlib-2.0.0-cp310-cp310-win_amd64.whl
Size 798.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
130e1ef6183f03e66c3e9ab239e04b38e5caf59c67498406d203be28b052b53a
BLAKE2b-256 checksum
How to use checksums
93e41b4df1fbe9b38fd2622a69232c447e080be7f0648b54e27b4b24af68bcec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.10.2

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page