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

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

Closed
hshen14 opened this issue Feb 1, 2023 · 16 comments · Fixed by #9950
Closed
Labels
enhancement New feature or request

Comments

@hshen14
Copy link

hshen14 commented Feb 1, 2023

Right now, the session state is either global shared between users or local within a page. We are seeing the needs to support session state across multiple pages.

E.g., we host a service on Hugging Face spaces, and we ask users to login with token first before using the spaces. The ideal case is users just need to login once but use the spaces multiple time without further login (even the page is refreshed, or multiple pages requires login status).

@abidlabs abidlabs added the enhancement New feature or request label Feb 1, 2023
@abidlabs abidlabs changed the title Support session state across multiple pages or page refresh Support session state across multiple pages or page refresh (e.g. with LocalStorage) Feb 10, 2023
@decent-engineer-decent-datascientist

@abidlabs is this something that's on the roadmap?

@abidlabs
Copy link
Member

We’re planning on tackling it but probably not anytime soon as we have competing priorities at the moment

@oaksharks
Copy link

Hi, abidlabs, Is there a workaround to keep chat histroy in gr.Chatbot after refreshing a page?

@decent-engineer-decent-datascientist

@abidlabs ^ that is my use case as well; curious what your recommended workaround is.

@abidlabs
Copy link
Member

Hi folks! It is possible to use localstorage by using custom javascript for now. Here's a gradio app that does this: https://huggingface.co/spaces/radames/gradio_window_localStorage

@oaksharks
Copy link

Hi @abidlabs,
How can I find the session data associated with the page after the user refreshes it using the above configuration?
I mean, although it can store data, how can i find previous gr. State?

@deckar01
Copy link
Contributor

deckar01 commented Aug 27, 2023

It would be nice to persist this on the backend so that authenticated sessions synchronize across devices also. I suspect this is actually simpler to implement. (I will assume in-memory persistence, because db-like schema migrations add significant complexity.)

Here is my current workaround:

import gradio as gr
from helper import persist

with gr.Blocks() as demo:
    foo_txt = persist(gr.Textbox())
    bar_txt = persist(gr.Textbox())

demo.launch(auth=[("a", "1"), ("b", "2")])
helper.py
from gradio.context import Context
from gradio import Request


def persist(component):
    sessions = {}

    def resume_session(value, request: Request):
        return sessions.get(request.username, value)

    def update_session(value, request: Request):
        sessions[request.username] = value

    Context.root_block.load(resume_session, inputs=[component], outputs=component)
    component.change(update_session, inputs=[component])

    return component

I suppose this could be encapsulated in gradio for the slightly sweeter gr.Textbox(persist=True) syntax. A complete solution should probably also detect the auth type and choose the session key accordingly. It is also possible to use request cookies for the session ID so that the UX feels like LocalStorage for anonymous users.

@arian81
Copy link
Contributor

arian81 commented Feb 5, 2024

@abidlabs Hey is there any update on this? I think persisting sessions would be a very useful feature and I see this issue is from a year ago.

@abidlabs
Copy link
Member

abidlabs commented Feb 5, 2024

I agree, cc @aliabid94 perhaps if he has bandwidth.

@kerthcet
Copy link

kerthcet commented Feb 6, 2024

+1 to see this happen.

@Moibe
Copy link

Moibe commented Feb 28, 2024

Hi folks! It is possible to use localstorage by using custom javascript for now. Here's a gradio app that does this: https://huggingface.co/spaces/radames/gradio_window_localStorage

It is a good example on how to set storage, but not on how to get storage. In the sample it gets the localStorage variable but never use it within the code. The results that the app display come directly from the input fields and not from the localStorage. For someone that is not an expert like me, it would have been very useful that this code also examples on how to retrieve the localStorage not just setting it.

@JackismyShephard
Copy link
Contributor

JackismyShephard commented Apr 26, 2024

@deckar01 Your solution is really neat and in my opinion preferable to using localstorage. The only problem is that it does not work with accordions or any other block types as they have no associated change function. It would be even neater if we could somehow also remember the state of accordions (i.e. whether they are closed or open and whether they are visible or not).

@xinyii
Copy link

xinyii commented Sep 14, 2024

I have implemented the functions of reading and writing localStorage and saving chat history. https://huggingface.co/spaces/YiXinCoding/gradio-chat-history

@abidlabs
Copy link
Member

That is so cool @xinyii!

@Ketzemot
Copy link

@xinyii and @abidlabs could you please share your code that you used to create your gradio apps? That would be really great.

@ROBERT-MCDOWELL
Copy link

@Ketzemot
click on files at the gradio app page!

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

Successfully merging a pull request may close this issue.