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
Copy file name to clipboardExpand all lines: README.md
+61-7Lines changed: 61 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,10 +29,11 @@ Android and iOS might also be able to run PyO3 in theory but require to have CPy
29
29
to be compiled for the target platform. I still need to figure out how to
30
30
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
31
31
32
-
You might use this plugin to create simple prototype applications
33
-
and later re-write functions in rust to improve
34
-
performance, add a specific rust library or just call some
35
-
low-level code.
32
+
You can use this plugin for fast prototypes or for production code.
33
+
It might be possible that you want to use some python library or code that
34
+
is not available for rust yet.
35
+
In case that you want to ship production software packages, you just need
36
+
to make sure to also ship the python code. If you use PyO3, you also need to ship libpython too.
36
37
37
38
## Example app
38
39
@@ -41,6 +42,10 @@ Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugi
41
42
42
43
43
44
## Add the plugin to an existing tauri application
45
+
46
+
47
+
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the example application.
48
+
44
49
- run `npm run tauri add python`
45
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"`
46
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
@@ -54,15 +59,64 @@ Check the examples for alternative function calls and code sugar.
54
59
55
60
Tauri events and calling js from python is currently not supported yet. You would need to use rust for that.
56
61
62
+
## Alternative manual plugin installation
63
+
64
+
-`$ cargo add tauri-plugin-python`
65
+
-`$ npm install tauri-plugin-python-api`
66
+
- modify `permissions:[]` in src-tauri/capabilities/default.json and add "python:default"
67
+
- add file `src-tauri/src-python/main.py` and add python code, for example:
68
+
```python
69
+
# src-tauri/src-python/main.py
70
+
defgreet_python(rust_var)
71
+
print(rust_var)
72
+
returnstr(rust_var) +" from python"
73
+
```
74
+
- add `.plugin(tauri_plugin_python::init(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.
75
+
- 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:
-> 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.
You either need to have python installed on the target machine or ship the shared
92
+
python library with your package. You also may link the python library statically - PyO3
93
+
may do this by default if it finds a static python library. In addition, you need
94
+
to copy the python files so that python files are next to the binary.
95
+
96
+
The file `src-python/main.py` is required for the plugin to work correctly.
97
+
You may also add additional python files or use a venv environment.
98
+
The included resources can be configurable in the `tauri.conf.json` file.
99
+
100
+
Check the tauri and PyO3 documentation for additional info.
101
+
57
102
## Security considerations
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.
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.
59
104
60
105
Keep in mind that this plugin could make it possible to run arbitrary python code.
61
-
It is therefore highly recommended to **not make the user interface accessible by a network URL**.
106
+
It is therefore highly recommended to **make sure the user interface is not accessible by a network URL** in production.
62
107
63
108
The "runPython" command is disabled by default via permissions. If enabled, it is possible to
64
-
inject python code via javascript.
109
+
inject python code directly via javascript.
65
110
Also, the function "register" is disabled by default. If enabled, it can
66
111
add control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code.
67
112
Both functions can be enabled during development for rapid prototyping.
68
113
114
+
## Alternatives
115
+
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).
116
+
It is a different approach to have all tauri functionality completely in python.
117
+
118
+
This approach here with tauri-plugin-python is more lightweight and it is for you, if you
119
+
- still want to write rust code
120
+
- already have a tauri application and just need a specific python library
121
+
- just want to simply support rare custom plugins
122
+
- if you want to embed python code directly in your javascript
0 commit comments