44// git clone https://github.com/marcomq/tauri-plugin-python
55
66use tauri:: {
7- plugin:: { Builder , TauriPlugin } , AppHandle , Manager , Runtime
7+ plugin:: { Builder , TauriPlugin } ,
8+ Manager , Runtime ,
89} ;
910
10- pub use models:: * ;
11-
1211#[ cfg( desktop) ]
1312mod desktop;
1413#[ cfg( mobile) ]
@@ -17,21 +16,50 @@ mod mobile;
1716mod commands;
1817mod error;
1918mod models;
19+ #[ cfg( not( feature = "pyo3" ) ) ]
2020mod py_lib;
21+ #[ cfg( feature = "pyo3" ) ]
22+ mod py_lib_pyo3;
2123
2224pub use error:: { Error , Result } ;
25+ use models:: * ;
2326
24- pub struct Python < R : Runtime > ( AppHandle < R > ) ;
27+ #[ cfg( desktop) ]
28+ use desktop:: Python ;
29+ #[ cfg( mobile) ]
30+ use mobile:: Python ;
2531
2632/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the python APIs.
2733pub trait PythonExt < R : Runtime > {
2834 fn python ( & self ) -> & Python < R > ;
35+ fn run_python ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > ;
36+ fn register_function ( & self , payload : RegisterRequest ) -> crate :: Result < StringResponse > ;
37+ fn call_function ( & self , payload : RunRequest ) -> crate :: Result < StringResponse > ;
38+ fn read_variable ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > ;
2939}
3040
3141impl < R : Runtime , T : Manager < R > > crate :: PythonExt < R > for T {
3242 fn python ( & self ) -> & Python < R > {
3343 self . state :: < Python < R > > ( ) . inner ( )
3444 }
45+ fn run_python ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > {
46+ py_lib:: run_python ( payload) ?;
47+ Ok ( StringResponse { value : "Ok" . into ( ) } )
48+ }
49+ fn register_function ( & self , payload : RegisterRequest ) -> crate :: Result < StringResponse > {
50+ py_lib:: register_function ( payload) ?;
51+ Ok ( StringResponse { value : "Ok" . into ( ) } )
52+ }
53+ fn call_function ( & self , payload : RunRequest ) -> crate :: Result < StringResponse > {
54+ let py_res = py_lib:: call_function ( payload) ?;
55+ Ok ( StringResponse {
56+ value : py_res. to_string ( ) ,
57+ } )
58+ }
59+ fn read_variable ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > {
60+ let py_res = py_lib:: read_variable ( payload) ?;
61+ Ok ( StringResponse { value : py_res } )
62+ }
3563}
3664
3765/// Initializes the plugin.
@@ -50,30 +78,9 @@ pub fn init<R: Runtime>(python_functions: Vec<&'static str>) -> TauriPlugin<R> {
5078 let python = desktop:: init ( app, api) ?;
5179 app. manage ( python) ;
5280 for function_name in python_functions {
53- py_lib:: register_function_str ( function_name. into ( ) , None ) ? ;
81+ py_lib:: register_function_str ( function_name. into ( ) , None ) . unwrap ( ) ;
5482 }
5583 Ok ( ( ) )
5684 } )
5785 . build ( )
5886}
59-
60- impl < R : Runtime > Python < R > {
61- pub fn run_python ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > {
62- py_lib:: run_python ( payload) ?;
63- Ok ( StringResponse { value : "Ok" . into ( ) } )
64- }
65- pub fn register_function ( & self , payload : RegisterRequest ) -> crate :: Result < StringResponse > {
66- py_lib:: register_function ( payload) ?;
67- Ok ( StringResponse { value : "Ok" . into ( ) } )
68- }
69- pub fn call_function ( & self , payload : RunRequest ) -> crate :: Result < StringResponse > {
70- let py_res = py_lib:: call_function ( payload) ?;
71- Ok ( StringResponse {
72- value : py_res. to_string ( ) ,
73- } )
74- }
75- pub fn read_variable ( & self , payload : StringRequest ) -> crate :: Result < StringResponse > {
76- let py_res = py_lib:: read_variable ( payload) ?;
77- Ok ( StringResponse { value : py_res } )
78- }
79- }
0 commit comments