pytauri.ffi
¶
Original FFI interface module.
Warning
All APIs under this module should not be considered stable. You should use the re-exported APIs under the top-level module.
Modules:
Name | Description |
---|---|
image |
|
ipc |
|
lib |
|
menu |
|
path |
|
tray |
|
webview |
|
window |
Classes:
Name | Description |
---|---|
App |
|
AppHandle |
|
Assets |
|
Builder |
|
BuilderArgs |
|
Context |
|
Emitter |
|
Event |
|
EventTarget |
|
Listener |
|
Manager |
|
Position |
|
Rect |
|
RunEvent |
|
Size |
Functions:
Name | Description |
---|---|
builder_factory |
A factory function for creating a |
context_factory |
A factory function for creating a |
Attributes:
Name | Type | Description |
---|---|---|
EXT_MOD |
ModuleType
|
The extension module of |
EventId |
||
EventTargetType |
See EventTarget for details. |
|
ImplEmitter |
|
|
ImplListener |
|
|
ImplManager |
|
|
PositionType |
See Position for details. |
|
RunEventType |
See RunEvent for details. |
|
SizeType |
See Size for details. |
|
Url |
EXT_MOD
module-attribute
¶
EXT_MOD: ModuleType
The extension module of pytauri
app.
It will be loaded from entry_points(group="pytauri", name="ext_mod")
.
Usually you don't need to use it, unless you want to write plugins for pytauri
.
EventTargetType
module-attribute
¶
EventTargetType = TypeAliasType('EventTargetType', Union[Any, AnyLabel, App, Window, Webview, WebviewWindow])
See EventTarget for details.
ImplManager
module-attribute
¶
ImplManager = TypeAliasType('ImplManager', Union[App, AppHandle, 'WebviewWindow'])
PositionType
module-attribute
¶
PositionType = TypeAliasType('PositionType', Union[Physical, Logical])
See Position for details.
RunEventType
module-attribute
¶
RunEventType = TypeAliasType('RunEventType', Union[Exit, ExitRequested, WindowEvent, WebviewEvent, Ready, Resumed, MainEventsCleared, MenuEvent, TrayIconEvent])
See RunEvent for details.
SizeType
module-attribute
¶
SizeType = TypeAliasType('SizeType', Union[Physical, Logical])
See Size for details.
__all__
module-attribute
¶
__all__ = ('EXT_MOD', 'App', 'AppHandle', 'Assets', 'Builder', 'BuilderArgs', 'Context', 'Emitter', 'Event', 'EventId', 'EventTarget', 'EventTargetType', 'ImplEmitter', 'ImplListener', 'ImplManager', 'Listener', 'Manager', 'Position', 'PositionType', 'Rect', 'RunEvent', 'RunEventType', 'Size', 'SizeType', 'Url', 'builder_factory', 'context_factory')
App
¶
Warning
This class is not thread-safe, and should not be shared between threads.
- You can only use it on the thread it was created on.
- And you need to ensure it is garbage collected on the thread it was created on, otherwise it will cause memory leaks.
Methods:
Name | Description |
---|---|
run |
Consume and run this app, will block until the app is exited. |
run_return |
Consume and run this application, returning its intended exit code. |
run_iteration |
Run this app iteratively without consuming it, calling |
cleanup_before_exit |
Runs necessary cleanup tasks before exiting the process. |
handle |
Get a handle to this app, which can be used to interact with the app from another thread. |
run
¶
Consume and run this app, will block until the app is exited.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Optional[_AppRunCallbackType]
|
a callback function that will be called on each event. It will be called on the same thread that the app was created on, so you should not block in this function. |
None
|
Note
This function will call std::process::exit
at the end to terminate the entire process,
which means the Python interpreter cannot be properly finalized.
If this is a problem for you, please use pytauri.App.run_return.
Warning
If callback
is specified, it must not raise an exception,
otherwise it is logical undefined behavior, and in most cases, the program will panic.
Source code in python/pytauri/src/pytauri/ffi/lib.py
run_return
¶
Consume and run this application, returning its intended exit code.
Warning
callback
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
run_iteration
deprecated
¶
Deprecated
When called in a loop (as suggested by the name), this function will busy-loop.
To re-gain control of control flow after the app has exited, use App::run_return
instead.
See https://docs.rs/tauri/latest/tauri/struct.App.html#method.run_iteration for more details.
Run this app iteratively without consuming it, calling callback
on each iteration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Optional[_AppRunCallbackType]
|
a callback function that will be called on each iteration. |
None
|
Warning
callback
has the same restrictions as App.run.
Tip
Approximately 2ms per calling in debug mode.
Source code in python/pytauri/src/pytauri/ffi/lib.py
cleanup_before_exit
¶
Runs necessary cleanup tasks before exiting the process.
You should always exit the tauri app immediately after this function returns and not use any tauri-related APIs.
AppHandle
¶
Methods:
Name | Description |
---|---|
run_on_main_thread |
Runs the given closure on the main thread. |
exit |
|
restart |
|
on_menu_event |
Registers a global menu event listener. |
on_tray_icon_event |
Registers a global tray icon menu event listener. |
tray_by_id |
|
remove_tray_by_id |
|
default_window_icon |
Returns the default window icon. |
menu |
|
set_menu |
|
remove_menu |
|
hide_menu |
|
show_menu |
|
invoke_key |
|
run_on_main_thread
¶
Runs the given closure on the main thread.
Warning
handler
has the same restrictions as App.run.
on_menu_event
¶
Registers a global menu event listener.
Warning
handler
has the same restrictions as App.run.
on_tray_icon_event
¶
on_tray_icon_event(handler: Callable[[Self, TrayIconEventType], None]) -> None
Registers a global tray icon menu event listener.
Warning
handler
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
tray_by_id
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
remove_tray_by_id
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
default_window_icon
¶
Returns the default window icon.
Warning
Each time you call this function, a new image instance will be created. So you should cache the result if you need to use it multiple times.
Source code in python/pytauri/src/pytauri/ffi/lib.py
menu
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
set_menu
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
remove_menu
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
hide_menu
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
show_menu
¶
Source code in python/pytauri/src/pytauri/ffi/lib.py
Assets
¶
Builder
¶
use builder_factory to instantiate this class.
Warning
This class is not thread-safe, and should not be shared between threads.
- You can only use it on the thread it was created on.
- And you need to ensure it is garbage collected on the thread it was created on, otherwise it will cause memory leaks.
Methods:
Name | Description |
---|---|
build |
Consume this builder and build an app with the given |
BuilderArgs
¶
Methods:
Name | Description |
---|---|
__new__ |
__new__
¶
__new__(context: Context, *, invoke_handler: Optional[_InvokeHandlerProto], setup: Optional[Callable[[AppHandle], object]] = None) -> Self
Warning
The implementer of invoke_handler must never raise an exception,
otherwise it is considered logical undefined behavior.
Additionally, invoke_handler
must not block.
Warning
If you do not specify invoke_handler,
pytauri
will not register the tauri-plugin-pytauri
plugin,
which means you cannot use pyInvoke
in the frontend to call Commands
(you will receive an error like "plugin pytauri not found").
If this is indeed the behavior you expect, explicitly pass None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Context
|
use context_factory to get it. |
required |
|
Optional[_InvokeHandlerProto]
|
use Commands to get it. |
required |
|
Optional[Callable[[AppHandle], object]]
|
see rust |
None
|
Source code in python/pytauri/src/pytauri/ffi/lib.py
Context
¶
Methods:
Name | Description |
---|---|
set_assets |
Use custom assets instead of the assets bundled by Tauri. |
set_assets
¶
set_assets(assets: Assets) -> None
Use custom assets instead of the assets bundled by Tauri.
To make this work:
- You need to enable the
tauri/custom-protocol
feature.- Or build using
tauri build
.
- Or build using
- Set
frontendDist
intauri.conf.json
to an empty directory (do not set it to a URL).
Source code in python/pytauri/src/pytauri/ffi/lib.py
Emitter
¶
Methods:
Name | Description |
---|---|
emit_str |
Similar to [ |
emit_str_to |
Similar to [ |
emit_str_filter |
Similar to [ |
emit_str
staticmethod
¶
emit_str(slf: ImplEmitter, event: str, payload: str) -> None
Similar to [Emitter::emit
] but the payload is json serialized.
emit_str_to
staticmethod
¶
emit_str_to(slf: ImplEmitter, target: EventTargetType, event: str, payload: str) -> None
Similar to [Emitter::emit_to
] but the payload is json serialized.
emit_str_filter
staticmethod
¶
emit_str_filter(slf: ImplEmitter, event: str, payload: str, filter: Callable[[EventTargetType], bool]) -> None
Similar to [Emitter::emit_filter
] but the payload is json serialized.
Warning
filter
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
Event
¶
EventTarget
¶
Classes:
Name | Description |
---|---|
Any |
Any and all event targets. |
AnyLabel |
Any |
App |
App and AppHandle targets. |
Window |
|
Webview |
Webview target. |
WebviewWindow |
WebviewWindow target. |
Any
¶
AnyLabel
¶
App
¶
Window
¶
Webview
¶
Listener
¶
See also: https://tauri.app/develop/calling-rust/#event-system
Examples¶
from pydantic import BaseModel
from pytauri import AppHandle, Event, Listener
class Payload(BaseModel): # or `RootModel`
url: str
num: int
def listen(app_handle: AppHandle) -> None:
def handler(event: Event):
assert event.id == event_id
serialized_event = Payload.model_validate_json(event.payload)
print(serialized_event.url, serialized_event.num)
event_id = Listener.listen(app_handle, "event_name", handler)
Methods:
Name | Description |
---|---|
listen |
Listen to an emitted event on this manager. |
once |
Listen to an event on this manager only once. |
unlisten |
Remove an event listener. |
listen_any |
Listen to an emitted event to any target. |
once_any |
Listens once to an emitted event to any target . |
listen
staticmethod
¶
listen(slf: ImplListener, event: str, handler: _EventHandlerType) -> EventId
Listen to an emitted event on this manager.
Warning
handler
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
once
staticmethod
¶
once(slf: ImplListener, event: str, handler: _EventHandlerType) -> EventId
Listen to an event on this manager only once.
Warning
handler
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
unlisten
staticmethod
¶
unlisten(slf: ImplListener, id: EventId) -> None
listen_any
staticmethod
¶
listen_any(slf: ImplListener, event: str, handler: _EventHandlerType) -> EventId
Listen to an emitted event to any target.
Warning
handler
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
once_any
staticmethod
¶
once_any(slf: ImplListener, event: str, handler: _EventHandlerType) -> EventId
Listens once to an emitted event to any target .
Warning
handler
has the same restrictions as App.run.
Source code in python/pytauri/src/pytauri/ffi/lib.py
Manager
¶
Methods:
Name | Description |
---|---|
app_handle |
The application handle associated with this manager. |
get_webview_window |
Fetch a single webview window from the manager. |
webview_windows |
Fetch all managed webview windows. |
path |
The path resolver is a helper class for general and application-specific path APIs. |
app_handle
staticmethod
¶
app_handle(slf: ImplManager) -> AppHandle
get_webview_window
staticmethod
¶
get_webview_window(slf: ImplManager, label: str) -> Optional[WebviewWindow]
webview_windows
staticmethod
¶
webview_windows(slf: ImplManager) -> dict[str, WebviewWindow]
path
staticmethod
¶
path(slf: ImplManager) -> PathResolver
The path resolver is a helper class for general and application-specific path APIs.
Position
¶
Classes:
Name | Description |
---|---|
Physical |
|
Logical |
Physical
¶
Bases: NamedTuple
[x, y]
Warning
This is actually a Class
disguised as an NamedTuple
.
See also: https://pyo3.rs/v0.23.4/class.html#pyclass-enums.
Logical
¶
Rect
¶
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
position |
PositionType
|
|
size |
SizeType
|
|
RunEvent
¶
Classes:
Name | Description |
---|---|
Exit |
|
ExitRequested |
|
WindowEvent |
|
WebviewEvent |
|
Ready |
|
Resumed |
|
MainEventsCleared |
|
MenuEvent |
|
TrayIconEvent |
Size
¶
Classes:
Name | Description |
---|---|
Physical |
|
Logical |
Physical
¶
Logical
¶
builder_factory
¶
A factory function for creating a Builder
instance.
This is the closure passed from the Rust side when initializing the pytauri pyo3 module.
args
and kwargs
will be passed to this closure.
Source code in python/pytauri/src/pytauri/ffi/lib.py
context_factory
¶
A factory function for creating a Context
instance.
This is the closure passed from the Rust side when initializing the pytauri pyo3 module.
args
and kwargs
will be passed to this closure.