You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This [tauri](https://v2.tauri.app/) v2 plugin is supposed to make it easy to use Python as backend code.
3
+
This [tauri](https://v2.tauri.app/) v2 plugin is supposed to make it easy to use Python as backend code.
4
4
It uses [RustPython](https://github.com/RustPython/RustPython) or alternatively [PyO3](https://pyo3.rs) as interpreter to call python from rust.
5
5
6
-
RustPython doesn't require python to be installed on the target platform and makes it
7
-
therefore easy to deploy your production binary. Unfortunately, it has some
8
-
compatibility issues and is slower than PyO3/CPython. PyO3 is also supported as optional Cargo feature for desktop applications.
9
-
PyO3 uses CPython as interpreter and therefore has a wide compatibility for available python libraries.
10
-
It isn't used as default as it requires to make libpython available for the target platform,
6
+
RustPython doesn't require python to be installed on the target platform and makes it
7
+
therefore easy to deploy your production binary. Unfortunately, it has some
8
+
compatibility issues and is slower than PyO3/CPython. PyO3 is also supported as optional Cargo feature for desktop applications.
9
+
PyO3 uses CPython as interpreter and therefore has a wide compatibility for available python libraries.
10
+
It isn't used as default as it requires to make libpython available for the target platform,
11
11
which can be complicated, especially for mobile targets.
12
12
13
-
The plugin reads by default the file `src-tauri/src-python/main.py` during
14
-
startup and runs it immediately. Make sure to add all your python source as tauri resource,
15
-
so it is shipped together with your production binaries. Python functions are all registered during plugin initialization
13
+
The plugin reads by default the file `src-tauri/src-python/main.py` during
14
+
startup and runs it immediately. Make sure to add all your python source as tauri resource,
15
+
so it is shipped together with your production binaries. Python functions are all registered during plugin initialization
16
16
and can get called during application workflow.
17
17
18
-
19
18
| Platform | Supported |
20
19
| -------- | --------- |
21
20
| Linux | ✓ |
22
21
| Windows | ✓ |
23
22
| MacOS | ✓ |
24
-
| Android | x*|
23
+
| Android | x*|
25
24
| iOS | ✓*|
26
25
27
-
28
-
`x*` There is currently a known issue on tauri+android that prevents reading files.
29
-
https://github.com/tauri-apps/tauri/issues/11823\
26
+
`x*` There is currently a known issue on tauri+android that prevents reading files.
27
+
https://github.com/tauri-apps/tauri/issues/11823
30
28
So python code cannot be read on android right now. Android is going to be supported as soon as reading resource files will be fixed.
31
29
32
-
`✓*` Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and IOS
33
-
currently only support RustPython.
34
-
Android and iOS might also be able to run with PyO3 in theory but would require to have CPython
35
-
to be compiled for the target platform. I still need to figure out how to
30
+
`✓*` Linux, Windows and MacOS support PyO3 and RustPython as interpreter. Android and iOS
31
+
currently only support RustPython.
32
+
Android and iOS might also be able to run with PyO3 in theory but would require to have CPython
33
+
to be compiled for the target platform. I still need to figure out how to
36
34
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
37
35
38
-
39
-
You can use this plugin for fast prototypes or for production code.
40
-
It might be possible that you want to use some python library or code that
41
-
is not available for rust yet.
42
-
In case that you want to ship production software packages, you need
36
+
You can use this plugin for fast prototypes or for production code.
37
+
It might be possible that you want to use some python library or code that
38
+
is not available for rust yet.
39
+
In case that you want to ship production software packages, you need
43
40
to make sure to also ship all your python code. If you use PyO3, you also need to ship libpython too.
44
41
45
42
### Switch from RustPython to PyO3
46
43
47
44
```toml
48
45
# src-tauri/Cargo.toml
49
-
tauri-plugin-python = { version="0.3", , features = ["pyo3"] }
50
-
46
+
tauri-plugin-python = { version="0.3", features = ["pyo3"] }
51
47
```
52
48
53
49
## Example app
54
50
55
-
There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla
51
+
There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla
56
52
Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript).
57
53
58
-
59
54
## Add the plugin to an existing tauri application
60
55
61
56
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the application in "example" directory.
62
57
63
58
- run `npm run tauri add python`
64
-
- add `src-tauri/src-python/main.py` and modify it according to your needs, for example add
59
+
- add `src-tauri/src-python/main.py` and modify it according to your needs, for example add
65
60
```python
66
61
# src-tauri/src-python/main.py
67
-
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
68
-
defgreet_python(rust_var)
62
+
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
63
+
defgreet_python(rust_var):
69
64
returnstr(rust_var) +" from python"
70
65
```
71
66
- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application
72
-
- add the plugin in your js, so
73
-
- add `import { callFunction } from 'tauri-plugin-python-api'`
67
+
- add the plugin in your js, so
68
+
- add `import { callFunction } from 'tauri-plugin-python-api'`
74
69
- add `outputEl.textContent = await callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value`
75
70
76
71
Check the examples for alternative function calls and code sugar.
@@ -85,18 +80,18 @@ Tauri events and calling js from python is currently not supported yet. You woul
85
80
- add file `src-tauri/src-python/main.py` and add python code, for example:
86
81
```python
87
82
# src-tauri/src-python/main.py
88
-
defgreet_python(rust_var)
83
+
defgreet_python(rust_var):
89
84
returnstr(rust_var) +" from python"
90
85
```
91
-
- 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.
92
-
- 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:
86
+
- 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.
87
+
- add javascript for python plugin in the index.html file directly or somewhere in your javascript application. For vanilla javascript / iife, the modules can be found in `window.__TAURI__.python`. For modern javascript:
-> 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.
92
+
→ 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.
0 commit comments