Dicts
A Dict is a key-value store in an environment. Functions, Servers, Sandboxes, Python code and your Workers read and write the same entries, so it’s where they share small results and state.
Entries are JSON, and expire
Section titled “Entries are JSON, and expire”Keys and values are JSON, so Python reads them with json.loads. An entry expires 7 days after its last write:
Results · Modal Dictget("last-run")
| key | value |
|---|---|
| last-run | { ok: true } |
| a | 1 |
| b | 2 |
Declare a Dict
Section titled “Declare a Dict”import * as Modal from "@alchemy.run/modal";
export const Results = Modal.Dict("Results", {});Bind it in a runtime
Section titled “Bind it in a runtime”const results = yield* Modal.ReadWriteDict(Results);Provide Modal.ReadWriteDictHttp. Use ReadDict or WriteDict for one direction.
Write entries
Section titled “Write entries”yield* results.put("last-run", { ok: true });yield* results.update([["a", 1], ["b", 2]]);Read entries
Section titled “Read entries”const last = yield* results.get<{ ok: boolean }>("last-run");const all = yield* results.entries();Remove entries
Section titled “Remove entries”const popped = yield* results.pop("a");yield* results.clear();pop returns the value it removed.