-
-
Notifications
You must be signed in to change notification settings - Fork 1
LocalStorage #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
LocalStorage #178
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,43 @@ | ||
| import uuid | ||
|
|
||
| import streamlit as st | ||
| from streamlit_local_storage import LocalStorage | ||
|
|
||
| SESSION_ID_STORAGE_KEY = "session_id" | ||
|
|
||
|
|
||
| class SessionState: | ||
| """Per-user session state wrapper. | ||
|
|
||
| The session identity is persisted in the browser's local storage so it | ||
| survives Streamlit session loss (websocket drops while a phone is locked, | ||
| page reloads). Local storage is scoped to the browser and never travels in | ||
| the URL, so sharing the room link cannot leak a user's identity. | ||
|
|
||
| See https://docs.streamlit.io/develop/api-reference/caching-and-state/st.session_state | ||
| for more details. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| if "session_id" not in st.session_state: | ||
| existing = st.query_params.get("session_id") | ||
| st.session_state.session_id = existing or str(uuid.uuid4()) | ||
| st.query_params["session_id"] = st.session_state.session_id | ||
| if SESSION_ID_STORAGE_KEY not in st.session_state: | ||
| st.session_state.session_id = load_or_create_session_id() | ||
|
|
||
| @property | ||
| def session_id(self) -> str: | ||
| return str(st.session_state.session_id) | ||
|
|
||
|
|
||
| def load_or_create_session_id() -> str: | ||
| """Return the browser's persisted session id, creating one if absent. | ||
|
|
||
| This is the single seam touching the browser-backed component, which lets | ||
| tests patch it without driving a real frontend. | ||
| """ | ||
| local_storage = LocalStorage() | ||
| existing = local_storage.getItem(SESSION_ID_STORAGE_KEY) | ||
| if existing: | ||
| return str(existing) | ||
|
|
||
| new_session_id = str(uuid.uuid4()) | ||
| local_storage.setItem(SESSION_ID_STORAGE_KEY, new_session_id) | ||
| return new_session_id | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Use the shared key constant consistently for
st.session_stateto avoid divergence.You’re checking for
SESSION_ID_STORAGE_KEYinst.session_statebut assigning via thesession_idattribute. If the constant changes, the check and assignment will diverge. Please write tost.session_state[SESSION_ID_STORAGE_KEY] = load_or_create_session_id()and have thesession_idproperty read from that key so there’s a single source of truth.Suggested implementation:
You should also search the rest of the codebase for any direct uses of
st.session_state.session_idand update them to usest.session_state[SESSION_ID_STORAGE_KEY]or thissession_idproperty, to ensure everything consistently uses the shared key constant.