File size: 1,676 Bytes
9a942f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55f75e4
70683fd
 
55f75e4
70683fd
 
 
 
 
6f8aec0
684a7eb
 
 
b1f6547
684a7eb
 
 
d0d6a68
9a942f0
 
 
 
684a7eb
9a942f0
 
 
684a7eb
 
 
55f75e4
d4d97b7
55f75e4
70683fd
 
 
d4d97b7
9a942f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr

def predict(text, url_params):
    print(url_params)
    return ["Hello " + text + "!!", url_params]


get_window_url_params = """
    function(text_input, url_params) {
        console.log(text_input, url_params);
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        return [text_input, url_params];
        }
    """

get_window_url_params_s = """
    function(url_params) {
        console.log(url_params);
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        return [url_params];
        }
    """
set_window_url_params = """
    function(text_input, url_params) {
            const state = {text_input:text_input}
    	    const queryString = '?' + new URLSearchParams(state).toString();
            window.parent.postMessage({ queryString: queryString }, "*")
            return [text_input, state];
        }
    """
with gr.Blocks() as block:
    url_params = gr.JSON({}, visible=True, label="URL Params")
    text_input = gr.Text(label="Input")
    text_output = gr.Text(label="Output")

    btn = gr.Button("Get Params")
    btn.click(fn=predict, inputs=[text_input, url_params],
              outputs=[text_output, url_params], _js=get_window_url_params)

    btn2 = gr.Button("Set Params")
    btn2.click(fn=predict, inputs=[text_input, url_params],
              outputs=[text_output, url_params], _js=set_window_url_params)
    
    block.load(
        fn=lambda x:x,
        inputs=[url_params],
        outputs=[url_params],
        _js=get_window_url_params_s
    )   
block.launch(debug=True)