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: https://github.com/pytauri/pytauri/discussions/45#discussioncomment-11870767
We encourage you to distribute plugins written in this way to benefit the entire community 💪.
In addition, PyTauri has already integrated some official Tauri plugins. Below, we use tauri-plugin-notification as an example to demonstrate how to use pytauri-integrated plugins.
All plugins we support¶
Using the plugin¶
Install tauri plugin¶
All PyTauri plugins are just Python bindings, which means you need to initialize the underlying Tauri extensions normally:
The above command will perform the following steps (which you can also do manually):
- Add
tauri-plugin-notification
as a Rust dependency inCargo.toml
. -
Add
@tauri-apps/plugin-notification
as a frontend dependency inpackage.json
This step is optional
If you only use the global API (
window.__TAURI__.notification
) on the frontend, you can skip this step. -
Register the core plugin with tauri
src-tauri/src/lib.rs// i.e., `builder_factory` function of python binding |_args, _kwargs| { let builder = tauri::Builder::default() .plugin(tauri_plugin_notification::init()); // 👈 Ok(builder) },
registering plugin from python
Since pytauri
v0.8
, you can also register plugins directly from your Python code:src-tauri/python/tauri_app/__init__.pyfrom pytauri import ( builder_factory, context_factory, ) from pytauri_plugins import notification app = builder_factory().build( context=context_factory(), invoke_handler=None, plugins=[notification.init()], # 👈 )
This depends on your preference, but for pytauri_wheel, plugins can only be registered in this way (since you don't have access to the Rust code).
-
Add the permissions to your capabilities file
Expose the pyo3 bindings to python¶
Enable the pytauri
feature:
[dependencies]
# ...
-pytauri = { version = "*" }
+pytauri = { version = "*", features = ["plugin-notification"] }
Use plugin API from python¶
The PyTauri API maps very well to the original Rust API of the plugin. You can refer to the plugin-notification, Js docs, Rust docs and Python docs to understand how to use it:
Tip
pytauri_plugins
is distributed as part of the pytauri
package on PyPI.
Therefore, running pip install pytauri
will also install it.
import sys
from pydantic import BaseModel
from pytauri import AppHandle, Commands
from pytauri_plugins.notification import 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(title="Greeting", body=f"Hello, {body.name}!")
return Greeting(
message=f"Hello, {body.name}! You've been greeted from Python {sys.version}!"
)