awacke1 commited on
Commit
3784187
·
1 Parent(s): 9701bea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ block = gr.Blocks()
3
+
4
+ # Test app to get and set URL parameters for deep links to gradio spaces
5
+
6
+ def predict(text, url_params):
7
+ print(url_params)
8
+ return ["Hello " + text + "!!", url_params]
9
+
10
+ get_window_url_params = """
11
+ function(text_input, url_params) {
12
+ console.log(text_input, url_params);
13
+ const params = new URLSearchParams(window.location.search);
14
+ url_params = Object.fromEntries(params);
15
+ return [text_input, url_params];
16
+ }
17
+ """
18
+
19
+ set_window_url_params = """
20
+ function(text_input, url_params) {
21
+ const state = {text_input:text_input}
22
+ const queryString = '?' + new URLSearchParams(state).toString();
23
+ window.parent.postMessage({ queryString: queryString }, "*")
24
+ return [text_input, state];
25
+ }
26
+ """
27
+
28
+ with gr.Blocks() as demo:
29
+ with gr.Row():
30
+ url_params = gr.JSON({}, visible=True, label="URL Params")
31
+ text_input = gr.Text(label="🔍 Input")
32
+ text_output = gr.Text(label="🌟 Output")
33
+ with gr.Row():
34
+ btn = gr.Button("Get Params")
35
+ btn.click(fn=predict, inputs=[text_input, url_params],
36
+ outputs=[text_output, url_params], _js=get_window_url_params)
37
+ btn2 = gr.Button("Set Params")
38
+ btn2.click(fn=predict, inputs=[text_input, url_params],
39
+ outputs=[text_output, url_params], _js=set_window_url_params)
40
+
41
+ demo.launch(debug=True, show_error=True)