pytauri.ffi.lib
¶
Classes:
Functions:
Name | Description |
---|---|
webview_version |
|
builder_factory |
A factory function for creating a |
context_factory |
A factory function for creating a |
Attributes:
Name | Type | Description |
---|---|---|
RESTART_EXIT_CODE |
Final[int]
|
|
VERSION |
Final[LiteralString]
|
|
IS_DEV |
Final[bool]
|
|
RunEventType |
See RunEvent for details. |
|
DragDropEventType |
See DragDropEvent for details. |
|
WebviewEventType |
See WebviewEvent for details. |
|
WindowEventType |
See WindowEvent for details. |
|
ImplManager |
|
|
EventId |
||
ImplListener |
|
|
PositionType |
See Position for details. |
|
SizeType |
See Size for details. |
|
Url |
||
ImplEmitter |
|
|
EventTargetType |
See EventTarget for details. |
__all__
module-attribute
¶
__all__ = ['IS_DEV', 'RESTART_EXIT_CODE', 'VERSION', 'App', 'AppHandle', 'Assets', 'Builder', 'BuilderArgs', 'CloseRequestApi', 'Context', 'DragDropEvent', 'DragDropEventType', 'Emitter', 'Event', 'EventId', 'EventTarget', 'EventTargetType', 'ExitRequestApi', 'ImplEmitter', 'ImplListener', 'ImplManager', 'Listener', 'Manager', 'Position', 'PositionType', 'Rect', 'RunEvent', 'RunEventType', 'Size', 'SizeType', 'Theme', 'Url', 'WebviewEvent', 'WebviewEventType', 'WindowEvent', 'WindowEventType', 'builder_factory', 'context_factory', 'webview_version']
RESTART_EXIT_CODE
module-attribute
¶
The exit code on RunEvent::ExitRequested
when AppHandle
is called.
IS_DEV
module-attribute
¶
Whether we are running in development mode or not.
RunEventType
module-attribute
¶
RunEventType = TypeAliasType('RunEventType', Union[Exit, ExitRequested, WindowEvent, WebviewEvent, Ready, Resumed, MainEventsCleared, MenuEvent, TrayIconEvent, _NonExhaustive])
See RunEvent for details.
DragDropEventType
module-attribute
¶
DragDropEventType = TypeAliasType('DragDropEventType', Union[Enter, Over, Drop, Leave, _NonExhaustive])
See DragDropEvent for details.
WebviewEventType
module-attribute
¶
WebviewEventType = TypeAliasType('WebviewEventType', Union[DragDrop, _NonExhaustive])
See WebviewEvent for details.
WindowEventType
module-attribute
¶
WindowEventType = TypeAliasType('WindowEventType', Union[Resized, Moved, CloseRequested, Destroyed, Focused, ScaleFactorChanged, DragDrop, ThemeChanged, _NonExhaustive])
See WindowEvent 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.
SizeType
module-attribute
¶
SizeType = TypeAliasType('SizeType', Union[Physical, Logical])
See Size for details.
EventTargetType
module-attribute
¶
EventTargetType = TypeAliasType('EventTargetType', Union[Any, AnyLabel, App, Window, Webview, WebviewWindow, _NonExhaustive])
See EventTarget for details.
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 |
|
set_theme |
|
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
set_theme
¶
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
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 |
build
¶
Consume this builder and build an app with the given BuilderArgs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Context
|
use context_factory to get it. |
required |
|
Unpack[BuilderArgs]
|
see BuilderArgs for details. |
{}
|
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
RunEvent
¶
Classes:
Name | Description |
---|---|
Exit |
|
ExitRequested |
|
WindowEvent |
|
WebviewEvent |
|
Ready |
|
Resumed |
|
MainEventsCleared |
|
MenuEvent |
|
TrayIconEvent |
Exit
¶
ExitRequested
¶
tauri::RunEvent::ExitRequested
Attributes:
Name | Type | Description |
---|---|---|
code |
Optional[int]
|
|
api |
ExitRequestApi
|
|
WindowEvent
¶
WebviewEvent
¶
Ready
¶
Resumed
¶
MainEventsCleared
¶
MenuEvent
¶
TrayIconEvent
¶
Bases: tuple[TrayIconEventType]
tauri::RunEvent::TrayIconEvent
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
__new__
¶
__new__(_0: TrayIconEventType) -> Self
Source code in python/pytauri/src/pytauri/ffi/lib.py
ExitRequestApi
¶
Methods:
Name | Description |
---|---|
prevent_exit |
|
CloseRequestApi
¶
Methods:
Name | Description |
---|---|
prevent_close |
|
DragDropEvent
¶
Classes:
Name | Description |
---|---|
Enter |
|
Over |
|
Drop |
|
Leave |
WebviewEvent
¶
Classes:
Name | Description |
---|---|
DragDrop |
DragDrop
¶
Bases: tuple['DragDropEventType']
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
__new__
¶
__new__(_0: DragDropEventType) -> Self
Source code in python/pytauri/src/pytauri/ffi/lib.py
WindowEvent
¶
Classes:
Name | Description |
---|---|
Resized |
|
Moved |
|
CloseRequested |
|
Destroyed |
|
Focused |
|
ScaleFactorChanged |
|
DragDrop |
|
ThemeChanged |
Resized
¶
Bases: tuple[_PhysicalSizeU32]
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
Moved
¶
Bases: tuple[_PhysicalPositionI32]
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
CloseRequested
¶
Destroyed
¶
Focused
¶
ScaleFactorChanged
¶
tauri::WindowEvent::ScaleFactorChanged
Attributes:
Name | Type | Description |
---|---|---|
scale_factor |
float
|
|
new_inner_size |
_PhysicalSizeU32
|
|
DragDrop
¶
Bases: tuple['DragDropEventType']
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
__new__
¶
__new__(_0: DragDropEventType) -> Self
Source code in python/pytauri/src/pytauri/ffi/lib.py
ThemeChanged
¶
Bases: tuple['Theme']
tauri::WindowEvent::ThemeChanged
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
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. |
manage |
Add |
state |
Retrieves the managed state for the type |
try_state |
Attempts to retrieve the managed state for the type |
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]
manage
staticmethod
¶
manage(slf: ImplManager, state: object) -> bool
Add state
to the state managed by the application.
If the state for the T
type has previously been set, the state is unchanged and false is returned.
Otherwise true is returned.
Source code in python/pytauri/src/pytauri/ffi/lib.py
state
staticmethod
¶
state(slf: ImplManager, state_type: type[_T]) -> _T
Retrieves the managed state for the type T
.
Raises:
Type | Description |
---|---|
Exception
|
Source code in python/pytauri/src/pytauri/ffi/lib.py
try_state
staticmethod
¶
try_state(slf: ImplManager, state_type: type[_T]) -> Optional[_T]
Attempts to retrieve the managed state for the type T
.
Returns Some
if the state has previously been managed
. Otherwise returns None
.
Source code in python/pytauri/src/pytauri/ffi/lib.py
path
staticmethod
¶
path(slf: ImplManager) -> PathResolver
The path resolver is a helper class for general and application-specific path APIs.
Event
¶
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
Position
¶
Classes:
Name | Description |
---|---|
Physical |
|
Logical |
Physical
¶
Bases: tuple[_PhysicalPositionI32]
Warning
This is actually a Class
disguised as an NamedTuple
.
See also: https://pyo3.rs/v0.23.4/class.html#pyclass-enums.
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
Logical
¶
Bases: tuple[_LogicalPositionF64]
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
Size
¶
Classes:
Name | Description |
---|---|
Physical |
|
Logical |
Physical
¶
Bases: tuple[_PhysicalSizeU32]
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
Logical
¶
Bases: tuple[_LogicalSizeF64]
Warning
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
__match_args__ |
|
Rect
¶
Methods:
Name | Description |
---|---|
__new__ |
|
Attributes:
Name | Type | Description |
---|---|---|
position |
PositionType
|
|
size |
SizeType
|
|
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
¶
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
BuilderArgs
¶
Bases: TypedDict
Attributes:
Name | Type | Description |
---|---|---|
invoke_handler |
Required[Optional[_InvokeHandlerProto]]
|
Use Commands to get it. |
setup |
Callable[[AppHandle], object]
|
See rust |
invoke_handler
instance-attribute
¶
Use Commands to get it.
Warning
The implement 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.
Assets
¶
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.