fusion-engineering / inline-python

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

Can I call Rust again from the python inline #23

Closed hyousefGopher closed 4 years ago

hyousefGopher commented 4 years ago

Can I do something like below, so that I create a Rust block inside the Python block !

image

m-ou-se commented 4 years ago

Nope.

m-ou-se commented 4 years ago

The just-published 0.5.1 version allows you to do this: https://github.com/fusion-engineering/inline-python/blob/9c1d04fee1f5fb71bde2356d91b3f2cea81134b2/examples/rust-fn.rs

hyousefGopher commented 4 years ago

I got the below for the code in the link:

D:\rust_webview>cargo run
   Compiling rust_webview v0.1.0 (D:\rust_webview)
error[E0432]: unresolved import `pyo3`
 --> src\main.rs:6:1
  |
6 | #[pyfunction]
  | ^^^^^^^^^^^^^ help: a similar path exists: `inline_python::pyo3`
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type or module `pyo3`
  --> src\main.rs:14:16
   |
14 |     c.add_wrapped(wrap_pyfunction!(rust_print));
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type or module `pyo3`
   |
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type or module `pyo3`
 --> src\main.rs:6:1
  |
6 | #[pyfunction]
  | ^^^^^^^^^^^^^ use of undeclared type or module `pyo3`
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors
m-ou-se commented 4 years ago

Make sure pyo3 is imported, (if your crate doesn't depend on pyo3 directly):

use inline_python::pyo3;
hyousefGopher commented 4 years ago

Thanks a lot, it is ok now, may you need to add it in the code, so no one else come with the same issue again :)

m-ou-se commented 4 years ago

Already did: https://github.com/fusion-engineering/inline-python/commit/4afa5bbfe48b82b6d1140de0a343fa423f902655 :)