Skip to content

Commit 0ac002d

Browse files
committed
Init commit files
1 parent d09bd53 commit 0ac002d

File tree

81 files changed

+21011
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+21011
-0
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts
36+
37+
# python
38+
**/__pycache__
39+
*.spec
40+
**/.venv

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"[python]": {
3+
"editor.formatOnSave": true
4+
},
5+
"python.formatting.provider": "black"
6+
}
7+

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
![logo](extras/sidecar-logo.png "python sidecar logo")
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+
![app screenshot](extras/screenshot.png "app screenshot")
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+
![python sidecar architecture](extras/diagram.png "python sidecar architecture")
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.

app/favicon.ico

28.3 KB
Binary file not shown.

app/globals.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
5+
:root {
6+
--foreground-rgb: 0, 0, 0;
7+
--background-start-rgb: 214, 219, 220;
8+
--background-end-rgb: 255, 255, 255;
9+
}
10+
11+
@media (prefers-color-scheme: dark) {
12+
:root {
13+
--foreground-rgb: 255, 255, 255;
14+
--background-start-rgb: 0, 0, 0;
15+
--background-end-rgb: 0, 0, 0;
16+
}
17+
}
18+
19+
body {
20+
color: rgb(var(--foreground-rgb));
21+
background: linear-gradient(
22+
to bottom,
23+
transparent,
24+
rgb(var(--background-end-rgb))
25+
)
26+
rgb(var(--background-start-rgb));
27+
}

app/layout.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import './globals.css'
2+
import { Inter } from 'next/font/google'
3+
4+
const inter = Inter({ subsets: ['latin'] })
5+
6+
export const metadata = {
7+
title: 'Example Tauri v2 Python sidecar',
8+
description: 'Template for tauri and python sidecar project.',
9+
}
10+
11+
export default function RootLayout({
12+
children,
13+
}: {
14+
children: React.ReactNode
15+
}) {
16+
return (
17+
<html lang="en">
18+
<body className={inter.className}>{children}</body>
19+
</html>
20+
)
21+
}

0 commit comments

Comments
 (0)