|
| 1 | +# Desktop preview |
| 2 | + |
| 3 | +`pn preview` renders your app in a native desktop window with **instant |
| 4 | +Fast Refresh** on every save. It's the fastest way to build UI in |
| 5 | +PythonNative: edit a component, hit save, and see the result in a second |
| 6 | +— no simulator boot, no device deploy, no rebuild. |
| 7 | + |
| 8 | +```bash |
| 9 | +pn preview |
| 10 | +``` |
| 11 | + |
| 12 | +The preview reuses the **same reconciler, hooks, navigation, async |
| 13 | +runtime, and pure-Python flex layout engine** that run on device. Only |
| 14 | +the leaf widgets differ: the desktop backend draws with |
| 15 | +[Tkinter](https://docs.python.org/3/library/tkinter.html) instead of |
| 16 | +UIKit / Android views. So behavior and layout match the device closely, |
| 17 | +and your iteration loop collapses from minutes to seconds. |
| 18 | + |
| 19 | +## Quick start |
| 20 | + |
| 21 | +From a project directory (one created by `pn init`, with an `app/main.py` |
| 22 | +that defines `App`): |
| 23 | + |
| 24 | +```bash |
| 25 | +pn preview |
| 26 | +``` |
| 27 | + |
| 28 | +`pn preview` runs your real app code, so install your project's |
| 29 | +dependencies in the same environment first (for example |
| 30 | +`pip install -r requirements.txt`). If an import fails, the preview shows |
| 31 | +the traceback in the window instead of crashing — install the missing |
| 32 | +package or fix the code and save to recover. |
| 33 | + |
| 34 | +This opens a phone-sized window, mounts your `App`, and starts watching |
| 35 | +`app/` for changes. Edit any component and save — the window updates in |
| 36 | +place while preserving component state (counters, text input, scroll |
| 37 | +position, navigation stack). |
| 38 | + |
| 39 | +```bash |
| 40 | +pn preview # the project entry point (app/main.py → App) |
| 41 | +pn preview app.screens.home # a module whose App attribute to mount |
| 42 | +pn preview app.main.DetailScreen # a specific dotted component |
| 43 | +pn preview --width 768 --height 1024 # tablet-sized window |
| 44 | +pn preview --title "My App" |
| 45 | +pn preview --no-hot-reload # mount once, don't watch files |
| 46 | +``` |
| 47 | + |
| 48 | +## Requirements |
| 49 | + |
| 50 | +The preview uses Tkinter, Python's standard GUI toolkit, which ships |
| 51 | +with most Python installations. If `pn preview` reports that Tkinter is |
| 52 | +missing: |
| 53 | + |
| 54 | +- **macOS** (Homebrew Python): `brew install python-tk` |
| 55 | +- **Debian / Ubuntu**: `sudo apt-get install python3-tk` |
| 56 | +- **Windows**: re-run the Python installer and enable the |
| 57 | + "tcl/tk and IDLE" optional feature. |
| 58 | + |
| 59 | +No other dependencies are required — the desktop backend is pure Python. |
| 60 | + |
| 61 | +## How it works |
| 62 | + |
| 63 | +`pn preview` sets `PN_PLATFORM=desktop` and starts |
| 64 | +`pythonnative.preview.run_preview`, which: |
| 65 | + |
| 66 | +1. Opens a single Tk window with one *stage* frame. |
| 67 | +2. Selects the Tkinter |
| 68 | + [native-view registry][pythonnative.native_views.get_registry], so |
| 69 | + every `pn.Text`, `pn.Button`, … maps to a Tk widget. |
| 70 | +3. Mounts your `App` through a normal |
| 71 | + [`Reconciler`][pythonnative.reconciler.Reconciler] and pushes the |
| 72 | + window size in as the layout viewport. |
| 73 | +4. Runs the Tk event loop on the main thread, polling ~60×/second to |
| 74 | + apply renders requested from the async runtime thread and to drain |
| 75 | + file-change reloads. |
| 76 | +5. Watches `app/` with a |
| 77 | + [`FileWatcher`][pythonnative.hot_reload.FileWatcher] and Fast |
| 78 | + Refreshes on every save. |
| 79 | + |
| 80 | +Layout is owned by the engine, not the widgets: the |
| 81 | +[flex layout engine](../concepts/layout.md) computes an absolute frame |
| 82 | +for every element, and the backend positions each Tk widget with that |
| 83 | +frame. This is the same contract the iOS and Android backends follow, so |
| 84 | +a column that lays out correctly on a phone lays out the same way in the |
| 85 | +preview. |
| 86 | + |
| 87 | +### Navigation |
| 88 | + |
| 89 | +Root navigators (`create_stack_navigator`, tabs, drawer) drive a real |
| 90 | +in-process stack of screen hosts in the preview, the same way they drive |
| 91 | +`UINavigationController` / AndroidX Navigation on device. `navigate(...)` |
| 92 | +pushes a new screen host (preserving the previous screen's state); |
| 93 | +`go_back()` pops it. Each screen runs in its own reconciler host. |
| 94 | + |
| 95 | +## Fast Refresh |
| 96 | + |
| 97 | +Saving a `.py` file under `app/` triggers a reload. The preview prefers |
| 98 | +**Fast Refresh**: the changed modules are reloaded and the live VNode |
| 99 | +tree's function references are swapped in place, so the next render |
| 100 | +reuses existing hook state. Edits to a component body keep your |
| 101 | +counters, form values, and scroll positions. When a clean swap isn't |
| 102 | +possible (structural edits, a raised exception), the preview falls back |
| 103 | +to a full remount so you're never stuck with a stale tree. |
| 104 | + |
| 105 | +If your component raises at import time (a syntax error you're mid-fix |
| 106 | +on), the preview shows the traceback as an overlay and recovers |
| 107 | +automatically on your next successful save — no restart needed. |
| 108 | + |
| 109 | +See the [Hot reload guide](hot-reload.md) for the underlying mechanics; |
| 110 | +the desktop preview shares the same Fast Refresh engine. |
| 111 | + |
| 112 | +## Branching on the platform |
| 113 | + |
| 114 | +When the preview is running, [`Platform.OS`][pythonnative.Platform] is |
| 115 | +`"desktop"`: |
| 116 | + |
| 117 | +```python |
| 118 | +import pythonnative as pn |
| 119 | + |
| 120 | +pad = pn.Platform.select({"desktop": 12, "ios": 16, "android": 16, "default": 12}) |
| 121 | +``` |
| 122 | + |
| 123 | +Note that `Platform.select`'s `"native"` key matches iOS and Android |
| 124 | +only — desktop is a development surface, so use an explicit `"desktop"` |
| 125 | +key (or `"default"`) for it. You can also check |
| 126 | +[`Platform.is_desktop`][pythonnative.Platform] or the |
| 127 | +`pythonnative.utils.IS_DESKTOP` flag directly. |
| 128 | + |
| 129 | +## What's faithful, and what's approximated |
| 130 | + |
| 131 | +The preview is a **development tool**, optimized for fidelity of layout |
| 132 | +and logic rather than pixel-perfect chrome. |
| 133 | + |
| 134 | +Faithful: |
| 135 | + |
| 136 | +- Flex layout, sizing, padding, spacing, absolute positioning. |
| 137 | +- Component lifecycle, hooks, effects, context, error boundaries. |
| 138 | +- Navigation (stack push/pop, tabs, drawer) and per-screen state. |
| 139 | +- The async runtime, `use_query` / `use_mutation`, timers, and |
| 140 | + state-driven updates. |
| 141 | +- Text wrapping and intrinsic sizing (measured with the same font the |
| 142 | + widget renders). |
| 143 | + |
| 144 | +Approximated or omitted (Tkinter can't express these cheaply): |
| 145 | + |
| 146 | +- Rounded corners, shadows, gradients, and per-widget opacity. |
| 147 | +- Overflow **clipping** — a `ScrollView`'s content renders but isn't |
| 148 | + clipped to the viewport, and there's no interactive scrolling. |
| 149 | +- Animations show their **end state** rather than smooth interpolation |
| 150 | + (translations are applied; scale/rotate/opacity are skipped). |
| 151 | +- `Image` loads local PNG/GIF files; network URLs and JPEG fall back to |
| 152 | + a labeled placeholder. `WebView` shows a placeholder. |
| 153 | + |
| 154 | +When the chrome matters, verify on device with `pn run`. |
| 155 | + |
| 156 | +## When to use device builds instead |
| 157 | + |
| 158 | +`pn preview` is for fast UI/logic iteration. Reach for `pn run` when you |
| 159 | +need: |
| 160 | + |
| 161 | +- Pixel-perfect native chrome and platform behaviors. |
| 162 | +- Real device APIs (camera, location, notifications, biometrics, …). |
| 163 | +- To test packaging, permissions, or store builds. |
| 164 | + |
| 165 | +There is no desktop packaging target — ship to devices with |
| 166 | +`pn run android` / `pn run ios`. |
| 167 | + |
| 168 | +## Next steps |
| 169 | + |
| 170 | +- Reference: [`pn` CLI](../api/cli.md). |
| 171 | +- Mechanics shared with device hot reload: [Hot reload](hot-reload.md). |
| 172 | +- How layout is computed: [Layout engine](../concepts/layout.md). |
| 173 | +- Platform branching: [Platform & accessibility](platform-accessibility.md). |
0 commit comments