Hot reload¶
Hot-reload comes in two cooperating pieces: a host-side file watcher
that pushes changed .py files to the device, and a device-side
module reloader that swaps the new code in and re-renders the active
page. Both are wired up automatically by pn run --hot-reload.
Hot-reload support for PythonNative development.
Two cooperating pieces:
- Host-side:
FileWatcherpolls the developer'sapp/directory for.pychanges and triggers a callback (typicallyadb pushon Android or asimctlfile copy on iOS). - Device-side:
ModuleReloaderreloads changed Python modules usingimportlib.reloadand asks the page host to re-render the current tree.
Example
Integrated into pn run --hot-reload:
Classes:
| Name | Description |
|---|---|
FileWatcher |
Watch a directory tree for |
ModuleReloader |
Reload changed Python modules on device and trigger a re-render. |
FileWatcher
¶
Watch a directory tree for .py file changes.
Uses simple os.path.getmtime polling rather than a native
inotify/FSEvents binding so the watcher works on every platform
where Python runs without extra dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
watch_dir
|
str
|
Directory to watch (recursively). |
required |
on_change
|
Callable[[List[str]], None]
|
Called with a list of changed file paths when modifications are detected. |
required |
interval
|
float
|
Polling interval, in seconds. |
1.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
watch_dir |
Directory being watched. |
|
on_change |
Change callback. |
|
interval |
Polling interval. |
Methods:
| Name | Description |
|---|---|
start |
Begin watching in a background daemon thread. |
stop |
Stop the watcher and join the background thread. |
ModuleReloader
¶
Reload changed Python modules on device and trigger a re-render.
Designed to be invoked from device-side glue when a hot-reload push completes. The class itself holds no state; all methods are static.
Methods:
| Name | Description |
|---|---|
reload_module |
Reload a single module by its dotted name. |
file_to_module |
Convert a file path to a dotted module name. |
reload_page |
Force a page re-render after a module reload. |
reload_module
staticmethod
¶
file_to_module
staticmethod
¶
Convert a file path to a dotted module name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to a |
required |
base_dir
|
str
|
Base directory that names should be relative to.
If empty, |
''
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The dotted module name (e.g., |
Optional[str]
|
|
Next steps¶
- See the workflow in Hot reload guide.