{"id":23142980,"url":"https://github.com/marcomq/tauri-plugin-python","last_synced_at":"2025-04-08T03:20:24.674Z","repository":{"id":268431614,"uuid":"903013866","full_name":"marcomq/tauri-plugin-python","owner":"marcomq","description":"Tauri plugin to run python code in the backend instead of rust","archived":false,"fork":false,"pushed_at":"2025-03-23T19:23:53.000Z","size":667,"stargazers_count":40,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T01:04:46.975Z","etag":null,"topics":["python","rust","tauri","tauri-plugin","tauri2","webview"],"latest_commit_sha":null,"homepage":"","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/marcomq.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"publiccode":null,"codemeta":null}},"created_at":"2024-12-13T18:36:41.000Z","updated_at":"2025-03-30T09:50:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"dfaee3bb-4966-40b2-a656-463eacc3f94a","html_url":"https://github.com/marcomq/tauri-plugin-python","commit_stats":null,"previous_names":["marcomq/tauri-plugin-python"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcomq%2Ftauri-plugin-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcomq%2Ftauri-plugin-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcomq%2Ftauri-plugin-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marcomq%2Ftauri-plugin-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marcomq","download_url":"https://codeload.github.com/marcomq/tauri-plugin-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457749,"owners_count":20941906,"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":["python","rust","tauri","tauri-plugin","tauri2","webview"],"created_at":"2024-12-17T15:06:11.916Z","updated_at":"2025-04-08T03:20:24.659Z","avatar_url":"https://github.com/marcomq.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tauri Plugin Python\n\nThis [tauri](https://v2.tauri.app/) v2 plugin is supposed to make it easy to use Python as backend code.\nIt uses [RustPython](https://github.com/RustPython/RustPython) or alternatively [PyO3](https://pyo3.rs) as interpreter to call python from rust.\n\nRustPython doesn't require python to be installed on the target platform and makes it \ntherefore easy to deploy your production binary. Unfortunately, it has some \ncompatibility issues and is slower than PyO3/CPython. PyO3 is also supported as optional Cargo feature for desktop applications. \nPyO3 uses CPython as interpreter and therefore has a wide compatibility for available python libraries.\nIt isn't used as default as it requires to make libpython available for the target platform,\nwhich can be complicated, especially for mobile targets.\n\nThe plugin reads by default the file `src-tauri/src-python/main.py` during \nstartup and runs it immediately. Make sure to add all your python source as tauri resource,\nso it is shipped together with your production binaries. Python functions are all registered during plugin initialization \nand can get called during application workflow.\n\n\n| Platform | Supported |\n| -------- | --------- |\n| Linux    | ✓         |\n| Windows  | ✓         |\n| MacOS    | ✓         |\n| Android  | x*         |\n| iOS      | ✓*        |\n\n\n`x*` There is currently a known issue on tauri+android that prevents reading files.\nhttps://github.com/tauri-apps/tauri/issues/11823 \\\nSo python code cannot be read on android right now. Android is going to be supported as soon as reading resource files will be fixed.\n\n`✓*` Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and IOS\ncurrently only support RustPython. \nAndroid and iOS might also be able to run with PyO3 in theory but would require to have CPython\nto be compiled for the target platform. I still need to figure out how to \ncross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.\n\n\nYou can use this plugin for fast prototypes or for production code. \nIt might be possible that you want to use some python library or code that\nis not available for rust yet.\nIn case that you want to ship production software packages, you need \nto make sure to also ship all your python code. If you use PyO3, you also need to ship libpython too.\n\n### Switch from RustPython to PyO3\n\n```toml\n# src-tauri/Cargo.toml\ntauri-plugin-python = { version=\"0.3\", , features = [\"pyo3\"] }\n\n```\n\n## Example app\n\nThere is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla \nJavascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript).\n\n\n## Add the plugin to an existing tauri application\n\nThese steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the application in \"example\" directory.\n\n- run `npm run tauri add python`\n- add `src-tauri/src-python/main.py` and modify it according to your needs, for example add \n```python\n# src-tauri/src-python/main.py\n_tauri_plugin_functions = [\"greet_python\"] # make \"greet_python\" callable from UI\ndef greet_python(rust_var)\n    return str(rust_var) + \" from python\"\n```\n- add `\"bundle\": {\"resources\": [  \"src-python/**/*\"],` to `tauri.conf.json` so that python files are bundled with your application\n- add the plugin in your js, so \n   - add `import { callFunction } from 'tauri-plugin-python-api'` \n   - add `outputEl.textContent = await callFunction(\"greet_python\", [value])` to get the output of the python function `greet_python` with parameter of js variable `value`\n\nCheck the examples for alternative function calls and code sugar.\n\nTauri events and calling js from python is currently not supported yet. You would need to use rust for that.\n\n## Alternative manual plugin installation\n\n- `$ cargo add tauri-plugin-python`\n- `$ npm install tauri-plugin-python-api`\n- modify `permissions:[]` in src-tauri/capabilities/default.json and add \"python:default\"  \n- add file `src-tauri/src-python/main.py` and add python code, for example:\n```python\n# src-tauri/src-python/main.py\ndef greet_python(rust_var)\n    return str(rust_var) + \" from python\"\n```\n- add `.plugin(tauri_plugin_python::init_and_register(vec![\"greet_python\"))` to `tauri::Builder::default()`, usually in `src-tauri/src/lib.rs`. This will initialize the plugin and make the python function \"greet_python\" available from javascript.\n- add javascript for python plugin in the index.html file directly or in your somewhere in your javascript application. For vanilla javascript / iife, the modules can be found in `window.__TAURI__.python`. For modern javascript:\n```javascript\nimport { callFunction } from 'tauri-plugin-python-api'\nconsole.log(await callFunction(\"greet_python\", [\"input value\"]))\n```\n-\u003e this will call the python function \"greet_python\" with parameter \"input value\". Of course, you can just pass in any available javascript value. This should work with \"boolean\", \"integer\", \"double\", \"string\", \"string[]\", \"double[]\" parameter types.\n\nAlternatively, to have more readable code: \n```javascript\nimport { call, registerJs } from 'tauri-plugin-python-api'\nregisterJs(\"greet_python\");\nconsole.log(await call.greet_python(\"input value\"));\n```\n\n## Deployment\n\nYou either need to have python installed on the target machine or ship the shared \npython library with your package. You also may link the python library statically - PyO3 \nmay do this by default if it finds a static python library. In addition, you need \nto copy the python files so that python files are next to the binary. \n\nThe file `src-python/main.py` is required for the plugin to work correctly. \nYou may also add additional python files or use a venv environment. \nThe included resources can be configurable in the `tauri.conf.json` file. \n\nCheck the tauri and PyO3 documentation for additional info. \n\n## Security considerations\nBy default, this plugin cannot call arbitrary python code. Python functions can only be called if registered from rust during plugin initialization.\nIt may still be possible to read values from python. This can be prevented via additional tauri permissions.\n\nKeep in mind that this plugin could make it possible to run arbitrary python code when using all allow permissions. \nIt is therefore highly recommended to **make sure the user interface is not accessible by a network URL** in production. \n\nThe \"runPython\" command is disabled by default via permissions. If enabled, it is possible to \ninject python code directly via javascript.\nAlso, the function \"register\" is disabled by default. If enabled, it can \nadd control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code.\nBoth functions can be enabled during development for rapid prototyping.\n\n## Alternatives\nIf already know that you just want to develop completely in python, you might want to take a look at [pytauri](https://github.com/WSH032/pytauri). \nIt is a different approach to have all tauri functionality completely in python.\n\nThis approach here with tauri-plugin-python is more lightweight and it is for you, if you \n- still want to write rust code\n- already have a tauri application and just need a specific python library\n- just want to simply support rare custom plugins\n- if you want to embed python code directly in your javascript\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcomq%2Ftauri-plugin-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcomq%2Ftauri-plugin-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcomq%2Ftauri-plugin-python/lists"}