Skip to content

Commit 8de6ab2

Browse files
committed
2 parents 2a22887 + 0838430 commit 8de6ab2

File tree

1 file changed

+55
-58
lines changed

1 file changed

+55
-58
lines changed

README.md

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,71 @@
11
# Tauri Plugin Python
22

3-
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.
44
It uses [RustPython](https://github.com/RustPython/RustPython) or alternatively [PyO3](https://pyo3.rs) as interpreter to call python from rust.
55

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,
1111
which can be complicated, especially for mobile targets.
1212

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
1616
and can get called during application workflow.
1717

18-
1918
| Platform | Supported |
2019
| -------- | --------- |
2120
| Linux ||
2221
| Windows ||
2322
| MacOS ||
24-
| Android | x* |
23+
| Android | x* |
2524
| iOS |* |
2625

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
3028
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.
3129

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
3634
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
3735

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
4340
to make sure to also ship all your python code. If you use PyO3, you also need to ship libpython too.
4441

4542
### Switch from RustPython to PyO3
4643

4744
```toml
4845
# src-tauri/Cargo.toml
49-
tauri-plugin-python = { version="0.3", , features = ["pyo3"] }
50-
46+
tauri-plugin-python = { version="0.3", features = ["pyo3"] }
5147
```
5248

5349
## Example app
5450

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
5652
Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript).
5753

58-
5954
## Add the plugin to an existing tauri application
6055

6156
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the application in "example" directory.
6257

6358
- 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
6560
```python
6661
# src-tauri/src-python/main.py
67-
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
68-
def greet_python(rust_var)
62+
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
63+
def greet_python(rust_var):
6964
return str(rust_var) + " from python"
7065
```
7166
- 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'`
7469
- add `outputEl.textContent = await callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value`
7570

7671
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
8580
- add file `src-tauri/src-python/main.py` and add python code, for example:
8681
```python
8782
# src-tauri/src-python/main.py
88-
def greet_python(rust_var)
83+
def greet_python(rust_var):
8984
return str(rust_var) + " from python"
9085
```
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:
9388
```javascript
9489
import { callFunction } from 'tauri-plugin-python-api'
9590
console.log(await callFunction("greet_python", ["input value"]))
9691
```
97-
-> 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.
9893

99-
Alternatively, to have more readable code:
94+
Alternatively, to have more readable code:
10095
```javascript
10196
import { call, registerJs } from 'tauri-plugin-python-api'
10297
registerJs("greet_python");
@@ -105,36 +100,38 @@ console.log(await call.greet_python("input value"));
105100

106101
## Deployment
107102

108-
You either need to have python installed on the target machine or ship the shared
109-
python library with your package. You also may link the python library statically - PyO3
110-
may do this by default if it finds a static python library. In addition, you need
111-
to copy the python files so that python files are next to the binary.
103+
You either need to have python installed on the target machine or ship the shared
104+
python library with your package. You also may link the python library statically - PyO3
105+
may do this by default if it finds a static python library. In addition, you need
106+
to copy the python files so that python files are next to the binary.
112107

113-
The file `src-python/main.py` is required for the plugin to work correctly.
114-
You may also add additional python files or use a venv environment.
115-
The included resources can be configurable in the `tauri.conf.json` file.
108+
The file `src-python/main.py` is required for the plugin to work correctly.
109+
You may also add additional python files or use a venv environment.
110+
The included resources can be configurable in the `tauri.conf.json` file.
116111

117-
Check the tauri and PyO3 documentation for additional info.
112+
Check the tauri and PyO3 documentation for additional info.
118113

119114
## Security considerations
120-
By default, this plugin cannot call arbitrary python code. Python functions can only be called if registered from rust during plugin initialization.
115+
116+
By default, this plugin cannot call arbitrary python code. Python functions can only be called if registered from rust during plugin initialization.
121117
It may still be possible to read values from python. This can be prevented via additional tauri permissions.
122118

123-
Keep in mind that this plugin could make it possible to run arbitrary python code when using all allow permissions.
124-
It is therefore highly recommended to **make sure the user interface is not accessible by a network URL** in production.
119+
Keep in mind that this plugin could make it possible to run arbitrary python code when using all allow permissions.
120+
It is therefore highly recommended to **make sure the user interface is not accessible by a network URL** in production.
125121

126-
The "runPython" command is disabled by default via permissions. If enabled, it is possible to
127-
inject python code directly via javascript.
128-
Also, the function "register" is disabled by default. If enabled, it can
129-
add control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code.
122+
The "runPython" command is disabled by default via permissions. If enabled, it is possible to
123+
inject python code directly via javascript.
124+
Also, the function "register" is disabled by default. If enabled, it can
125+
add control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code.
130126
Both functions can be enabled during development for rapid prototyping.
131127

132128
## Alternatives
133-
If 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).
129+
130+
If you 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).
134131
It is a different approach to have all tauri functionality completely in python.
135132

136-
This approach here with tauri-plugin-python is more lightweight and it is for you, if you
137-
- still want to write rust code
138-
- already have a tauri application and just need a specific python library
139-
- just want to simply support rare custom plugins
140-
- if you want to embed python code directly in your javascript
133+
This approach here with tauri-plugin-python is more lightweight and it is for you, if you
134+
- still want to write rust code
135+
- already have a tauri application and just need a specific python library
136+
- just want to simply support rare custom plugins
137+
- want to embed python code directly in your javascript

0 commit comments

Comments
 (0)