Skip to content

PyTauri Wheel

The pytauri-wheel is built using the method described in tutorial/build-wheel to provide a precompiled pytauri.EXT_MOD. This means you can use pytauri without writing any Rust code and needing a Rust compiler.

Installation

PyPi

Prerequisites

Install WebView2 by visiting the WebView2 Runtime download section. Download the "Evergreen Bootstrapper" and install it.

As mentioned in https://tauri.app/distribute/debian/#debian:

  • libwebkit2gtk-4.1-0
  • libgtk-3-0
  • libappindicator3-1

Nothing

Wheel

We provide precompiled Wheels for the following platforms and supported Python versions:

OS Arch
windows-2022 x64
manylinux_2_35 X64, arm64
macOS-13 intel
macOS-14 arm64

win-arm64 need to wait for https://github.com/github/roadmap/issues/1098

pip install "pytauri-wheel == 0.4.*"  # (1)!
  1. This is the version at the time of writing this tutorial. There may be a newer version of pytauri available when you use it.

Sdist

If the above platform and Python version requirements are not met, the Wheel will be automatically built (compiled) from the source distribution when installing from pypi.

This requires you to meet the tutorial/#prerequisites.

Usage

The development experience of pytauri-wheel is almost the same as Rust tauri. You can find a complete example in examples/tauri-app-wheel.

Tauri Config

First, we need to create a Tauri.toml, refer to https://tauri.app/develop/configuration-files/#tauri-config:

/Tauri.toml
"$schema" = "https://schema.tauri.app/config/2"
productName = "pytauri-wheel-app"
version = "0.1.0"
identifier = "com.pytauri-wheel-app.app"

[build]
frontendDist = "./frontend"

[app]
withGlobalTauri = true

[[app.windows]]
title = "pytauri-wheel-app"
width = 800
height = 600

Tip

pytauri-wheel also support tauri.conf.json and tauri.conf.json and tauri.linux.conf.json and etc.

Capabilities

Refer to:

Create the following capabilities file to enable ipc permissions:

/capabilities/default.toml
# "$schema" = "..."  # we do not support this feature yet
identifier = "default"
description = "Capability for the main window"
windows = ["main"]
permissions = [
    "core:default",
    "pytauri:default", # need for pytauri ipc
    "dialog:default"
]

Tip

pytauri-wheel also support json and json5 capabilities files.

PyTauri Wheel App

The final step, refer to:

/main.py
import json
import sys
from os import environ
from pathlib import Path

from anyio.from_thread import start_blocking_portal
from pytauri import BuilderArgs, Commands
from pytauri_wheel.lib import builder_factory, context_factory

SRC_TAURI_DIR = Path(__file__).parent.absolute()
"""In rust tauri project, it's usually `src-tauri` dir.

πŸ“ {SRC_TAURI_DIR}/
β”œβ”€β”€ πŸ“ capabilities/
β”œβ”€β”€ πŸ“„ tauri.conf.json
└── ...
"""

DEV_SERVER = environ.get("DEV_SERVER")
"""Whether to use frontend dev server to serve frontend assets.

e.g, `Vite` dev server on `http://localhost:1420`.
"""

commands = Commands()


@commands.command()
async def greet() -> bytes:
    return json.dumps(sys.version).encode()


if DEV_SERVER is not None:
    # ref: <https://tauri.app/reference/config/#frontenddist-1>
    tauri_config = json.dumps(
        {
            "build": {
                "frontendDist": DEV_SERVER,
            },
        }
    )
else:
    tauri_config = None


def main() -> None:
    with start_blocking_portal("asyncio") as portal:  # or `trio`
        app = builder_factory().build(
            BuilderArgs(
                context=context_factory(SRC_TAURI_DIR, tauri_config=tauri_config),
                invoke_handler=commands.generate_handler(portal),
            )
        )
        app.run()


if __name__ == "__main__":
    main()

Note

The frontend assets directory must the same as the one in Tauri.toml.

/frontend/index.html
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>PyTauri-Wheel-App</title>
    </head>
    <body>
        <button onclick="greet()">Click me</button>
        <script>
            // πŸ€“ NOTE: you must set `[app.withGlobalTauri] = true` in `Tauri.toml,
            // or you can't use `window.__TAURI__` in the frontend.
            async function greet() {
                const message =
                    await window.__TAURI__.pytauri.pyInvoke("greet");
                alert(message);
            }
        </script>
    </body>
</html>

You get following directory structure:

πŸ“ {SRC_TAURI_DIR}
β”œβ”€β”€ πŸ“ capabilities
β”‚   └─── πŸ“„ default.toml
β”œβ”€β”€ πŸ“ frontend
β”‚   └─── πŸ“„ index.html
β”œβ”€β”€ πŸ“„ Tauri.toml
└── πŸ“„ main.py

Then run the app:

python main.py

Development Mode

If you want to use frontend dev server such as vite in development, or you want to dynamically/programmatically set tarui config, please refer to pytauri_wheel.lib.context_factory(tauri_config)

Tauri Plugins

pytauri-wheel enables a set of Tauri plugins during compilation, please refer to pytauri_wheel.lib.builder_factory.

For example, the tauri-plugin-dialog can be used in the frontend as follows:

import { ask } from '@tauri-apps/plugin-dialog';
// when using `"withGlobalTauri": true`, you may use
// const { ask } = window.__TAURI__.dialog;

Warning

Remember to enable the permissions for these plugins in /capabilities, otherwise you will receive an error on the frontend.