Persistence
Save a store with persist=, or a single value with persisted, between launches of the application.
janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-storeA store that saves itself
@store(persist="cart") saves the public fields in cart.json, in the data folder, and reads them when the instance is created. field(..., persist=False) leaves a field out.
from janelkit_store import field, store
@store(persist="cart")
class Cart:
items: list[dict] = []
coupon: str = field("", persist=False) # not savedCopypersisted: one value that survives a restart
persisted(key, initial) works like state, with one difference: it is saved. When created, it loads the saved value, or uses initial if there is none. Every change, including changes inside lists and dictionaries, is saved automatically. Use it for preferences and small data: the theme, the last open section, recent files.
The key belongs to the whole application: two persisted("prefs.theme", …) calls in different files return the same object, and the second default is ignored.
<script lang="python">
from janelkit_store import persisted
name = persisted("lesson.name", "")
</script>
<column padding={24} gap={12}>
<textfield bind:value={name} placeholder="Your name" label="Your name" />
<text>Hello, {name or "visitor"}.</text>
<button on:click={name = ""}>Forget my name</button>
</column>CopyComplete component · Save as src/App.janel in a scaffolded project.
How it works
- name behaves like state: bind:, .value and plain names in the template all work.
- Typing saves the value; there is no save button and no file to manage.
- Forget my name saves an empty string; it does not delete the key.
What you should see
Typing Ada shows Hello, Ada. Closing and reopening the application shows Ada in the field again.
Common mistakes
- Do not store secrets: values are plain JSON files, not encrypted storage.
- Do not reuse a key for different kinds of data; the first value loaded wins.
Keep preferences together in one module
Gather persisted values in a module such as src/services/prefs.py, and import them where needed. bind:value={prefs.theme} binds to the module’s value. To show it in a template without .value, import it by name: from services.prefs import theme.
# src/services/prefs.py
from janelkit_store import persisted
theme = persisted("prefs.theme", "light")
recent = persisted("prefs.recent", [])
def opened(path):
recent.value.insert(0, str(path)) # changes in place are saved too
del recent.value[10:]CopyWhat can be saved, and how keys are written
Only JSON values: strings, numbers, booleans, None, lists and dictionaries with string keys. A value JSON cannot represent, such as a date, a set or your own object, raises TypeError when created or assigned. Some values change shape on the way: tuples come back as lists, and dictionary keys come back as strings, so {1: "a"} is {"1": "a"} after a restart. Convert dates and paths to strings yourself. When you change the shape of saved data, migrate it yourself or use a new key.
Keys use ASCII letters, digits, dots, underscores and hyphens, and start with a letter or digit. Use dots to group them: prefs.theme, editor.font_size.
Where and when it is written
Each key, and each persisted store, is a file named <key>.json in the data folder of the session: JANELKIT_DATA_DIR if it is set, the temporary folder of janelkit.inspect.mount in tests, or the user’s data folder plus the project name: %APPDATA%\<project> on Windows, ~/Library/Application Support/<project> on macOS, ~/.config/<project> on Linux.
Changes are grouped and written at most every 300 ms, and once more when the application closes. Writing replaces the file in one step, so a crash never leaves half a file. Each change rewrites the whole file, which is why this is for small and medium data; documents and databases belong in their own files. .flush() writes what is pending at once.
Common mistakes
- Tests with janelkit.inspect.mount use an empty temporary folder, so they never touch real data. To test that a value survives two mounts, set JANELKIT_DATA_DIR.
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.