pyo3/examples/plugin
Matthew Neeley 68ec6de0c9
Use single-arg form of #[pymodule] function in docs and tests (#3899)
* Use single-arg form for `#[pymodule]` functions in docs and tests

* Update guide/src/function.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Add test of two-argument module function

* Fix new test

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
2024-02-28 22:36:50 +00:00
..
.template release: 0.20.3 (#3890) 2024-02-23 12:30:38 +00:00
plugin_api Use single-arg form of #[pymodule] function in docs and tests (#3899) 2024-02-28 22:36:50 +00:00
python_plugin added a plugin example that shows how to integrate a Python plugin into a Rust app while having option to test API without the main app 2023-01-27 08:34:09 +00:00
src add PyModule::new_bound and PyModule::import_bound (#3775) 2024-02-22 09:35:47 +00:00
.DS_Store add cargo-generate template for plugin example 2023-01-27 08:34:09 +00:00
cargo-generate.toml add cargo-generate template for plugin example 2023-01-27 08:34:09 +00:00
Cargo.toml added a plugin example that shows how to integrate a Python plugin into a Rust app while having option to test API without the main app 2023-01-27 08:34:09 +00:00
README.md added a plugin example that shows how to integrate a Python plugin into a Rust app while having option to test API without the main app 2023-01-27 08:34:09 +00:00

plugin

An example of a Rust app that uses Python for a plugin. A Python extension module built using PyO3 and maturin is used to provide interface types that can be used to exchange data between Rust and Python. This also deals with how to separately test and load python modules.

Building and Testing

Host application

To run the app itself, you only need to run

cargo run

It will build the app, as well as the plugin API, then run the app, load the plugin and show it working.

Plugin API testing

The plugin API is in a separate crate plugin_api, so you can test it separately from the main app.

To build the API only package, first install maturin:

pip install maturin

When building the plugin, simply using maturin develop will fail to produce a viable extension module due to the features arrangement of PyO3. Instead, one needs to enable the optional feature as follows:

cd plugin_api
maturin build --features "extension-module"

Alternatively, install nox and run the tests inside an isolated environment:

nox

Copying this example

Use cargo-generate:

$ cargo install cargo-generate
$ cargo generate --git https://github.com/PyO3/pyo3 examples/plugin

(cargo generate will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.)