|
| 1 | +# Example Tauri v2 app using Python sidecar |
| 2 | + |
| 3 | +A native app built with Tauri version 2 that spawns a Python sub-process (sidecar) which is a FastAPI server. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +The 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. |
| 10 | + |
| 11 | +This template project is intended to demonstrate the use of single file Python executables with Tauri v2. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +The use of "sidecars" is to allow developers to package dependencies to make installation easier on the user. Tauri allows the frontend to communicate with any runtime and gives access to the OS's disk, camera, and other native hardware features. This is defined in the `tauri.conf.json` file. See [here](https://v2.tauri.app/develop/sidecar/) for more info. |
| 16 | + |
| 17 | +## How It Works |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +- 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. |
| 22 | + |
| 23 | +- [PyInstaller](https://pyinstaller.org/en/stable/) is used to compile a binary of the Python code so users don't have to worry about installing dependencies. |
| 24 | + |
| 25 | +- 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 (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). |
| 26 | + |
| 27 | +- 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. |
| 28 | + |
| 29 | +- When the GUI window is closed, the server and python processes are shutdown properly. |
| 30 | + |
| 31 | +## Features |
| 32 | + |
| 33 | +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). |
| 34 | + |
| 35 | +- Launch and communicate with any binary or runtime written in any language. This example uses a single Python executable. |
| 36 | + |
| 37 | +- Communicate between frontend (javascript) and a spawned server via http. |
| 38 | + |
| 39 | +- IPC communication between frontend (javascript) and Tauri (Rust) framework. |
| 40 | + |
| 41 | +- Also control this app from an external UI source like another website. As long as it uses http(s) and you whitelist the url in the server config `main.py`. |
| 42 | + |
| 43 | +## Project Structure |
| 44 | + |
| 45 | +These are the important project folders to understand. |
| 46 | + |
| 47 | +```bash |
| 48 | +/app # (frontend code, js/html/css) |
| 49 | +/src/backends # (backend code, the "sidecar") |
| 50 | +/src-tauri |
| 51 | + | /bin/api # (compiled sidecar is put here) |
| 52 | + | /icons # (app icons go here) |
| 53 | + | /src/main.rs # (Tauri main app logic) |
| 54 | + | tauri.conf.json # (Tauri config file for app permissions, etc.) |
| 55 | +package.json # (build scripts) |
| 56 | +``` |
| 57 | + |
| 58 | +## Getting Started |
| 59 | + |
| 60 | +### Dependencies |
| 61 | + |
| 62 | +Install all dependencies for javascript and Python: |
| 63 | + |
| 64 | +```bash |
| 65 | +pnpm install-reqs |
| 66 | +``` |
| 67 | + |
| 68 | +Or install javascript dependencies only: |
| 69 | + |
| 70 | +```bash |
| 71 | +pnpm install |
| 72 | +``` |
| 73 | + |
| 74 | +Or install dependencies for Python only. Be sure to run with admin privileges. Recommend creating a virtual env: |
| 75 | + |
| 76 | +``` |
| 77 | +pip install -r requirements.txt |
| 78 | +``` |
| 79 | + |
| 80 | +Tauri requires icons in the appropriate folder. Run the script to automatically generate icons from a source image. I have included icons for convenience. |
| 81 | + |
| 82 | +```bash |
| 83 | +pnpm build:icons |
| 84 | +``` |
| 85 | + |
| 86 | +### Run |
| 87 | + |
| 88 | +Run the app in development mode: |
| 89 | + |
| 90 | +```bash |
| 91 | +pnpm tauri dev |
| 92 | +``` |
| 93 | + |
| 94 | +### Build |
| 95 | + |
| 96 | +#### Compile python sidecar |
| 97 | + |
| 98 | +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`: |
| 99 | + |
| 100 | +```bash |
| 101 | +pnpm build:sidecar-winos |
| 102 | +# OR |
| 103 | +pnpm build:sidecar-macos |
| 104 | +# OR |
| 105 | +pnpm build:sidecar-linux |
| 106 | +``` |
| 107 | + |
| 108 | +In case you dont have PyInstaller installed run: |
| 109 | + |
| 110 | +``` |
| 111 | +pip install -U pyinstaller |
| 112 | +``` |
| 113 | + |
| 114 | +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. |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +#### Build for production |
| 119 | + |
| 120 | +Build the production app for a specific OS: |
| 121 | + |
| 122 | +```bash |
| 123 | +pnpm tauri build |
| 124 | +``` |
| 125 | + |
| 126 | +This creates an installer located here: |
| 127 | + |
| 128 | +- `<project-dir>\src-tauri\target\release\bundle\nsis` |
| 129 | + |
| 130 | +And the raw executable here: |
| 131 | + |
| 132 | +- `<project-dir>\src-tauri\target\release` |
| 133 | + |
| 134 | +## Todo's |
| 135 | + |
| 136 | +- Pass parameters to the sidecar (like server port) via a frontend form. |
| 137 | + |
| 138 | +- 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 run `pnpm build:sidecar-[os]` each time you make changes to the Python code. |
| 139 | + |
| 140 | +- Develop a standalone multi-sidecar manager that can handle startup/shutdown and communication between all other sidecars spawned in the app. |
| 141 | + |
| 142 | +## Learn More |
| 143 | + |
| 144 | +- [Tauri Framework](https://tauri.app/) - learn about native app development in javascript and rust. |
| 145 | +- [NextJS](https://nextjs.org/docs) - learn about the popular react framework Next.js |
| 146 | +- [FastAPI](https://fastapi.tiangolo.com/) - learn about FastAPI server features and API. |
| 147 | +- [PyInstaller](https://pyinstaller.org/en/stable/) - learn about packaging python code. |
0 commit comments