{"id":19334298,"url":"https://github.com/peng1999/typst-pyrunner","last_synced_at":"2025-04-23T00:31:37.082Z","repository":{"id":192561073,"uuid":"686991384","full_name":"peng1999/typst-pyrunner","owner":"peng1999","description":"Run python code in typst","archived":false,"fork":false,"pushed_at":"2024-04-22T07:56:04.000Z","size":204,"stargazers_count":33,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T05:42:30.608Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peng1999.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-09-04T11:32:40.000Z","updated_at":"2025-02-21T06:25:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"b8da57ee-3b02-4f6a-a075-ba419c74f40d","html_url":"https://github.com/peng1999/typst-pyrunner","commit_stats":null,"previous_names":["peng1999/typst-pyrunner"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peng1999%2Ftypst-pyrunner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peng1999%2Ftypst-pyrunner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peng1999%2Ftypst-pyrunner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peng1999%2Ftypst-pyrunner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peng1999","download_url":"https://codeload.github.com/peng1999/typst-pyrunner/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250348258,"owners_count":21415891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T02:58:11.200Z","updated_at":"2025-04-23T00:31:36.802Z","avatar_url":"https://github.com/peng1999.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typst Python Runner Plugin\n\nRun python code in [typst](https://typst.app) using [RustPython](https://github.com/RustPython/RustPython).\n\n````typst\n#import \"@preview/pyrunner:0.2.0\" as py\n\n#let compiled = py.compile(\n```python\ndef find_emails(string):\n    import re\n    return re.findall(r\"\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b\", string)\n\ndef sum_all(*array):\n    return sum(array)\n```)\n\n#let txt = \"My email address is john.doe@example.com and my friend's email address is jane.doe@example.net.\"\n\n#py.call(compiled, \"find_emails\", txt)\n#py.call(compiled, \"sum_all\", 1, 2, 3)\n````\n\nBlock mode is also available.\n\n````typst\n#let code = ```\nf'{a+b=}'\n```\n\n#py.block(code, globals: (a: 1, b: 2))\n\n#py.block(code, globals: (a: \"1\", b: \"2\"))\n````\n\nThe result will be `a+b=3` and `a+b='12'`.\n\n## Current limitations\n\nDue to restrictions of typst and its plugin system, some Python function will not work as expected:\n- File and network IO will always raise an exception.\n- `datatime.now` will always return 1970-01-01.\n\nAlso, 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.\n\n## API\n### `block`\nRun Python code block and get its result.\n\n#### Arguments\n- `code` : string | raw content - The Python code to run.\n- `globals` : dict (named optional) - The global variables to bring into scope.\n\n#### Returns\nThe last expression of the code block.\n\n### `compile`\nCompile Python code to bytecode.\n\n#### Arguments\n- `code` : string | raw content - The Python code to compile.\n\n#### Returns\nThe bytecode representation in `bytes`.\n\n### `call`\nCall a python function with arguments.\n\n#### Arguments\n- `compiled` : bytes - The bytecode representation of Python code.\n- `fn_name` : string - The name of the function to be called.\n- `..args` : any - The arguments to pass to the function.\n\n#### Returns\nThe result of the function call.\n\n## Build from source\n\nInstall [`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).\n\n[`wasi-stub`]: https://github.com/astrale-sharp/wasm-minimal-protocol\n\n\u003c!-- ```\ncargo install --git https://github.com/astrale-sharp/wasm-minimal-protocol.git wasi-stub\n```--\u003e\n\nBuild pyrunner.\n\n```\nrustup target add wasm32-wasi\ncargo build --target wasm32-wasi\nmake pkg/typst-pyrunner.wasm\n```\n\nAdd to local package.\n\n```\nmkdir -p ~/.local/share/typst/packages/local/pyrunner/0.0.1\ncp pkg/* ~/.local/share/typst/packages/local/pyrunner/0.0.1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeng1999%2Ftypst-pyrunner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeng1999%2Ftypst-pyrunner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeng1999%2Ftypst-pyrunner/lists"}