Skip to content

Commit b2dfa46

Browse files
committed
working on windows - read python code during startup
1 parent c6352a8 commit b2dfa46

3 files changed

Lines changed: 7 additions & 15 deletions

File tree

build.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

6-
use std::path::Path;
7-
86
const COMMANDS: &[&str] = &[
97
"run_python",
108
"register_function",
@@ -13,14 +11,6 @@ const COMMANDS: &[&str] = &[
1311
];
1412

1513
fn main() {
16-
let py_main = concat!(env!("PWD"), "/src-tauri/src-python/main.py");
17-
let py_main_path = Path::new(py_main);
18-
if !py_main_path.exists() {
19-
let parent = py_main_path.parent().unwrap_or(py_main_path);
20-
std::fs::create_dir_all(parent).unwrap_or_default();
21-
std::fs::write(py_main_path, "# auto loaded on starts").unwrap_or_default();
22-
}
23-
2414
tauri_plugin::Builder::new(COMMANDS)
2515
// .global_api_script_path("./api-iife.js")
2616
.android_path("android")

src/py_lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ lazy_static! {
2323
}
2424
pub fn init_python() -> PyResult<()> {
2525
pyo3::prepare_freethreaded_python();
26-
let code = py_main_import::read_at_compile_time();
26+
let code = py_main_import::read_at_startup();
27+
dbg!(&code);
2728
let c_code = CString::new(code).expect("error loading python");
2829
marker::Python::with_gil(|py| -> PyResult<()> {
2930
let globals = GLOBALS.lock().unwrap().clone_ref(py).into_bound(py);

src/py_main_import.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// © Copyright 2024, by Marco Mengelkoch
33
// Licensed under MIT License, see License file for more details
44
// git clone https://github.com/marcomq/tauri-plugin-python
5+
use std::env;
56

6-
pub fn read_at_compile_time<'a>() -> &'a str {
7-
// no actual error, file auto generated in build.rs, no idea how to ignore this
8-
// moved to separate file to not ignore other errors
9-
include_str!(concat!(env!("PWD"), "/", "src-tauri/src-python/main.py"))
7+
pub fn read_at_startup<'a>() -> String {
8+
let py_file_path = env::current_dir().unwrap().join("src-python/main.py");
9+
std::fs::read_to_string(py_file_path).unwrap_or_default()
10+
// include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
1011
}

0 commit comments

Comments
 (0)