modules browser.local_storage and browser.session_storage
This module uses the local storage defined in HTML5. The specification can be
found following this link
HTML5 local storage ?:
HTML5 defines two kinds of storage, local storage and session storage ;
the first one is persistent, i.e. keeps the data in the store when the user
closes the browser window ; the second loses the data when the browser window
HTML5 local storage is implemented in Brython under the browser package as
the following modules:
- local storage is a client-side key-value database, i.e. the data is stored in
the users browser. This means the users data is saved on their machine inside
their browser. This also means that the stored data is only available to them
when the user is on that machine and in that browser. Remember that
local storage is per browser not per computer.
- Keys and values are strings.
- Keys and values are stored persistently on a specific protocol, domain and
port. Local storage databases are scoped to an HTML5 origin, basically the
tuple (scheme, host, port, i.e.
scheme://host:port). This means that the
database is shared across all pages on the same domain, even concurrently by
multiple browser tabs. However, a page connecting over
http:// cannot see a
database that was created during an
A simple example of
This module exposes a single object,
storage, which gives acces to the
local storage. You can interact with it like a dictionary, however,
keep in mind that keys and values are restricted to strings.
This module also exposes the object
storage, which provides access to
the session storage. It is otherwise the same as above. Use
session_storage when you do not wish data to be shared across browser
sessions or tabs. A typical use case is a log-in token.
local_storage is as follows:
from browser.local_storage import storage
Now, if you close your tab, your browser or even your computer when you open
again the same browser you will have access to the values stored by the
'foo' key in the same
scheme://host:port where the key-value pair was
If you want to remove permanently a key-value pair you can use the following:
print(storage['foo']) # raises KeyError
The storage object mimics the interface of a dict object, and supports:
items return a list copy instead of a view.
A more complete example using
local_storage, a TO-DO list app, can be found
in the iframe below.