Skip to content

Commit 9f6a368

Browse files
committed
update readme
1 parent 145e645 commit 9f6a368

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,63 @@
11
# Tauri Plugin Python
22

33
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+
512
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
715
and can get called during application workflow.
816

917

1018
| Platform | Supported |
1119
| -------- | --------- |
1220
| Linux ||
1321
| Windows ||
14-
| macOS ||
15-
| Android | not yet |
16-
| iOS | not yet |
22+
| MacOS ||
23+
| Android | * |
24+
| iOS | * |
1725

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

1932
You might use this plugin to create simple prototype applications
2033
and later re-write functions in rust to improve
2134
performance, add a specific rust library or just call some
2235
low-level code.
2336

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.
30-
3137
## Example app
3238

3339
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)
40+
Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript).
41+
42+
43+
## Add the plugin to an existing tauri application
44+
- run `npm run tauri add python`
45+
- 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+
- 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
47+
want to call
48+
- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application
49+
- add the plugin in your js, so
50+
- add `import { callFunction } from 'tauri-plugin-python-api'`
51+
- add `outputEl.textContent = await tauri.python.callFunction("greet_python", [value])` to get the output of the python function `greet_python` with parameter of js variable `value`
52+
53+
Check the examples for alternative function calls and code sugar.
54+
55+
Tauri events and calling js from python is currently not supported yet. You would need to use rust for that.
3556

3657
## Security considerations
37-
Generally, this plugin has been created by "security by default" concept. Python functions can onl be called if registered from rust.
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.
3859

39-
Keep in mind that this plugin can make it possible to run arbitrary python code.
60+
Keep in mind that this plugin could make it possible to run arbitrary python code.
4061
It is therefore highly recommended to **not make the user interface accessible by a network URL**.
4162

4263
The "runPython" command is disabled by default via permissions. If enabled, it is possible to

examples/plain-javascript/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ and Vanilla Javascript
1010
- run `npm install` in the example path (`cd examples/plain-javascript`)
1111
- run `npm run tauri dev` to start the application
1212

13+
To run this sample app on iOS:
14+
- run `npx @tauri-apps/cli plugin ios init` to init ios project files
15+
- run `npm run tauri ios dev` to start the application on iOS in develop mode
16+
1317
## Manual modifications on default template to add plugin:
1418
- add `tauri-plugin-python` to Cargo.toml
1519
- add `tauri-plugin-python-api` to package.json
1620
- modify `permissions:[]` in src-tauri/capabilities/default.json and add "python:default"
1721
- modify `src-tauri/src-python/main.py` and add python code, for example `def greet_python(..`
18-
- add `.plugin(tauri_plugin_python::init())` to `src-tauri/src/lib.rs`
22+
- add `.plugin(tauri_plugin_python::init(["greet_python"]))` to `src-tauri/src/lib.rs`
1923
- include javascript for python plugin in the index.html file for example by adding `<script type="module" src="/tauri-plugin-python-api/index.iife.js" defer></script>`
20-
- register python functions in javascript by calling `registerFunction("greet_python");`
24+
- register python functions in javascript by calling `registerJs("greet_python");`
2125
- calling python function by calling `call.greet_python(...)`
2226

2327
## Recommended IDE Setup

0 commit comments

Comments
 (0)