Typst extension, adding support for generating figures using inline Python code
Project description
typst_pyimage
Wraps Typst to support inline Python code for generating figures.
Example
#import ".typst_pyimage/pyimage.typ": pyimage
Consider the Lotka--Volterra (predator-prey)
equations:
#pyimage("
import diffrax
import jax.numpy as jnp
import matplotlib.pyplot as plt
def func(t, y, args):
rabbits, cats = y
d_rabbits = rabbits - rabbits*cats
d_cats = -cats + rabbits*cats
return d_rabbits, d_cats
term = diffrax.ODETerm(func)
solver = diffrax.Tsit5()
y0 = (2, 1)
t0 = 0
t1 = 20
dt0 = 0.01
ts = jnp.linspace(t0, t1, 100)
saveat = diffrax.SaveAt(ts=ts)
sol = diffrax.diffeqsolve(term, solver, t0,
t1, dt0, y0,
saveat=saveat)
plt.plot(ts, sol.ys[0], label='Rabbits')
plt.plot(ts, sol.ys[1], label='Cats')
plt.xlim(0, 20)
plt.ylim(0, 2.5)
plt.xlabel('Time')
plt.ylabel('Population')
plt.legend()
", width: 70%)
(This example uses JAX and Diffrax to solve an ODE.)
Installation
pip install typst_pyimage
This requires that you're using Typst locally -- it won't work with the web app.
Usage
-
Import
pyimage.typ
. At the start of your.typ
file, add the line#import ".typst_pyimage/pyimage.typ": pyimage
. -
Use the
pyimage
function. This has syntaxpyimage(string, ..arguments) -> content
. The positional string should be a Python program that creates a single matplotlib figure. Any named arguments are forward on to Typst's built-inimage
function. You can use it just like the normalimage
function, e.g.#align(center, pyimage("..."))
. -
Compile or watch. Run either of the following two commands:
python -m typst_pyimage compile your_file.typ python -m typst_pyimage watch your_file.typ
This will extract and run all your Python code. In addition it will call either
typst compile your_file.typ
ortypst watch your_file.typ
.The resulting images are saved in the
.typst_pyimage
folder.
Limitations
- The watcher just extracts all the
pyimage("...")
blocks via regex, and runs them in the order that they appear in the file. This means that (a) the"
character may not appear anywhere in the Python code (even if escaped), and (b) trying to callpyimage
dynamically (i.e. not with a literal string at the top level of your program) will not work. - Only
pyimage("...")
calls inside the single watched file are tracked.
We could probably lift 1a and 2 with a bit of effort. PRs welcome.
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
File details
Details for the file typst_pyimage-0.0.1.tar.gz
.
File metadata
- Download URL: typst_pyimage-0.0.1.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39e894b76f9e7d397a6d99a5330bb9ac4a4d994ab6771e02349dc31914642250 |
|
MD5 | 4742c695bc9b0ef16576ac24cb064a0d |
|
BLAKE2b-256 | 3e7a23351d78d0d3816f586f25d42afa7317f870146ad063a9057b03d4fd0f68 |
Provenance
File details
Details for the file typst_pyimage-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: typst_pyimage-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1b67b19e50669972832ecb47cbe62244546489f5c9a07a860b9260e07c6f047 |
|
MD5 | 9d32c516bb4ba8d29d6557a827d732b3 |
|
BLAKE2b-256 | 7bda06608d9f2718275832c624eeebcb8cb25d0bbdf9f00e49519bce10d85656 |