Using tauri plugins¶
The Tauri team and community have developed some plugins, you can use them by:
- Official Tauri plugins usually provide corresponding JavaScript APIs, which you can use directly on the frontend.
- Write your own Rust functions using pyo3 and expose them to Python.
PyTauri has provided Python APIs for some Tauri plugins using the second method, and they are called pytauri-plugin-*
.
Below, we use pytauri-plugin-notification
as an example to demonstrate how to use pytauri plugins.
Using the plugin¶
install tauri plugin¶
All PyTauri plugins are just Python bindings, which means you need to initialize the underlying Tauri extensions normally:
expose the pyo3 bingings to python¶
PyTauri plugins usually export their pyo3 API with the following conventions:
- Export a Rust
mod
with the same name as the plugin at the top level. - Export a function named
pymodule_export
at the top level.- The pyo3 API of
pytauri
itself is exported in this way:pytauri::pymodule_export
- The pyo3 API of
pytauri-plugin-notification
uses the first method.
Add the rust dependency:
ref to https://pyo3.rs/v0.23.3/module.html to export the pyo3 bindings:
use pyo3::prelude::*;
// ...
#[pymodule(gil_used = false)]
#[pyo3(name = "ext_mod")]
pub mod ext_mod {
#[pymodule_export]
use pytauri_plugin_notification::notification;
// ...
}
use plugin api from python¶
Add the python dependency:
# ...
[project]
# ...
dependencies = [
# ...
"pytauri-plugin-notification == 0.1.*",
]
Tip
After adding dependencies, you need to use commands like uv sync
or uv pip install
to synchronize your dependency environment.
The PyTauri API maps very well to the original Rust API of the plugin. You can refer to the Rust documentation to understand how to use it:
import sys
from pydantic import BaseModel
from pytauri import AppHandle, Commands
from pytauri_plugin_notification import NotificationBuilderArgs, NotificationExt
commands: Commands = Commands()
class Person(BaseModel):
name: str
class Greeting(BaseModel):
message: str
@commands.command()
async def greet(body: Person, app_handle: AppHandle) -> Greeting:
notification_builder = NotificationExt.builder(app_handle)
notification_builder.show(
NotificationBuilderArgs(title="Greeting", body=f"Hello, {body.name}!")
)
return Greeting(
message=f"Hello, {body.name}! You've been greeted from Python {sys.version}!"
)