|
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 | not yet | |
| 24 | +| iOS | ✓* | |
| 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. |
17 | 31 |
|
18 | 32 | You can use this plugin for fast prototypes or for production code. |
19 | 33 | It might be possible that you want to use some python library or code that |
20 | 34 | is not available for rust yet. |
21 | 35 | In case that you want to ship production software packages, you just need |
22 | | -to make sure to also ship the python code and python interpreter. |
23 | | - |
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. |
| 36 | +to make sure to also ship the python code. If you use PyO3, you also need to ship libpython too. |
30 | 37 |
|
31 | 38 | ## Example app |
32 | 39 |
|
33 | 40 | 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) |
| 41 | +Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript). |
| 42 | + |
| 43 | + |
| 44 | +## Add the plugin to an existing tauri application |
35 | 45 |
|
36 | | -## Manual plugin installation / usage |
37 | 46 |
|
38 | 47 | These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the example application. |
39 | 48 |
|
| 49 | +- run `npm run tauri add python` |
| 50 | +- 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"` |
| 51 | +- 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 |
| 52 | +want to call |
| 53 | +- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application |
| 54 | +- add the plugin in your js, so |
| 55 | + - add `import { callFunction } from 'tauri-plugin-python-api'` |
| 56 | + - add `outputEl.textContent = await callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value` |
| 57 | + |
| 58 | +Check the examples for alternative function calls and code sugar. |
| 59 | + |
| 60 | +Tauri events and calling js from python is currently not supported yet. You would need to use rust for that. |
| 61 | + |
| 62 | +## Alternative manual plugin installation |
| 63 | + |
40 | 64 | - `$ cargo add tauri-plugin-python` |
41 | 65 | - `$ npm install tauri-plugin-python-api` |
42 | 66 | - modify `permissions:[]` in src-tauri/capabilities/default.json and add "python:default" |
@@ -76,9 +100,9 @@ The included resources can be configurable in the `tauri.conf.json` file. |
76 | 100 | Check the tauri and PyO3 documentation for additional info. |
77 | 101 |
|
78 | 102 | ## Security considerations |
79 | | -Generally, this plugin has been created by "security by default" concept. Python functions can only be called if registered from rust. |
| 103 | +Generally, this plugin has been created by "security by default" concept. Python functions can only be called if registered from rust during plugin initialization. |
80 | 104 |
|
81 | | -Keep in mind that this plugin can make it possible to run arbitrary python code. |
| 105 | +Keep in mind that this plugin could make it possible to run arbitrary python code. |
82 | 106 | It is therefore highly recommended to **make sure the user interface is not accessible by a network URL** in production. |
83 | 107 |
|
84 | 108 | The "runPython" command is disabled by default via permissions. If enabled, it is possible to |
|
0 commit comments