Sessions & persistence
Every conversation is stored, with a small dict of state you control. Read this page to keep per-conversation state, choose where conversations are stored, or plug in your own database.
The snippets below are fragments; the complete example is a whole file.
Per-conversation state
Section titled “Per-conversation state”chat.session is a dict stored with the conversation. Read and write it freely. It’s saved when the handler finishes, so values must be JSON-serializable.
@app.on_messageasync def reply(msg, chat): count = chat.session.get("turns", 0) + 1 chat.session["turns"] = count yield f"This is message {count} in this conversation."Identity
Section titled “Identity”DyUI has no user accounts. Each browser gets an anonymous dyui_uid cookie (random, HttpOnly, SameSite=Lax), and conversations, files and actions are scoped to it. chat.user_id exposes it. To restrict who can open the app, see Authentication.
Storage
Section titled “Storage”App(persist=...) |
Store |
|---|---|
True (default) |
SQLite at <data_dir>/dyui.db (./.dyui/dyui.db) |
"path/to/chats.db" |
SQLite at that path |
False |
In memory; everything is lost on restart |
a Store instance |
Yours |
Uploaded files are written to <data_dir>/uploads/, and their metadata goes in the store.
Your own store
Section titled “Your own store”Implement the dyui.store.Store protocol and pass an instance as persist=. Every method is a coroutine, and records are plain dicts:
from dyui.store import Store
class PostgresStore: # implements dyui.store.Store async def create_thread(self, user_id, *, title="New chat"): ... async def get_thread(self, thread_id): ... async def list_threads(self, user_id, *, limit=200): ... async def update_thread(self, thread_id, **fields): ... # title, title_locked, session, updated_at async def delete_thread(self, thread_id): ... async def get_messages(self, thread_id): ... async def get_message(self, thread_id, message_id): ... async def put_message(self, thread_id, message): ... # insert or replace by id, keep position async def delete_messages_after(self, thread_id, message_id, *, inclusive=False): ... async def put_file(self, record): ... async def get_file(self, file_id): ... async def close(self): ...
app = dyui.App(persist=PostgresStore())The record shapes are documented on dyui.store.Store. dyui.store.MemoryStore is a short, complete reference implementation.
Complete example
Section titled “Complete example”A counter kept in chat.session, stored in an SQLite file you name. It needs no API key.
"""Per-conversation state with chat.session, stored in SQLite.
Save as app.py, then run: dyui run app.pyConversations are kept in ./chats.db; restart the app and they are still there."""import dyuifrom dyui import ui
app = dyui.App(title="Counter", persist="chats.db", starters=["Count this"])
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): count = chat.session.get("turns", 0) + 1 chat.session["turns"] = count # saved when the handler finishes; values must be JSON chat.session.setdefault("words", []).append(msg.text) yield f"This is message {count} in this conversation." yield ui.KeyValue({"Thread": chat.thread_id, "Browser": chat.user_id[:8] + "…", "Turns": count})-
Save it as
app.pyin a new, empty file, or download app.py. -
Run it:
Terminal window pip install dyuidyui run app.py -
Open http://localhost:8000 and send a few messages. Stop the app, start it again, and the conversation and its count are still there.
See also
Section titled “See also”- Authentication: who can open the app.
- Production server: storage in production.