Skip to content

Commit 81d5d06

Browse files
committed
Merge remote-tracking branch 'origin/main' into rustpython
2 parents d90937a + 90229ab commit 81d5d06

1 file changed

Lines changed: 61 additions & 7 deletions

File tree

README.md

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ Android and iOS might also be able to run PyO3 in theory but require to have CPy
2929
to be compiled for the target platform. I still need to figure out how to
3030
cross compile python and PyO3 for iOS and Android. Ping me if you know how to do that.
3131

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.
3637

3738
## Example app
3839

@@ -41,6 +42,10 @@ Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugi
4142

4243

4344
## 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+
4449
- run `npm run tauri add python`
4550
- 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"`
4651
- 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.
5459

5560
Tauri events and calling js from python is currently not supported yet. You would need to use rust for that.
5661

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+
def greet_python(rust_var)
71+
print(rust_var)
72+
return str(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:
76+
```javascript
77+
import { callFunction } from 'tauri-plugin-python-api'
78+
console.log(await callFunction("greet_python", ["input value"]))
79+
```
80+
-> 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.
81+
82+
Alternatively, to have more readable code:
83+
```javascript
84+
import { call, registerJs } from 'tauri-plugin-python-api'
85+
registerJs("greet_python");
86+
console.log(await call.greet_python("input value"));
87+
```
88+
89+
## Deployment
90+
91+
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+
57102
## 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.
59104

60105
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.
62107

63108
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.
65110
Also, the function "register" is disabled by default. If enabled, it can
66111
add control from javascript which functions can be called. This avoids to modify rust code when changing or adding python code.
67112
Both functions can be enabled during development for rapid prototyping.
68113

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

Comments
 (0)