Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,40 +4,54 @@ from Data_Fetching_and_Rendering import fetch_org_urn
|
|
4 |
# shared state
|
5 |
token_received = {"status": False, "token": "", "client_id": ""}
|
6 |
|
7 |
-
|
|
|
8 |
token_received["status"] = True
|
9 |
token_received["token"] = accessToken
|
10 |
token_received["client_id"] = client_id
|
11 |
-
return
|
|
|
|
|
|
|
|
|
12 |
|
|
|
13 |
def check_status():
|
14 |
return "✅ Code received" if token_received["status"] else "❌ Waiting for code…"
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def reset_status():
|
17 |
token_received["status"] = False
|
18 |
token_received["token"] = ""
|
19 |
token_received["client_id"] = ""
|
20 |
return "❌ Waiting for code…", "", "", "", ""
|
21 |
|
22 |
-
# This function is only called *after* we've got the token.
|
23 |
-
def fetch_and_render(_):
|
24 |
-
if not token_received["status"]:
|
25 |
-
return "", ""
|
26 |
-
return fetch_org_urn(token_received["client_id"], token_received["token"])
|
27 |
-
|
28 |
with gr.Blocks() as demo:
|
29 |
-
#
|
30 |
-
hidden_token = gr.Textbox(visible=False,
|
31 |
-
hidden_client_id = gr.Textbox(visible=False,
|
32 |
-
hidden_btn = gr.Button(visible=False,
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
hidden_btn.click(
|
42 |
fn=receive_token,
|
43 |
inputs=[hidden_token, hidden_client_id],
|
@@ -45,15 +59,16 @@ with gr.Blocks() as demo:
|
|
45 |
)
|
46 |
|
47 |
with gr.Row():
|
48 |
-
gr.Button("Refresh Status")
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
# Timer
|
55 |
timer = gr.Timer(1.0)
|
56 |
timer.tick(fn=check_status, outputs=status_box)
|
57 |
timer.tick(fn=fetch_and_render, outputs=[urn_display, name_display])
|
58 |
|
59 |
-
|
|
|
|
4 |
# shared state
|
5 |
token_received = {"status": False, "token": "", "client_id": ""}
|
6 |
|
7 |
+
# 1) this will be called by POST
|
8 |
+
def receive_token(accessToken: str, client_id: str):
|
9 |
token_received["status"] = True
|
10 |
token_received["token"] = accessToken
|
11 |
token_received["client_id"] = client_id
|
12 |
+
return {
|
13 |
+
"status": "✅ Code received",
|
14 |
+
"token": accessToken,
|
15 |
+
"client_id": client_id
|
16 |
+
}
|
17 |
|
18 |
+
# 2) poll for status
|
19 |
def check_status():
|
20 |
return "✅ Code received" if token_received["status"] else "❌ Waiting for code…"
|
21 |
|
22 |
+
def show_token():
|
23 |
+
return token_received["token"] if token_received["status"] else ""
|
24 |
+
|
25 |
+
def show_client_id():
|
26 |
+
return token_received["client_id"] if token_received["status"] else ""
|
27 |
+
|
28 |
+
# 3) fetch after token arrives
|
29 |
+
def fetch_and_render():
|
30 |
+
if not token_received["status"]:
|
31 |
+
return "", ""
|
32 |
+
return fetch_org_urn(token_received["client_id"], token_received["token"])
|
33 |
+
|
34 |
+
# 4) reset everything
|
35 |
def reset_status():
|
36 |
token_received["status"] = False
|
37 |
token_received["token"] = ""
|
38 |
token_received["client_id"] = ""
|
39 |
return "❌ Waiting for code…", "", "", "", ""
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
with gr.Blocks() as demo:
|
42 |
+
# Hidden inputs for POST
|
43 |
+
hidden_token = gr.Textbox(visible=False, elem_id="hidden_token")
|
44 |
+
hidden_client_id = gr.Textbox(visible=False, elem_id="hidden_client_id")
|
45 |
+
hidden_btn = gr.Button(visible=False, elem_id="hidden_btn")
|
46 |
+
|
47 |
+
# Display elements
|
48 |
+
status_box = gr.Textbox(value=check_status(), label="Status", interactive=False)
|
49 |
+
token_display = gr.Textbox(value=show_token(), label="Received Token", interactive=False)
|
50 |
+
client_display = gr.Textbox(value=show_client_id(), label="Client ID", interactive=False)
|
51 |
+
urn_display = gr.Textbox(value="", label="Org URN", interactive=False)
|
52 |
+
name_display = gr.Textbox(value="", label="Org Name", interactive=False)
|
53 |
+
|
54 |
+
# Wire hidden POST handler
|
55 |
hidden_btn.click(
|
56 |
fn=receive_token,
|
57 |
inputs=[hidden_token, hidden_client_id],
|
|
|
59 |
)
|
60 |
|
61 |
with gr.Row():
|
62 |
+
gr.Button("Refresh Status").click(fn=check_status, outputs=status_box)
|
63 |
+
gr.Button("Reset").click(
|
64 |
+
fn=reset_status,
|
65 |
+
outputs=[status_box, token_display, client_display, urn_display, name_display]
|
66 |
+
)
|
67 |
|
68 |
+
# Timer triggers polling and fetch
|
69 |
timer = gr.Timer(1.0)
|
70 |
timer.tick(fn=check_status, outputs=status_box)
|
71 |
timer.tick(fn=fetch_and_render, outputs=[urn_display, name_display])
|
72 |
|
73 |
+
# Launch
|
74 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|