Device Libraries¶
An App can bundle MicroPython files that go on the board when it's installed.
Structure¶
Device files live in the device/ directory of your App:
devicePaths Mapping¶
In extension.json, map files under device/ to where they go on the board:
{
"devicePaths": {
"helpers.py": "/lib/ext/my_app/helpers.py",
"utils.py": "/lib/ext/my_app/utils.py",
"submodule/__init__.py": "/lib/ext/my_app/submodule/__init__.py"
}
}
Device files go to /lib/ext/<folder>/. Use underscores in the folder name, since a hyphen can't appear in a Python module name.
Installation Flow¶
- You click Install in the App library.
- Studio downloads the bundle and stores it in the browser.
- With a board connected, Studio calls
onInstall()on the App. - The App writes the files in
this.deviceFilesto the board. - Studio records the install on the board in
/lib/ext/registry.json.
An agent or script that copies the files itself can record the install with the scripto_installed tool. It takes id, version, and optionally name and folder (the folder under /lib/ext/, when it differs from the id with hyphens turned into underscores). Call it once the files are in place.
onInstall Implementation¶
async onInstall() {
if (!this.state.isConnected) return false
try {
// Create directories
await this.device.mkdir('/lib/ext/my_app')
await this.device.mkdir('/lib/ext/my_app/submodule')
// Write all device files
for (const [path, content] of Object.entries(this.deviceFiles)) {
console.log(`Writing ${path}...`)
await this.device.saveFile(path, content)
}
return true
} catch (e) {
console.error('Installation failed:', e)
return false
}
}
Import Paths¶
Device code imports from the installed path:
Best Practices¶
- Keep files small - Device flash is limited
- Avoid heavy dependencies - Files should be self-contained
- Use lazy imports - Import modules only when needed
- Don't verify imports in onInstall - an import can fail if a file it needs isn't written yet
Bundle Process¶
The bundler:
1. Reads devicePaths from extension.json
2. Reads each source file from device/
3. Base64-encodes the content
4. Embeds as __DEVICE_FILES__ in the bundle
At runtime, the loader decodes __DEVICE_FILES__ and injects it as instance.deviceFiles.