FileStorage
Files an app keeps between runs, in a folder of its own.
The Flet client stores them for the app: on web in the browser's origin
private file system (OPFS), on desktop and mobile in a folder under the app
support directory. Use it for data that outgrows
SharedPreferences, such as a SQLite database
(sqlite3.Connection.serialize() / sqlite3.Connection.deserialize()),
images or JSON documents.
It matters most on web, where an app's Python has no persistent file
system of its own. On desktop and mobile, an app can also use regular
files (see StoragePaths), which is faster for large
or frequently written data; FileStorage works there too, so the same code
runs on every platform.
Names are paths relative to the app's folder, such as "notes.json" or
"db/app.sqlite": parts are separated by /, and none may be empty,
. or .., contain \ : * ? " < > | or control characters, or end with a
space or a dot. The same rules apply on every platform.
A write replaces the whole file atomically: a reader sees the old contents or the new ones, never a mix.
An embedded app gets a folder derived from its
FletApp.storage_prefix. Apps with
the same effective prefix share files and named locks. It can keep
up to FletApp.file_storage_quota
bytes (50 MB unless the host sets another limit). A top-level app has
no limit beyond the platform's own.
On web, writing needs writable OPFS files and file moves. Supported browsers include desktop Chrome 102+, Chrome for Android 109+, Firefox 111+ and Safari 26+.
Inherits: Service
Methods
delete- Deletes a file.exists- Checks whether a file exists.list- Lists the app's files, including those in folders.lock- Takes an advisory lock.read- Reads a file.size- Returns the total size of the app's files, in bytes.unlock- Releases a lock taken withlock().write- Creates or replaces a file, atomically.
Examples
File storage notes
import json
import flet as ft
NOTES = "notes.json"
async def main(page: ft.Page):
page.title = "Notes"
storage = ft.FileStorage()
notes: list[str] = []
def show():
note_list.controls = [ft.Text(n) for n in notes]
status.value = f"{len(notes)} notes saved"
page.update()
async def save():
await storage.write(NOTES, json.dumps(notes).encode())
async def add_note():
if not new_note.value:
return
notes.append(new_note.value)
new_note.value = ""
await save()
show()
async def clear_notes():
notes.clear()
await storage.delete(NOTES)
show()
page.add(
ft.SafeArea(
content=ft.Column(
controls=[
ft.Row(
[
new_note := ft.TextField(
label="New note", on_submit=add_note
),
ft.Button("Add", on_click=add_note),
ft.OutlinedButton("Clear", on_click=clear_notes),
]
),
status := ft.Text(),
note_list := ft.Column(),
]
)
)
)
# The notes outlive the app: load what an earlier run saved.
saved = await storage.read(NOTES)
if saved is not None:
notes.extend(json.loads(saved))
show()
if __name__ == "__main__":
ft.run(main)
Methods
deleteasync
delete(name: str) -> boolDeletes a file.
Parameters:
- name (str) - The file's name.
Returns:
- bool -
Trueif a file was deleted,Falseif there was none.
existsasync
exists(name: str) -> boolChecks whether a file exists.
Parameters:
- name (str) - The file's name.
listasync
list(prefix: str='') -> list[str]Lists the app's files, including those in folders.
Parameters:
- prefix (str, default:
'') - Only names starting with this, such as"db/".
Returns:
- list[str] - The files' names, sorted, such as
["db/app.sqlite", "notes.json"].
lockasync
lock(name: str, timeout: float=5.0) -> boolTakes an advisory lock. On web it coordinates tabs on the same origin. On desktop and mobile it coordinates callers in the same Flet client process. Separate desktop processes need their own coordination.
Locks are named like files and belong to the app; they don't stop
anything by themselves, so take the lock before reading a file you'll
write back, and unlock() it after. A lock the app
still holds is released when it closes.
Parameters:
- name (str) - The lock's name, usually the file's.
- timeout (float, default:
5.0) - Seconds to wait for another copy to release it.0doesn't wait.
Returns:
- bool -
Trueif the app now holds the lock (or already did),Falseif another copy still held it aftertimeout.
readasync
read(name: str) -> bytes | NoneReads a file.
Parameters:
- name (str) - The file's name, such as
"db/app.sqlite".
Returns:
- bytes | None - The file's contents, or
Noneif there's no such file.
unlockasync
unlock(name: str) -> NoneReleases a lock taken with lock().
Parameters:
- name (str) - The lock's name.
writeasync
write(name: str, data: bytes) -> NoneCreates or replaces a file, atomically. Folders in its name are created as needed.
Parameters:
- name (str) - The file's name, such as
"db/app.sqlite". - data (bytes) - The file's new contents.
Raises:
- TypeError - If
dataisn'tbytes,bytearrayormemoryview. - RuntimeError - If the write would take an embedded app past its
FletApp.file_storage_quota.