Ab0 commited on
Commit
de7fb85
1 Parent(s): 6727d88

Query params example.

Browse files
Files changed (1) hide show
  1. app.py +42 -4
app.py CHANGED
@@ -1,7 +1,45 @@
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)