Skip to main content

Builder

Struct Builder 

Source
pub struct Builder { /* private fields */ }
Expand description

插件构建器。未设置的选项回退到 tauri.conf.json 中的 Config,再回退到默认值。

唯一必填项是入口脚本(Builder::script 或 plugins.py-runtime.script)。

§Examples

use std::time::Duration;

let plugin = tauri_plugin_py_runtime::Builder::new()
  .script("../src-python/main.py")
  .python("python3")
  .args(["--verbose"])
  .env("APP_MODE", "dev")
  .shutdown_timeout(Duration::from_secs(5))
  .restart_on_crash(true)
  .build::<tauri::Wry>();
let _app = tauri::Builder::default().plugin(plugin);

Implementations§

Source§

impl Builder

Source

pub fn new() -> Self

创建所有选项均未设置的构建器。

Source

pub fn python(self, program: impl Into<String>) -> Self

设置 Python 解释器(路径或命令名)。

未设置时,若脚本目录下存在 .venv(如 uv sync 创建的)则使用其中的解释器, 否则为 python3(Windows 上为 python)。该解释器必须能导入 tauri_runtime (及其依赖 pydantic)。

Source

pub fn script(self, path: impl Into<PathBuf>) -> Self

设置 Python 入口脚本。

绝对路径原样使用。相对路径按以下顺序尝试,第一个存在的文件胜出:仅在 debug 构建中, 相对于当前工作目录(tauri dev 下为 src-tauri);然后在所有构建中,相对于资源目录, 并把每个 .. 替换为 _up_(与打包器布局一致)。因此当 bundle.resources 包含 "../src-python/**/*" 时,"../src-python/main.py" 在开发与发布构建中都能找到。 Python 以脚本所在目录作为工作目录运行。

Source

pub fn bundle(self, name: impl Into<String>) -> Self

设置 Nuitka 打包产物名(即 tauri-runtime build 配置中的 name)。

仅在 release 构建中生效:运行资源目录下的 <name>/<name>(Windows 为 <name>.exe), 以其所在目录为工作目录,并忽略 Builder::python 与 Builder::script;debug 构建 仍以解释器运行脚本。产物需通过 bundle.resources 打进应用,找不到时 setup 失败。

Source

pub fn args<I, S>(self, args: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置追加在脚本路径(或打包产物)之后的命令行参数,覆盖配置文件中的 args。

Source

pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self

为 Python 进程追加一个环境变量,可多次调用。

PYTHONUNBUFFERED=1 与 PYTHONIOENCODING=utf-8 总会被设置,无法覆盖。

Source

pub fn invoke_timeout(self, timeout: Duration) -> Self

设置每次调用的默认超时,默认 30 秒。前端的 timeoutMs 最多可将其放宽到该值的 10 倍。

Source

pub fn shutdown_timeout(self, timeout: Duration) -> Self

设置 Python 执行 shutdown 钩子的时间预算,默认 3 秒;超过该时间再加 1 秒宽限后进程会被杀死。

Source

pub fn handshake_timeout(self, timeout: Duration) -> Self

设置 Python 启动并发送握手的时限,默认 10 秒;也是调用等待启动中的运行时就绪的最长时间。

Source

pub fn restart_on_crash(self, enabled: bool) -> Self

设置 Python 意外退出后是否以指数退避自动重启,默认关闭。

详见 SupervisorConfig::restart_on_crash。

Source

pub fn build<R: Runtime>(self) -> TauriPlugin<R, Option<Config>>

构建 Tauri 插件。

插件 setup 时解析配置与入口脚本、注册 PyRuntime 状态并在后台启动 Python; 应用退出(RunEvent::Exit)时阻塞等待 Supervisor::shutdown 完成。 未配置入口脚本或脚本不存在时 setup 失败,错误信息列出所有尝试过的路径。

Trait Implementations§

Source§

impl Debug for Builder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.