|
1 | 1 | # Tauri Plugin Python |
2 | 2 |
|
3 | 3 | This [tauri](https://v2.tauri.app/) plugin is supposed to make it easy to use Python as backend code. |
4 | | -It uses [PyO3](https://pyo3.rs) to call python from rust. |
| 4 | +It uses [RustPython](https://github.com/RustPython/RustPython) as interpreter to call python from rust. |
| 5 | +RustPython doesn't require python to be installed on the target platform and makes it |
| 6 | +therefore easy to deploy your production binary. Unfortunately, it has some |
| 7 | +compatibility issues and is slower than PyO3/CPython. [PyO3](https://pyo3.rs) is also supported as optional Cargo feature for desktop applications. |
| 8 | +PyO3 uses CPython as interpreter and therefore has a much better compatibility for python libraries. |
| 9 | +It isn't used as default as it requires to make libpython available for the target platform, |
| 10 | +which can be complicated, especially for mobile targets. |
| 11 | + |
5 | 12 | The plugin reads by default the file `src-tauri/src-python/main.py` during |
6 | | -startup and runs it immediately. Python functions are then registered during initialization |
| 13 | +startup and runs it immediately. Make sure to add all your python source as tauri resource, |
| 14 | +so it is shipped together with your productioon binaries. Python functions are all registered during plugin initialization |
7 | 15 | and can get called during application workflow. |
8 | 16 |
|
9 | 17 |
|
10 | 18 | | Platform | Supported | |
11 | 19 | | -------- | --------- | |
12 | 20 | | Linux | ✓ | |
13 | 21 | | Windows | ✓ | |
14 | | -| macOS | ✓ | |
15 | | -| Android | not yet | |
16 | | -| iOS | not yet | |
| 22 | +| MacOS | ✓ | |
| 23 | +| Android | ✓* | |
| 24 | +| iOS | ✓* | |
17 | 25 |
|
| 26 | +`*` Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and IOS |
| 27 | +currently only supports RustPython. |
| 28 | +Android and iOS might also be able to run PyO3 in theory but require to have CPython |
| 29 | +to be compiled for the target platform. I still need to figure out how to |
| 30 | +cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that. |
18 | 31 |
|
19 | 32 | You might use this plugin to create simple prototype applications |
20 | 33 | and later re-write functions in rust to improve |
21 | 34 | performance, add a specific rust library or just call some |
22 | 35 | low-level code. |
23 | 36 |
|
24 | | -Android and iOS are possible in theory but I still need to figure out how to |
25 | | -cross compile python and PyO3 for iOS and android. |
26 | | - |
27 | | -Also, this plugin hasn't been optimized yet for production binaries. |
28 | | -The target platform therefore either needs to have libpython installed |
29 | | -or you manually need to ship the shared libs together with the installer package. |
30 | | - |
31 | 37 | ## Example app |
32 | 38 |
|
33 | 39 | There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla |
34 | | -Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript) |
| 40 | +Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript). |
| 41 | + |
| 42 | + |
| 43 | +## Add the plugin to an existing tauri application |
| 44 | +- run `npm run tauri add python` |
| 45 | +- add `src-tauri/src-python/main.py` and modify it acording to your needs, for example add `def greet_python(intput): return str(input) + " from python"` |
| 46 | +- modify `src-tauri/src/lib.rs` and change `.plugin(tauri_plugin_python::init())` to `.plugin(tauri_plugin_python::init(["greet_python"]))`; make sure you list all python functions you |
| 47 | +want to call |
| 48 | +- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application |
| 49 | +- add the plugin in your js, so |
| 50 | + - add `import { callFunction } from 'tauri-plugin-python-api'` |
| 51 | + - add `outputEl.textContent = await tauri.python.callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value` |
| 52 | + |
| 53 | +Check the examples for alternative function calls and code sugar. |
| 54 | + |
| 55 | +Tauri events and calling js from python is currently not supported yet. You would need to use rust for that. |
35 | 56 |
|
36 | 57 | ## Security considerations |
37 | | -Generally, this plugin has been created by "security by default" concept. Python functions can onl be called if registered from rust. |
| 58 | +Generally, this plugin has been created by "security by default" concept. Python functions can onl be called if registered from rust during plugin initialization. |
38 | 59 |
|
39 | | -Keep in mind that this plugin can make it possible to run arbitrary python code. |
| 60 | +Keep in mind that this plugin could make it possible to run arbitrary python code. |
40 | 61 | It is therefore highly recommended to **not make the user interface accessible by a network URL**. |
41 | 62 |
|
42 | 63 | The "runPython" command is disabled by default via permissions. If enabled, it is possible to |
|
0 commit comments