File size: 2,470 Bytes
b560569
a39570d
506f1ee
751e28f
a0b418d
b560569
e1e0cc1
b560569
0f9e24f
a0b418d
73e88eb
b560569
 
73e88eb
6e2376b
734a45c
 
0f9e24f
6e2376b
73e88eb
734a45c
73e88eb
 
 
 
 
506f1ee
b560569
73e88eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3447a5
e1e0cc1
73e88eb
 
 
 
 
e1e0cc1
73e88eb
d45a226
73e88eb
 
7ab0240
73e88eb
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
54
55
56
57
58
59
60
import gradio as gr
from Data_Fetching_and_Rendering import fetch_org_urn

# shared state
token_received = {"status": False, "token": "", "client_id": ""}

def receive_token(accessToken: dict, client_id: str):
    token_received["status"] = True
    token_received["token"] = accessToken
    token_received["client_id"] = client_id
    return "✅ Code received", accessToken, client_id

def check_status():
    return "✅ Code received" if token_received["status"] else "❌ Waiting for code…"

def reset_status():
    token_received["status"] = False
    token_received["token"] = ""
    token_received["client_id"] = ""
    return "❌ Waiting for code…", "", "", "", ""

# This function is only called *after* we've got the token.
def fetch_and_render(_):
    if not token_received["status"]:
        return "", ""
    return fetch_org_urn(token_received["client_id"], token_received["token"])

with gr.Blocks() as demo:
    # these will be populated by your external POST call
    hidden_token     = gr.Textbox(visible=False,  elem_id="hidden_token")
    hidden_client_id = gr.Textbox(visible=False,  elem_id="hidden_client_id")
    hidden_btn       = gr.Button(visible=False,   elem_id="hidden_btn")

    status_box       = gr.Textbox(value=check_status(), label="Status", interactive=False)
    token_display    = gr.Textbox(value="", label="Received Token",    interactive=False)
    client_display   = gr.Textbox(value="", label="Client ID",         interactive=False)
    urn_display      = gr.Textbox(value="", label="Org URN",            interactive=False)
    name_display     = gr.Textbox(value="", label="Org Name",           interactive=False)

    # when the POST comes in, this hidden button is “clicked”
    hidden_btn.click(
        fn=receive_token,
        inputs=[hidden_token, hidden_client_id],
        outputs=[status_box, token_display, client_display]
    )

    with gr.Row():
        gr.Button("Refresh Status")\
          .click(fn=check_status, outputs=status_box)
        gr.Button("Reset")\
          .click(fn=reset_status, 
                 outputs=[status_box, token_display, client_display, urn_display, name_display])

    # Timer to drive the fetch. Ticks every second, but our function will no-op until status=True
    timer = gr.Timer(1.0)
    timer.tick(fn=check_status, outputs=status_box)
    timer.tick(fn=fetch_and_render, outputs=[urn_display, name_display])

demo.launch(server_name="0.0.0.0", server_port=7860)