A Django template engine powered by Typst
Project description
Django Typst
A Django template engine that uses Typst to render Portable Document Format (PDF) files.
Installation and Configuration
The Django Typst engine is available from PyPI so you can install it with all the usual tools:
pip install django_typst
# or
uv add django_typst
# or
poetry add django_typst
Once installed, to make the typst engine available, you need to add it to the
TEMPLATES configuration in your settings.py. e.g.:
TEMPLATES = [
...
{
"BACKEND": "django_typst.TypstEngine",
"NAME": "typst",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": False,
"OPTIONS": {
"ROOT": None,
"FONT_PATHS": [],
"IGNORE_SYSTEM_FONTS": False,
"PDF_STANDARD": "1.7",
"PPI": None,
}
},
]
Note that this should be in addition to the standard Django template engine that was already there.
All the OPTIONS are... optional and the values above represent their defaults should
no alternative be provided.
| Option | Description | Default |
|---|---|---|
| ROOT | The root path to use for relative paths | None* |
| FONT_PATHS | Paths to look in for fonts | [] |
| IGNORE_SYSTEM_FONTS | Only consider fonts in the defined font paths | False |
| PDF_STANDARD | PDF revision to target (1.7, a2-b, a3-b) |
"1.7" |
| PPI | Pixel Per Inch for included PNG | None |
* The engine with use the folder the template is in as the root if one is not specified.
Usage
To use this engine with one of the standard Django class based views you only need to
set the template_engine class property to the value "typst". You should also set the
content_type property to the value "application/pdf" to ensure the PDF is returned
with the correct content type. For example, in a simple TemplateView it would look
like this:
from django.views import generic
class MyTemplateView(generic.TemplateView):
template_engine = "typst"
content_type = "application/pdf"
...
Handling Context
Context data passed into the template it is first encoded in TOML format. Note this means that only data types that can be serialized as TOML can be passed as context variables. We use tomlkit to serialize the context. In practice this means the following types can be included in the context:
strintfloatbooldatetime.datetime,.time, and.datelistdict
In addition to these types, you can also include decimal.Decimal, uuid.UUID objects,
which are rendered to strings before serializing.
There is also special handling for the Django HTTPRequest object, which is converted
to a dict before serializing. The contents of that dict are:
{
"path": request.path,
"path_info": request.path_info,
"method": request.method,
"content_type": request.content_type,
"content_params": request.content_params,
"headers": {}, # header-name: header-value
}
To then make use of context within a Typst template you must parse the incoming toml data by adding the following to the top of the typst file:
#let ctx = toml(bytes(sys.inputs.context))
This will deserialize the context and assign it to the typst variable ctx which can
then be used in the template like any other variable. For example if you context was
something like:
{
"name": "J Moss",
"flight": "QF1",
}
Then in your template you will be able to reference it with:
#ctx.name your flight number is #ctx.flight
Or even better is to parse the context as follows:
// Parse context or use defaults
#let ctx = if ("context" in sys.inputs) {
toml(bytes(sys.inputs.context))
} else {
(
"name": "A Citizen",
"flight": "DL31"
)
}
This version sets a default if context is not passed in. This allows you to test the
template in isolation - say with the Tinymist Extension to VSCode or even just running
typst directly on it.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_typst-0.1.1.tar.gz.
File metadata
- Download URL: django_typst-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789277876d5059b058446244f8fbb20878f810b6bf6bf24c4c52749e4771466f
|
|
| MD5 |
397a634dc399c3904bf6f505e9d50359
|
|
| BLAKE2b-256 |
64ea2455c6e382aab22d007752dfc3551370cede17bd71ded409b3811a9ccda6
|
Provenance
The following attestation bundles were made for django_typst-0.1.1.tar.gz:
Publisher:
publish.yml on a-musing-moose/django-typst-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_typst-0.1.1.tar.gz -
Subject digest:
789277876d5059b058446244f8fbb20878f810b6bf6bf24c4c52749e4771466f - Sigstore transparency entry: 467073921
- Sigstore integration time:
-
Permalink:
a-musing-moose/django-typst-engine@fc205f7492d14db4e20d4e85d2b427b4ac215935 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/a-musing-moose
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fc205f7492d14db4e20d4e85d2b427b4ac215935 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_typst-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_typst-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74a16f8a7e4b46ae77b1f61e397d5c6f64598467ba780688d629f080b9eb6fc0
|
|
| MD5 |
ff19ef171b7fa9fd57e3cd3f94020709
|
|
| BLAKE2b-256 |
5d17ff38131ec45b20b1b68b8fa9e87ca5038d8a314203d7b578db34c2569539
|
Provenance
The following attestation bundles were made for django_typst-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on a-musing-moose/django-typst-engine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_typst-0.1.1-py3-none-any.whl -
Subject digest:
74a16f8a7e4b46ae77b1f61e397d5c6f64598467ba780688d629f080b9eb6fc0 - Sigstore transparency entry: 467073948
- Sigstore integration time:
-
Permalink:
a-musing-moose/django-typst-engine@fc205f7492d14db4e20d4e85d2b427b4ac215935 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/a-musing-moose
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fc205f7492d14db4e20d4e85d2b427b4ac215935 -
Trigger Event:
release
-
Statement type: