Skip to content

Commit 8b05d50

Browse files
authored
Update README.md
1 parent c480d14 commit 8b05d50

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,41 @@ or you manually need to ship the shared libs together with the installer package
3333
There is a sample Desktop application for Windows/Linux/MacOS using this plugin and vanilla
3434
Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugin-python/tree/main/examples/plain-javascript)
3535

36+
## Manual plugin installation / usage
37+
38+
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the example application.
39+
40+
- `$ cargo add tauri-plugin-python`
41+
- `$ npm install tauri-plugin-python-api`
42+
- modify `permissions:[]` in src-tauri/capabilities/default.json and add "python:default"
43+
- add file `src-tauri/src-python/main.py` and add python code, for example:
44+
```python
45+
# src-tauri/src-python/main.py
46+
def greet_python(rust_var)
47+
print(rust_var)
48+
return str(rust_var) + " from python"
49+
```
50+
- 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.
51+
- 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:
52+
```javascript
53+
import { callFunction } from 'tauri-plugin-python-api'
54+
console.log(await callFunction("greet_python", ["input value"]))
55+
```
56+
-> 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.
57+
58+
Alternatively, to have more readable code:
59+
```javascript
60+
import { call, registerJs } from 'tauri-plugin-python-api'
61+
registerJs("greet_python");
62+
console.log(await call.greet_python("input value"));
63+
```
64+
65+
## Deployment
66+
67+
You either need to have python installed on the target machine or ship the shared python library with your package. You also may link the python library statically - PyO3 may do this by default if it finds a static python library. In addition, you need to copy the python files so that python files are next to the binary. The file `src-python/main.py` is required for the plugin to work correctly. You may also add additional python files or use a venv environment. The included resources can be configurable in the `tauri.conf.json` file. Check the tauri and PyO3 documentation for additional info.
68+
3669
## Security considerations
37-
Generally, this plugin has been created by "security by default" concept. Python functions can onl be called if registered from rust.
70+
Generally, this plugin has been created by "security by default" concept. Python functions can only be called if registered from rust.
3871

3972
Keep in mind that this plugin can make it possible to run arbitrary python code.
4073
It is therefore highly recommended to **not make the user interface accessible by a network URL**.

0 commit comments

Comments
 (0)