Skip to content

Commit 4b10085

Browse files
pngwnabidlabsomerXfaruq
authored
add frontend for page load events (#967)
* Blocks load event implemented * tweks * add frontend for page load events Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: Ömer Faruk Özdemir <farukozderim@gmail.com>
1 parent 8eae59f commit 4b10085

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

demo/blocks_page_load/run.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import gradio as gr
2+
3+
def print_message(n):
4+
return "Welcome! This page has loaded for " + n
5+
6+
with gr.Blocks() as demo:
7+
t = gr.Textbox("Frank", label="Name")
8+
t2 = gr.Textbox(label="Output")
9+
demo.load(print_message, t, t2)
10+
11+
12+
if __name__ == "__main__":
13+
demo.launch()

ui/packages/app/src/Blocks.svelte

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
}
2525
2626
interface Dependency {
27-
trigger: "click" | "change";
27+
trigger: string;
2828
targets: Array<number>;
29-
inputs: Array<string>;
30-
outputs: Array<string>;
29+
inputs: Array<number>;
30+
outputs: Array<number>;
3131
queue: boolean;
3232
}
3333
@@ -121,6 +121,32 @@
121121
instance_map[t]
122122
]);
123123
124+
// page events
125+
if (
126+
targets.length === 0 &&
127+
!handled_dependencies[i]?.includes(-1) &&
128+
trigger === "load" &&
129+
// check all input + output elements are on the page
130+
outputs.every((v) => instance_map[v].instance) &&
131+
inputs.every((v) => instance_map[v].instance)
132+
) {
133+
fn(
134+
"predict",
135+
{
136+
fn_index: i,
137+
data: inputs.map((id) => instance_map[id].value)
138+
},
139+
queue,
140+
() => {}
141+
).then((output) => {
142+
output.data.forEach((value, i) => {
143+
instance_map[outputs[i]].value = value;
144+
});
145+
});
146+
147+
handled_dependencies[i] = [-1];
148+
}
149+
124150
target_instances.forEach(([id, { instance }]: [number, Instance]) => {
125151
// console.log(id, handled_dependencies[i]?.includes(id) || !instance);
126152
if (handled_dependencies[i]?.includes(id) || !instance) return;

0 commit comments

Comments
 (0)