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 guide on Client state and and fix default values of components #7830

Merged
merged 11 commits into from Mar 26, 2024
5 changes: 5 additions & 0 deletions .changeset/puny-planets-cover.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

feat:Add guide on Client state and and fix default values of components
8 changes: 4 additions & 4 deletions client/python/test/test_client.py
Expand Up @@ -1083,8 +1083,8 @@ def test_layout_components_in_output(self, hello_world_with_group):
{
"label": "name",
"parameter_name": "name",
"parameter_has_default": True,
"parameter_default": "",
"parameter_has_default": False,
"parameter_default": None,
"type": {"type": "string"},
"python_type": {"type": "str", "description": ""},
"component": "Textbox",
Expand Down Expand Up @@ -1117,8 +1117,8 @@ def test_layout_and_state_components_in_output(
{
"label": "name",
"parameter_name": "name",
"parameter_has_default": True,
"parameter_default": "",
"parameter_has_default": False,
"parameter_default": None,
"type": {"type": "string"},
"python_type": {"type": "str", "description": ""},
"component": "Textbox",
Expand Down
2 changes: 1 addition & 1 deletion gradio/_simple_templates/simpletextbox.py
Expand Up @@ -19,7 +19,7 @@ class SimpleTextbox(FormComponent):

def __init__(
self,
value: str | Callable | None = "",
value: str | Callable | None = None,
*,
placeholder: str | None = None,
label: str | None = None,
Expand Down
2 changes: 1 addition & 1 deletion gradio/components/html.py
Expand Up @@ -23,7 +23,7 @@ class HTML(Component):

def __init__(
self,
value: str | Callable = "",
value: str | Callable | None = None,
*,
label: str | None = None,
every: float | None = None,
Expand Down
2 changes: 1 addition & 1 deletion gradio/components/markdown.py
Expand Up @@ -25,7 +25,7 @@ class Markdown(Component):

def __init__(
self,
value: str | Callable = "",
value: str | Callable | None = None,
*,
label: str | None = None,
every: float | None = None,
Expand Down
2 changes: 1 addition & 1 deletion gradio/components/textbox.py
Expand Up @@ -30,7 +30,7 @@ class Textbox(FormComponent):

def __init__(
self,
value: str | Callable | None = "",
value: str | Callable | None = None,
*,
lines: int = 1,
max_lines: int = 20,
Expand Down
2 changes: 1 addition & 1 deletion gradio/templates.py
Expand Up @@ -20,7 +20,7 @@ class TextArea(components.Textbox):

def __init__(
self,
value: str | Callable | None = "",
value: str | Callable | None = None,
*,
lines: int = 7,
max_lines: int = 20,
Expand Down
Expand Up @@ -295,3 +295,40 @@ job = client.submit("abcdef")
time.sleep(3)
job.cancel() # job cancels after 2 iterations
```

## Demos with Session State

Gradio demos can include [session state](https://www.gradio.app/guides/state-in-blocks), which provides a way for demos to persist information from user interactions within a page session.

For example, consider the following demo, which maintains a list of words that a user has submitted in a `gr.State` component. When a user submits a new word, it is added to the state, and the number of previous occurrences of that word is displayed:

```python
import gradio as gr

def count(word, list_of_words):
return list_of_words.count(word), list_of_words + [word]

with gr.Blocks() as demo:
words = gr.State([])
textbox = gr.Textbox()
number = gr.Number()
textbox.submit(count, inputs=[textbox, words], outputs=[number, words])

demo.launch()
```

If you were to connect this this Gradio app using the Python Client, you would notice that the API information only shows a single input and output:

```csv
Client.predict() Usage Info
---------------------------
Named API endpoints: 1

- predict(word, api_name="/count") -> value_31
Parameters:
- [Textbox] word: str (required)
Returns:
- [Number] value_31: float
```

That is because the Python client handles state automatically for you -- as you make a series of requests, the returned state from one request is stored internally and automatically supplied for the subsequent request. If you'd like to reset the state, you can do that by calling `Client.reset_session()`.
2 changes: 1 addition & 1 deletion test/test_components.py
Expand Up @@ -84,7 +84,7 @@ def test_component_functions(self):
"lines": 1,
"max_lines": 20,
"placeholder": None,
"value": "",
"value": None,
"name": "textbox",
"show_copy_button": False,
"show_label": True,
Expand Down