@@ -372,6 +372,80 @@ The following code demonstrates a `pyscript.WebSocket` in action.
372372 ws = WebSocket(url="ws://example.com/socket", onmessage=onmessage)
373373 ```
374374
375+ ### ` pyscript.storage `
376+
377+ The ` pyscript.storage ` API wraps the browser's built-in
378+ [ IndexDB] ( https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API )
379+ persistent storage in a synchronous Pythonic API.
380+
381+ !!! info
382+
383+ The storage API is persistent per user tab, page, or domain, in the same
384+ way IndexedDB persists.
385+
386+ This API **is not** saving files in the interpreter's virtual file system
387+ nor onto the user's hard drive.
388+
389+ ``` python
390+ from pyscript import storage
391+
392+
393+ # Each store must have a meaningful name.
394+ store = await storage(" my-storage-name" )
395+
396+ # store is a dictionary and can now be used as such.
397+ ```
398+
399+ The returned dictionary automatically loads the current state of the referenced
400+ IndexDB. All changes are automatically queued in the background.
401+
402+ ``` python
403+ # This is a write operation.
404+ store[" key" ] = value
405+
406+ # This is also a write operation (it changes the stored data).
407+ del store[" key" ]
408+ ```
409+
410+ Should you wish to be certain changes have been synchronized to the underlying
411+ IndexDB, just ` await store.sync() ` .
412+
413+ Common types of value can be stored via this API: ` bool ` , ` float ` , ` int ` , ` str `
414+ and ` None ` . In addition, data structures like ` list ` , ` dict ` and ` tuple ` can
415+ be stored.
416+
417+ !!! warning
418+
419+ Because of the way the underlying data structure are stored in IndexDB,
420+ a Python `tuple` will always be returned as a Python `list`.
421+
422+ It is even possible to store arbitrary data via a ` bytearray ` or
423+ ` memoryview ` object. However, there is a limitation that ** such values must be
424+ stored as a single key/value pair, and not as part of a nested data
425+ structure** .
426+
427+ Sometimes you may need to modify the behaviour of the ` dict ` like object
428+ returned by ` pyscript.storage ` . To do this, create a new class that inherits
429+ from ` pyscript.Storage ` , then pass in your class to ` pyscript.storage ` as the
430+ ` storage_class ` argument:
431+
432+ ``` python
433+ from pyscript import window, storage, Storage
434+
435+
436+ class MyStorage (Storage ):
437+
438+ def __setitem__ (self , key , value ):
439+ super ().__setitem__ (key, value)
440+ window.console.log(key, value)
441+ ...
442+
443+
444+ store = await storage(" my-data-store" , storage_class = MyStorage)
445+
446+ # The store object is now an instance of MyStorage.
447+ ```
448+
375449### ` pyscript.ffi.to_js `
376450
377451A utility function to convert Python references into their JavaScript
0 commit comments