Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/peng1999/typst-pyrunner

Run python code in typst
https://github.com/peng1999/typst-pyrunner

Last synced: about 2 months ago
JSON representation

Run python code in typst

Awesome Lists containing this project

README

        

# Typst Python Runner Plugin

Run python code in [typst](https://typst.app) using [RustPython](https://github.com/RustPython/RustPython).

````typst
#import "@preview/pyrunner:0.2.0" as py

#let compiled = py.compile(
```python
def find_emails(string):
import re
return re.findall(r"\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b", string)

def sum_all(*array):
return sum(array)
```)

#let txt = "My email address is [email protected] and my friend's email address is [email protected]."

#py.call(compiled, "find_emails", txt)
#py.call(compiled, "sum_all", 1, 2, 3)
````

Block mode is also available.

````typst
#let code = ```
f'{a+b=}'
```

#py.block(code, globals: (a: 1, b: 2))

#py.block(code, globals: (a: "1", b: "2"))
````

The result will be `a+b=3` and `a+b='12'`.

## Current limitations

Due to restrictions of typst and its plugin system, some Python function will not work as expected:
- File and network IO will always raise an exception.
- `datatime.now` will always return 1970-01-01.

Also, there is no way to import third-party modules. Only bundled stdlib modules are available. We might find a way to lift this restriction, so feel free to submit an issue if you want this functionality.

## API
### `block`
Run Python code block and get its result.

#### Arguments
- `code` : string | raw content - The Python code to run.
- `globals` : dict (named optional) - The global variables to bring into scope.

#### Returns
The last expression of the code block.

### `compile`
Compile Python code to bytecode.

#### Arguments
- `code` : string | raw content - The Python code to compile.

#### Returns
The bytecode representation in `bytes`.

### `call`
Call a python function with arguments.

#### Arguments
- `compiled` : bytes - The bytecode representation of Python code.
- `fn_name` : string - The name of the function to be called.
- `..args` : any - The arguments to pass to the function.

#### Returns
The result of the function call.

## Build from source

Install [`wasi-stub`][]. You should use a slightly modified one. See [the related issue](https://github.com/astrale-sharp/wasm-minimal-protocol/issues/22#issuecomment-1827379467).

[`wasi-stub`]: https://github.com/astrale-sharp/wasm-minimal-protocol

Build pyrunner.

```
rustup target add wasm32-wasi
cargo build --target wasm32-wasi
make pkg/typst-pyrunner.wasm
```

Add to local package.

```
mkdir -p ~/.local/share/typst/packages/local/pyrunner/0.0.1
cp pkg/* ~/.local/share/typst/packages/local/pyrunner/0.0.1
```