Skip to content

An example desktop app built with Tauri version 2. Bundle a Next.js frontend with a Python api server.

License

Notifications You must be signed in to change notification settings

dieharders/example-tauri-v2-python-server-sidecar

Repository files navigation

Example Tauri v2 app using Python sidecar

A native app built with Tauri version 2 that spawns a Python sub-process (sidecar) which starts a FastAPI server.

Download the example app and try out

Python TypeScript Rust
FastAPI NextJS Tauri

Note

Tauri v1 example also available: example-tauri-python-server-sidecar

logo

Introduction

This example app uses Next.js as the frontend and Python (FastAPI) as the backend. Tauri is a Rust framework that orchestrates the frontend and backend(s) into a native app experience.

This template project is intended to demonstrate the use of single file Python executables with Tauri v2.

app screenshot

Tauri "sidecars" allow developers to package dependencies to make installation easier on the user. Tauri's API allows the frontend to communicate with any runtime and give it access to the OS disk, camera, and other native hardware features. This is defined in the tauri.conf.json file. See here for more info.

How It Works

python sidecar architecture

  • Tauri takes your frontend UI written in html/javascript and displays it in a native webview. This makes the resulting file size smaller since it does not need to include a web browser (as Electron.js requires).

  • PyInstaller is used to compile a binary of the Python code so users don't have to worry about installing dependencies.

  • In main.rs we spawn a python binary (sidecar) called main.exe which starts an api server on localhost:8008. The frontend tells Tauri to startup and shutdown the sidecar via stdin/stdout commands. Note, we cannot use process.kill() for one-file Python executables since Tauri only knows the pid of the PyInstaller bootloader process and not its' child process (which is actually the sidecar).

  • If you are not comfortable coding in Rust, do not worry, this example orchestrates things in a way so you only need concern yourself with communication between your frontend and server (sidecar) via http.

  • When the GUI window is closed, the server and python processes are shutdown properly.

Features

This should give you everything you need to build a local, native application that can use other programs to perform specialized work (like a server, llm engine or database).

  • Launch and communicate with any binary or runtime written in any language. This example uses a Python executable.

  • Communicate between frontend (javascript) and backend (Python) server via http.

  • IPC communication between frontend (javascript) and Tauri (Rust) framework.

  • Also control this app from an external UI source like another website, app or terminal. As long as it uses http(s) and you whitelist the url in the server config main.py.

Project Structure

These are the important project folders to understand.

/app # (frontend code, js/html/css)
/src/backends # (backend code, the "sidecar")
/src-tauri
  |  /bin/api # (compiled sidecar is put here)
  |  /icons # (app icons go here)
  |  /src/main.rs # (Tauri main app logic)
  |  tauri.conf.json # (Tauri config file for app permissions, etc.)
package.json # (build scripts)

Getting Started

Dependencies

Install all dependencies for javascript and Python:

pnpm install-reqs

Or install javascript dependencies only:

pnpm install

Or install dependencies for Python only. Be sure to run with admin privileges. Recommend creating a virtual env:

pip install -r requirements.txt

Tauri requires icons in the appropriate folder. Run the script to automatically generate icons from a source image. I have included icons for convenience.

pnpm build:icons

Run

Run the app in development mode:

pnpm tauri dev

Build

Compile python sidecar

Run this at least once before running pnpm tauri dev and each time you make changes to your python code. This command is also called by pnpm tauri build:

pnpm build:sidecar-winos
# OR
pnpm build:sidecar-macos
# OR
pnpm build:sidecar-linux

In case you dont have PyInstaller installed run:

pip install -U pyinstaller

A note on compiling Python exe (the -F flag bundles everything into one .exe). You won't need to run this manually each build, I have included it in the build scripts.


Build for production

Build the production app for a specific OS:

# Uses `pnpm build:sidecar-winos` by default
# Modify script for your target OS
pnpm tauri build

This creates an installer located here:

  • <project-dir>\src-tauri\target\release\bundle\nsis

And the raw executable here:

  • <project-dir>\src-tauri\target\release

Todo's

  • Pass parameters to the sidecar (like server port) via a frontend form.

  • Pass argument --dev-sidecar to pnpm tauri dev script that tells Tauri to run sidecars in "dev mode". This would allow for running the python code from the python interpreter installed on your machine rather than having to manually compile with pnpm build:sidecar-[os] each time you make changes to the Python code.

  • Develop a standalone multi-sidecar manager that can handle startup/shutdown and communication between all other sidecars spawned in the app.

Learn More

  • Tauri Framework - learn about native app development in javascript and rust.
  • NextJS - learn about the popular react framework Next.js
  • FastAPI - learn about FastAPI server features and API.
  • PyInstaller - learn about packaging python code.