Skip to content

Commit 4f6327b

Browse files
Load visible components in 6.0 (#12491)
1 parent d3966ca commit 4f6327b

File tree

14 files changed

+311
-59
lines changed

14 files changed

+311
-59
lines changed

.changeset/hot-coins-deny.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@gradio/accordion": patch
3+
"@gradio/colorpicker": patch
4+
"@gradio/core": patch
5+
"@gradio/tabitem": patch
6+
"@gradio/tabs": patch
7+
"gradio": patch
8+
---
9+
10+
fix:Load visible components in 6.0

demo/invisible_textbox/run.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: invisible_textbox"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", " textbox = gr.Textbox(visible=False, interactive=True, elem_id=\"test-textbox\")\n", "\n", " make_visible_btn = gr.Button(\"Show\")\n", " hide = gr.Button(\"Hide\")\n", " def show():\n", " return gr.Textbox(visible=True)\n", " make_visible_btn.click(fn=show, outputs=textbox)\n", " hide.click(lambda: gr.Textbox(visible=False), outputs=textbox)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}

demo/invisible_textbox/run.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import gradio as gr
2+
3+
with gr.Blocks() as demo:
4+
textbox = gr.Textbox(visible=False, interactive=True, elem_id="test-textbox")
5+
6+
make_visible_btn = gr.Button("Show")
7+
hide = gr.Button("Hide")
8+
def show():
9+
return gr.Textbox(visible=True)
10+
make_visible_btn.click(fn=show, outputs=textbox)
11+
hide.click(lambda: gr.Textbox(visible=False), outputs=textbox)
12+
13+
if __name__ == "__main__":
14+
demo.launch()

js/accordion/Index.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
<Accordion
3232
{label}
3333
open={gradio.props.open}
34-
on:expand={() => gradio.dispatch("expand")}
34+
on:expand={() => {
35+
gradio.dispatch("expand");
36+
gradio.dispatch("gradio_expand");
37+
}}
3538
on:collapse={() => gradio.dispatch("collapse")}
3639
>
3740
<BaseColumn>

js/colorpicker/Index.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import type { ColorPickerProps, ColorPickerEvents } from "./types";
1515
1616
let props = $props();
17-
const gradio = new Gradio<ColorPickerEvents, ColorPickerProps>(props);
18-
gradio.props.value = gradio.props.value ?? "#000000";
17+
const gradio = new Gradio<ColorPickerEvents, ColorPickerProps>(props, {
18+
value: "#000000"
19+
});
1920
let old_value = $state(gradio.props.value);
2021
let label = $derived(
2122
gradio.shared.label || gradio.i18n("color_picker.color_picker")

js/core/src/Blocks.svelte

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,6 @@
104104
}
105105
});
106106
107-
let app_tree = new AppTree(
108-
components,
109-
layout,
110-
dependencies,
111-
{
112-
root,
113-
theme: theme_mode,
114-
version,
115-
api_prefix,
116-
max_file_size,
117-
autoscroll
118-
},
119-
app,
120-
$reactive_formatter
121-
);
122-
123-
setContext(GRADIO_ROOT, {
124-
register: app_tree.register_component.bind(app_tree),
125-
dispatcher: gradio_event_dispatcher
126-
});
127-
128107
let messages: (ToastMessage & { fn_index: number })[] = $state([]);
129108
130109
function gradio_event_dispatcher(
@@ -142,6 +121,12 @@
142121
new_message("Warning", data as string, -1, event, 10, true);
143122
} else if (event === "info") {
144123
new_message("Info", data as string, -1, event, 10, true);
124+
} else if (event === "gradio_expand" || event === "gradio_tab_select") {
125+
const id_ =
126+
event === "gradio_expand"
127+
? id
128+
: (data as { component_id: number }).component_id;
129+
app_tree.render_previously_invisible_children(id_);
145130
} else if (event == "clear_status") {
146131
app_tree.update_state(
147132
id,
@@ -173,6 +158,28 @@
173158
}
174159
}
175160
161+
let app_tree = new AppTree(
162+
components,
163+
layout,
164+
dependencies,
165+
{
166+
root,
167+
theme: theme_mode,
168+
version,
169+
api_prefix,
170+
max_file_size,
171+
autoscroll
172+
},
173+
app,
174+
$reactive_formatter,
175+
gradio_event_dispatcher
176+
);
177+
178+
setContext(GRADIO_ROOT, {
179+
register: app_tree.register_component.bind(app_tree),
180+
dispatcher: gradio_event_dispatcher
181+
});
182+
176183
let api_calls: Payload[] = $state([]);
177184
let last_api_call: Payload | null = $state(null);
178185
// We need a callback to add to api_calls from the DependencyManager

js/core/src/dependency.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ export class DependencyManager {
320320
`${event_meta.event_name}-${event_meta.target_id}`
321321
);
322322
}
323-
324323
for (let i = 0; i < (deps?.length || 0); i++) {
325324
const dep = deps ? deps[i] : undefined;
326325
if (dep) {

0 commit comments

Comments
 (0)