To synthesize @Tinger and @SillyFreak with the suggestion of the typst
library, here is a simple hello world example in case someone is looking in the future:
In helloworld.typ
:
= Hello world
#sys.inputs.at("some_value")
In helloworld.py
:
import typst # using v0.11.1
# sys_inputs must be a dictionary of type string:string
sys_inputs: dict[str, str] = {"some_value": "hello world"}
typst.compile(input="helloworld.typ", output="helloworld.pdf", sys_inputs=sys_inputs)
The benefit of this method is that you could more easily compose your typst template (i.e. via the website).
However, a limitation seems to be in what you can pass to the template (dictionary of key:value, but both are limited to string type). That likely means that, for example, if you want to pass a tuple of values from a database query, it would have to be converted to string, then in the typst template you would have to convert to a typst-native type.
So, if you need more versatility in objects, you may need to do string templating of some variety within python (i.e. jinja, or string templates, etc).
Update:
Based on @SillyFreakâs comment below, here is another example passing JSON data:
In json_example.py
:
import typst
import json
# convert your data to string with json.dumps
data = json.dumps([1, 2, 3, 4, 5])
sys_inputs: dict[str, str] = {"data": data}
typst.compile(
input="json_example.typ", output="json_example.pdf", sys_inputs=sys_inputs
)
In json_example.typ
:
#let values = json.decode(sys.inputs.data)
#for value in values [
#value #linebreak()
]