Skip to main content

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

  1. Import pyimage.typ. At the start of your .typ file, add the line #import ".typst_pyimage/pyimage.typ": pyimage, pycontent, pyinit.

  2. 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-in image function. You can use it just like the normal image 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 string will be treated as Typst code.

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

  3. 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 or typst watch your_file.typ.

    The resulting images are saved in the .typst_pyimage folder.

    For more information on the available arguments, run python -m typst_pyimage -h.

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

  1. 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 call pyimage etc. dynamically (i.e. not with a literal string at the top level of your program) will not work.
  2. 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

typst_pyimage-0.0.4.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

typst_pyimage-0.0.4-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typst_pyimage-0.0.4.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for typst_pyimage-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e9b18422a854fc642db0607ea9c26a39b3fdfd2d75808d9b05f48dc97cdfb988
MD5 6ddd5b149b99cca3c3e0c1bd41338c3d
BLAKE2b-256 ed80ab0b9a698d6b968d32969475a37a1611e11dc7514671570b886a04e84728

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for typst_pyimage-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c3f79ff4c1ef61b68d931dcef0df4689be9f42731f8ac734cde4ac5f947d0f37
MD5 c907c6f57cbdcb2ed31ac4918525ff45
BLAKE2b-256 90697102cbe178cca98c81b3faef7e072168ecace8d9c4e0514c7905bf46484e

See more details on using hashes here.

Provenance

Supported by

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