Skip to main content

Typst extension, adding support for generating figures using inline Python code

Project description

typst_pyimage

Extends Typst with 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

Usage is two steps. Write your content within the .typ file, and then run from the command line to build the content for Typst to actually import.

Within the .typ file:

Import like so (the .typst_pyimage/pyimage.typ file will be added in our next step):

#import ".typst_pyimage/pyimage.typ": pyinit, pycontent, pyimage

It provides the following functions:

  • pyinit(string). The positional string should be a Python program. This will be evaluated once before all pyimage or pycontent calls, e.g. to perform imports or other setup.

  • pycontent(string). The positional string should be a single Python expression. When evaluated with a Python str(...), the result will treated as Typst markup.

  • pyimage(string, ..arguments) -> content. The positional string should be a Python program that creates a single matplotlib figure. Any additional ..arguments are forwarded on to Typst's built-in image function. You can use it just like the normal image function, e.g. #align(center, pyimage("...")).

Each pycontent or pyimage call is assumed to be a deterministic function of its string input (and that of the string passed to pyinit if it is used). The result will be cached, and only re-evaluated if that input changes.

Call from the command line:

Now it's time to run things!

To one-off compile:

python -m typst_pyimage compile your_file.typ
typst compile your_file.typ

To watch (run these two commands at the same time):

python -m typst_pyimage watch your_file.typ
typst watch your_file.typ

Python scoping rules

Each #pyinit("..."), #pycontent("...") and #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 state will be shared across all #pyimage("...") calls.

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

typst_pyimage-0.1.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typst_pyimage-0.1.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file typst_pyimage-0.1.1.tar.gz.

File metadata

  • Download URL: typst_pyimage-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for typst_pyimage-0.1.1.tar.gz
Algorithm Hash digest
SHA256 595ea6ad33cb46ce46551a5afae0712cf253d9f5ceed36648cb043ac579540b3
MD5 4444d9bc2d4d0c2808bd269253ba8c36
BLAKE2b-256 05eb9b3cbe8d129ad71b10e60d7c8a9a7a56eb0730fbd19f175688f6c6ace04a

See more details on using hashes here.

File details

Details for the file typst_pyimage-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: typst_pyimage-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for typst_pyimage-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27d52256918859b4df7bc3434d2c65d602b42e5948b30d7dbbe63db756c5d0f3
MD5 de04c045361163b4c2fe3235675fd308
BLAKE2b-256 657a34484c76983c63e62af1c164f4e45b9101a65bc2c815cac336351b0f2455

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page