Skip to content

Improvements to State#2100

Merged
abidlabs merged 15 commits into
mainfrom
state-fix
Aug 29, 2022
Merged

Improvements to State#2100
abidlabs merged 15 commits into
mainfrom
state-fix

Conversation

@abidlabs
Copy link
Copy Markdown
Member

@abidlabs abidlabs commented Aug 28, 2022

This PR makes some enhancements to session state. First, it introduces gr.State() as an alias of gr.Variable(), unifying the naming we use in Blocks and Interface. To use state in Interface, one can do either of these approaches:

  • gr.Interface(..., "state", "state")
  • gr.Interface(..., gr.State(), gr.State()) (previously, this would not work)

In Blocks, the same approach works as before but using gr.State() instead of gr.Variable().

Some additional tests have been added. The demos and documentation have also been updated to use gr.State(). For backwards compatibility, gr.Variable() is still accepted.

Fixes: #730

@github-actions
Copy link
Copy Markdown
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2100-all-demos

@abidlabs abidlabs changed the title Improvements to state Improvements to State Aug 28, 2022
@abidlabs abidlabs marked this pull request as ready for review August 28, 2022 15:06
Copy link
Copy Markdown
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

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

@abidlabs Looks good to me except the change in the config for gr.Variable I note in one of the comments

Comment thread demo/kitchen_sink_random/run.py
Comment thread gradio/components.py
Comment thread demo/hangman/run.py
Comment thread gradio/interface.py
3. In `guess_letter`, we pass the value of this `State` to `used_letters`, and then return an updated value of this `State` in the return statement.

With more complex apps, you will likely have many Variables storing session state in a single Blocks app.
With more complex apps, you will likely have many State variables storing session state in a single Blocks app.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In the interface_state.md guide should we mention that interfaces only support a single state variable? And point them to blocks for more complex state?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure will make the addition

@abidlabs
Copy link
Copy Markdown
Member Author

Thanks for the catches @freddyaboulton! Have updated the PR and will merge after the tests pass

@abidlabs abidlabs merged commit 4d58ae7 into main Aug 29, 2022
@abidlabs abidlabs deleted the state-fix branch August 29, 2022 16:53
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.

[Blocks] State throws a js error

2 participants