Skip to content
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

Add ability to read and write from LocalStorage #9950

Merged
merged 45 commits into from
Nov 15, 2024
Merged

Add ability to read and write from LocalStorage #9950

merged 45 commits into from
Nov 15, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Nov 12, 2024

I've added a component gr.BrowserState(), which acts similarly to gr.State() but any value written to it is saved to the browser's local storage, so it is preserved across page loads. The value is encrypted with a key, which means that one Gradio app will not be able to read the local state value of another Gradio app.

Test demo: demo/browserstate/run.py

Closes: #3106

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Nov 12, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/ec2e569baa8a9b69674ad542c99a1ddface86286/gradio-5.5.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@ec2e569baa8a9b69674ad542c99a1ddface86286#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/ec2e569baa8a9b69674ad542c99a1ddface86286/gradio-client-1.7.1.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/ec2e569baa8a9b69674ad542c99a1ddface86286/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Nov 12, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/browserstate minor
@gradio/client minor
@gradio/core minor
@gradio/utils minor
@self/app minor
@self/component-test minor
@self/spa minor
gradio minor

With the following changelog entry.

Add ability to read and write from LocalStorage

⚠️ The changeset file for this pull request has been modified manually, so the changeset generation bot has been disabled. To go back into automatic mode, delete the changeset file.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

Copy link
Contributor

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool feature 🔥

@abidlabs abidlabs requested review from julien-c and whitphx November 14, 2024 02:32
@abidlabs abidlabs marked this pull request as ready for review November 15, 2024 19:49
@abidlabs
Copy link
Member Author

Should be ready for review now, thanks @aliabid94 for helping me sort out a few Svelte things!

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for fixing the issues!

@@ -30,6 +30,7 @@
stylesheets?: string[];
path: string;
app_id?: string;
browser_state_secret?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope I’ll get rid of it

@@ -29,6 +29,7 @@
stylesheets?: string[];
path: string;
app_id?: string;
browser_state_secret?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one?

self.secret = secret or "".join(
secrets.choice(string.ascii_letters + string.digits) for _ in range(16)
)
self.storage_key = storage_key or "".join(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've never used this secrets.choice approach to generate keys - we usually use uuid. Not a big deal tho

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secrets is better as it doesn’t rely on timers etc which are guessable

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM good stuff!

@abidlabs
Copy link
Member Author

Thanks @aliabid94!

@abidlabs abidlabs merged commit fc06fe4 into main Nov 15, 2024
22 of 23 checks passed
@abidlabs abidlabs deleted the localstate branch November 15, 2024 20:56
@ROBERT-MCDOWELL
Copy link

will it be in the next release version?

@abidlabs
Copy link
Member Author

@ROBERT-MCDOWELL yes which we are planning for today / Monday

@ROBERT-MCDOWELL
Copy link

ok good to know as I had impletemented it with my own solution a week ago.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support session state across multiple pages or page refresh (e.g. with LocalStorage)
6 participants