Spaces:
Runtime error
Runtime error
Query params example.
Browse files
app.py
CHANGED
@@ -1,7 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def predict(text, url_params):
|
4 |
+
print(url_params)
|
5 |
+
return ["Hello " + text + "!!", url_params]
|
6 |
|
7 |
+
|
8 |
+
get_window_url_params = """
|
9 |
+
function(text_input, url_params) {
|
10 |
+
console.log(text_input, url_params);
|
11 |
+
const params = new URLSearchParams(window.location.search);
|
12 |
+
url_params = Object.fromEntries(params);
|
13 |
+
return [text_input, url_params];
|
14 |
+
}
|
15 |
+
"""
|
16 |
+
set_window_url_params = """
|
17 |
+
function(text_input, url_params) {
|
18 |
+
const params = new URLSearchParams(window.location.search);
|
19 |
+
params.set("text_input", text_input)
|
20 |
+
url_params = Object.fromEntries(params);
|
21 |
+
const queryString = '?' + params.toString();
|
22 |
+
// this next line is only needed inside Spaces, so the child frame updates parent
|
23 |
+
window.parent.postMessage({ queryString: queryString }, "*")
|
24 |
+
return [text_input, url_params];
|
25 |
+
}
|
26 |
+
"""
|
27 |
+
with gr.Blocks() as block:
|
28 |
+
url_params = gr.JSON({}, visible=True, label="URL Params")
|
29 |
+
text_input = gr.Text(label="Input")
|
30 |
+
text_output = gr.Text(label="Output")
|
31 |
+
|
32 |
+
btn = gr.Button("Get Params")
|
33 |
+
btn.click(fn=predict, inputs=[text_input, url_params],
|
34 |
+
outputs=[text_output, url_params], _js=get_window_url_params)
|
35 |
+
|
36 |
+
btn2 = gr.Button("Set Params")
|
37 |
+
btn2.click(fn=predict, inputs=[text_input, url_params],
|
38 |
+
outputs=[text_output, url_params], _js=set_window_url_params)
|
39 |
+
block.load(
|
40 |
+
fn=predict,
|
41 |
+
inputs=[text_input, url_params],
|
42 |
+
outputs=[text_output, url_params],
|
43 |
+
_js=get_window_url_params
|
44 |
+
)
|
45 |
+
block.launch(debug=True)
|