Typst extension, adding support for generating figures using inline Python code
Project description
typst_pyimage
Wraps Typst to support inline Python code for generating content and 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, pycontent, pyinit
. -
Use these functions.
a.
pyimage(string, ..arguments) -> content
. The positional string should be a Python program that creates a single matplotlib figure. Any named arguments are forwarded on to Typst's built-inimage
function. You can use it just like the normalimage
function, e.g.#align(center, pyimage("..."))
.b.
pycontent(string)
. The positional string should be a Python program that produces a string on its final line. This will be treated as Typst code.c.
pyinit(string)
. The positional string should be a Python program. This will be evaluated before allpyimage
orpycontent
calls, e.g. to do import or perform setup. -
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.
Notes
It's common to have an initial block of code that is in common to all #pyimage("...")
and #pycontent("...")
calls (such as import statements, defining helpers etc). These can be placed in a #pyinit("...")
directive.
Each #pyimage("...")
block is executed as a fresh module (i.e. as if each was a separate Python file), but with the same Python interpreter.
Overall, this is essentially equivalent to the following Python code:
# main.py
import pyinit
import pyimage1
import pyimage2
# pyinit.py
... # your #pyinit("...") code
# pyimage1.py
from pyinit import *
... # your first #pyimage("...") code
# pyimage2.py
from pyinit import *
... # your second #pyimage("...") code
This means that e.g. any global caches will be shared across all #pyimage("...")
calls. (Useful when using a library like JAX, which has a JIT compilation cache.)
Limitations
- The watcher just extracts all the
pyimage("...")
etc. 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
etc. dynamically (i.e. not with a literal string at the top level of your program) will not work. - Only
pyimage("...")
etc. 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.3.tar.gz
.
File metadata
- Download URL: typst_pyimage-0.0.3.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c9f5f69528f0b58e0a06b4faa57040936f72d6820a7a6772ad33d1fa75e22d6 |
|
MD5 | 71294b55af91f7b3bbc3ca2e06e5e3d5 |
|
BLAKE2b-256 | 11c8050227423df5615d58836afb32cc57bb3d8540863764816b4affa36c9b77 |
Provenance
File details
Details for the file typst_pyimage-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: typst_pyimage-0.0.3-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4c2ef67a92c1bfc6065423948d584e73d8f591008aaf3928eae00d173b64a03 |
|
MD5 | 9fb07812bcaa886a4234b98e0d60780f |
|
BLAKE2b-256 | 3fea79723de8807dd2dff3ad39d7985137351b931958be530fa5b1465ea2be30 |