m-ou-se / inline-python

Inline Python code directly in your Rust code
https://docs.rs/inline-python
BSD 2-Clause "Simplified" License
1.16k stars 40 forks source link

how can I call a callback function or a hook function in "python!" Marco? #15

Closed panicfrog closed 4 years ago

panicfrog commented 4 years ago
#[pyfunction]
fn rust_hook(s: ProcessStatus) -> PyResult<()> {
    println!("rust func {:?}", s);
    Ok(())
}

fn main() {
    python! {
        import youtube_dl
        ydl_opts = {
            "progress_hooks": [rust_hook],
            "proxy": "https://127.0.0.1:1087"
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            res = ydl.download(["https://youtu.be/HyLSpU0u1qo"])
    }
}

In my scene,I want to download a YouTube video with 'YouTube-dl', and get the status in the progress. how can I call a rust hook function in python! Marco?

panicfrog commented 4 years ago

and then I think, mybe I can export a lib to import that package in python! Marco, and got a error to build the lib.

   Compiling inline-python v0.4.0
error: dlopen(/Users/yeyongping/Source/Rust/rust-daily/dl/ytb-dl/target/release/deps/libinline_python_macros-5fd842f914c2952f.dylib, 1): Symbol not found: _PyBaseObject_Type
  Referenced from: /Users/yeyongping/Source/Rust/rust-daily/dl/ytb-dl/target/release/deps/libinline_python_macros-5fd842f914c2952f.dylib
  Expected in: flat namespace
 in /Users/yeyongping/Source/Rust/rust-daily/dl/ytb-dl/target/release/deps/libinline_python_macros-5fd842f914c2952f.dylib
  --> /Users/yeyongping/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/inline-python-0.4.0/src/lib.rs:94:9
   |
94 | pub use inline_python_macros::python;
   |         ^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: could not compile `inline-python`.
m-ou-se commented 4 years ago

This is now possible using Context::add_wrapped.