Skip to content

Commit a8e6730

Browse files
committed
resolve clippy issues and add register to python source
1 parent 8f408d1 commit a8e6730

File tree

9 files changed

+31
-17
lines changed

9 files changed

+31
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tauri-plugin-python"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = [ "Marco Mengelkoch" ]
55
description = "A tauri 2 plugin to use python code in the backend."
66
keywords = ["rust", "python", "tauri", "gui"]

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ Javascript in [examples/plain-javascript](https://github.com/marcomq/tauri-plugi
5353
These steps assume that you already have a basic tauri application available. Alternatively, you can immediately start with the application in "example" directory.
5454

5555
- run `npm run tauri add python`
56-
- 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"`
57-
- modify `src-tauri/src/lib.rs` and change `.plugin(tauri_plugin_python::init())` to `.plugin(tauri_plugin_python::init_and_register(["greet_python"]))`; make sure you list all python functions you
58-
want to call
56+
- add `src-tauri/src-python/main.py` and modify it acording to your needs, for example add
57+
```python
58+
# src-tauri/src-python/main.py
59+
_tauri_plugin_functions = ["greet_python"] # make "greet_python" callable from UI
60+
def greet_python(rust_var)
61+
return str(rust_var) + " from python"
62+
```
5963
- add `"bundle": {"resources": [ "src-python/**/*"],` to `tauri.conf.json` so that python files are bundled with your application
6064
- add the plugin in your js, so
6165
- add `import { callFunction } from 'tauri-plugin-python-api'`
@@ -74,7 +78,6 @@ Tauri events and calling js from python is currently not supported yet. You woul
7478
```python
7579
# src-tauri/src-python/main.py
7680
def greet_python(rust_var)
77-
print(rust_var)
7881
return str(rust_var) + " from python"
7982
```
8083
- 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.

examples/plain-javascript/src-tauri/src-python/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# auto loaded on startup
1+
# File auto loaded on startup
2+
_tauri_plugin_functions = ["greet_python"] # make these functions callable from UI
23

34
counter = 0
45

examples/plain-javascript/src-tauri/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn greet_rust(name: &str) -> String {
88
pub fn run() {
99
tauri::Builder::default()
1010
.invoke_handler(tauri::generate_handler![greet_rust])
11-
.plugin(tauri_plugin_python::init_and_register(vec!["greet_python"]))
11+
.plugin(tauri_plugin_python::init())
1212
.run(tauri::generate_context!())
1313
.expect("error while running tauri application");
1414
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tauri-plugin-python-api",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"author": "Marco Mengelkoch",
55
"description": "Javascript package for tauri 2 python plugin.",
66
"type": "module",

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ impl From<PyErr> for Error {
5555

5656
impl From<tauri::Error> for Error {
5757
fn from(error: tauri::Error) -> Self {
58-
error.into()
58+
Error::String(error.to_string())
5959
}
6060
}

src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<R: Runtime, T: Manager<R>> crate::PythonExt<R> for T {
5454
Ok(StringResponse { value: "Ok".into() })
5555
}
5656
fn call_function(&self, payload: RunRequest) -> crate::Result<StringResponse> {
57-
let py_res: String = py_lib::call_function(payload)?.into();
57+
let py_res: String = py_lib::call_function(payload)?;
5858
Ok(StringResponse { value: py_res })
5959
}
6060
fn read_variable(&self, payload: StringRequest) -> crate::Result<StringResponse> {
@@ -115,6 +115,17 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
115115
for function_name in python_functions {
116116
py_lib::register_function_str(function_name.into(), None).unwrap();
117117
}
118+
let functions = py_lib::read_variable(StringRequest {
119+
value: "_tauri_plugin_functions".into(),
120+
})
121+
.unwrap_or_default().replace("'","\"");
122+
123+
// dbg!(&functions);
124+
if let Ok(python_functions) = serde_json::from_str::<Vec<String>>(&functions) {
125+
for function_name in python_functions {
126+
py_lib::register_function_str(function_name.into(), None).unwrap();
127+
}
128+
}
118129
Ok(())
119130
})
120131
.build()

src/py_lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ if len(signature({}).parameters) != {}:
8585
"Number of args doesn't match signature of {fn_name}."
8686
));
8787
}
88+
// dbg!(format!("Added '{fn_name}'"));
8889
FUNCTION_MAP.lock().unwrap().insert(fn_name);
8990
Ok(())
9091
})
@@ -117,10 +118,8 @@ pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
117118
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
118119
let res = GLOBALS
119120
.globals
120-
.get_item(&payload.value, vm)
121-
.unwrap()
122-
.str(vm)
123-
.unwrap()
121+
.get_item(&payload.value, vm)?
122+
.str(vm)?
124123
.to_string();
125124
Ok(res)
126125
})

src/py_lib_pyo3.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> cra
5858
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);
5959

6060
let fn_dot_split: Vec<&str> = fn_name.split(".").collect();
61-
let app = globals.get_item(&fn_dot_split[0])?;
61+
let app = globals.get_item(fn_dot_split[0])?;
6262
if app.is_none() {
6363
return Err(Error::String(format!("{} not found", &fn_name)));
6464
}
@@ -84,7 +84,7 @@ if len(signature({}).parameters) != {}:
8484
);
8585
let code_c = CString::new(py_analyze_sig).expect("CString::new failed");
8686
py.run(&code_c, Some(&globals), None)
87-
.expect(&format!("Could not register '{}'. ", &fn_name));
87+
.unwrap_or_else(|_| panic!("Could not register '{}'. ", &fn_name));
8888
}
8989
// dbg!("{} was inserted", &fn_name);
9090
FUNCTION_MAP.lock().unwrap().insert(fn_name, app.into());
@@ -118,7 +118,7 @@ pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
118118
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);
119119

120120
let var_dot_split: Vec<&str> = payload.value.split(".").collect();
121-
let var = globals.get_item(&var_dot_split[0])?;
121+
let var = globals.get_item(var_dot_split[0])?;
122122
if let Some(var) = var {
123123
if var_dot_split.len() > 1 {
124124
Ok(var.getattr(var_dot_split.get(1).unwrap())?.to_string())

0 commit comments

Comments
 (0)