gradio/guides/02_building-interfaces/03_interface-state.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
2.5 KiB
Markdown
Raw Normal View History

2022-07-25 09:18:42 +08:00
# Interface State
Rewriting parts of the README and getting started guides for 4.0 (#6767) * doc rewrite * changes * changes * tip * changes * notebook * add changeset * history * add * quickstart done * readme * changes * quickstart * changes * reorder * link * changes * changes * changes * quickstart done * readme * quickstart * quickstart' * moving around * spaces * readme * guides * guides * links * readme * readme * readme * readme * readme * readme * readme * readme * readme * readme * email address * add changeset * shorten quickstart * readme * Update README.md * readme * changes * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * changes * changes * remove gr.Interface.load * guides * changes * more changes * changes * sharing * concurrency * changes * changes * components * key features * event listeners * features * notebook * test * guides * changes * changes * changes * transitions * readme links * links * links * guides * new gif * add gif * update gif * Update guides/02_building-interfaces/01_more-on-examples.md * Update guides/03_building-with-blocks/01_blocks-and-event-listeners.md * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/02_building-interfaces/00_the-interface-class.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * changes * replace space * changes --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
2023-12-21 03:07:48 +08:00
So far, we've assumed that your demos are *stateless*: that they do not persist information beyond a single function call. What if you want to modify the behavior of your demo based on previous interactions with the demo? There are two approaches in Gradio: *global state* and *session state*.
2022-07-25 09:18:42 +08:00
## Global State
Rewriting parts of the README and getting started guides for 4.0 (#6767) * doc rewrite * changes * changes * tip * changes * notebook * add changeset * history * add * quickstart done * readme * changes * quickstart * changes * reorder * link * changes * changes * changes * quickstart done * readme * quickstart * quickstart' * moving around * spaces * readme * guides * guides * links * readme * readme * readme * readme * readme * readme * readme * readme * readme * readme * email address * add changeset * shorten quickstart * readme * Update README.md * readme * changes * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * changes * changes * remove gr.Interface.load * guides * changes * more changes * changes * sharing * concurrency * changes * changes * components * key features * event listeners * features * notebook * test * guides * changes * changes * changes * transitions * readme links * links * links * guides * new gif * add gif * update gif * Update guides/02_building-interfaces/01_more-on-examples.md * Update guides/03_building-with-blocks/01_blocks-and-event-listeners.md * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/02_building-interfaces/00_the-interface-class.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * changes * replace space * changes --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
2023-12-21 03:07:48 +08:00
If the state is something that should be accessible to all function calls and all users, you can create a variable outside the function call and access it inside the function. For example, you may load a large model outside the function and use it inside the function so that every function call does not need to reload the model.
2022-07-25 09:18:42 +08:00
$code_score_tracker
In the code above, the `scores` array is shared between all users. If multiple users are accessing this demo, their scores will all be added to the same list, and the returned top 3 scores will be collected from this shared reference.
2022-07-25 09:18:42 +08:00
## Session State
Rewriting parts of the README and getting started guides for 4.0 (#6767) * doc rewrite * changes * changes * tip * changes * notebook * add changeset * history * add * quickstart done * readme * changes * quickstart * changes * reorder * link * changes * changes * changes * quickstart done * readme * quickstart * quickstart' * moving around * spaces * readme * guides * guides * links * readme * readme * readme * readme * readme * readme * readme * readme * readme * readme * email address * add changeset * shorten quickstart * readme * Update README.md * readme * changes * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * changes * changes * remove gr.Interface.load * guides * changes * more changes * changes * sharing * concurrency * changes * changes * components * key features * event listeners * features * notebook * test * guides * changes * changes * changes * transitions * readme links * links * links * guides * new gif * add gif * update gif * Update guides/02_building-interfaces/01_more-on-examples.md * Update guides/03_building-with-blocks/01_blocks-and-event-listeners.md * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/02_building-interfaces/00_the-interface-class.md Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> * Update guides/01_getting-started/01_quickstart.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * Update guides/01_getting-started/02_key-features.md Co-authored-by: Hannah <hannahblair@users.noreply.github.com> * changes * replace space * changes --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com> Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
2023-12-21 03:07:48 +08:00
Another type of data persistence Gradio supports is session state, where data persists across multiple submits within a page session. However, data is _not_ shared between different users of your model. To store data in a session state, you need to do three things:
2022-07-25 09:18:42 +08:00
1. Pass in an extra parameter into your function, which represents the state of the interface.
2. At the end of the function, return the updated value of the state as an extra return value.
3. Add the `'state'` input and `'state'` output components when creating your `Interface`
Here's a simple app to illustrate session state - this app simply stores users previous submissions and displays them back to the user:
2022-07-25 09:18:42 +08:00
$code_interface_state
$demo_interface_state
2022-07-25 09:18:42 +08:00
Notice how the state persists across submits within each page, but if you load this demo in another tab (or refresh the page), the demos will not share chat history. Here, we could not store the submission history in a global variable, otherwise the submission history would then get jumbled between different users.
The initial value of the `State` is `None` by default. If you pass a parameter to the `value` argument of `gr.State()`, it is used as the default value of the state instead.
Note: the `Interface` class only supports a single session state variable (though it can be a list with multiple elements). For more complex use cases, you can use Blocks, [which supports multiple `State` variables](/guides/state-in-blocks/). Alternatively, if you are building a chatbot that maintains user state, consider using the `ChatInterface` abstraction, [which manages state automatically](/guides/creating-a-chatbot-fast).