Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from Data_Fetching_and_Rendering import fetch_org_urn
|
| 3 |
|
| 4 |
-
|
| 5 |
# shared state
|
| 6 |
token_received = {"status": False, "token": "", "client_id": ""}
|
| 7 |
|
| 8 |
-
# 1) this will be called by POST
|
| 9 |
def receive_token(accessToken: dict, client_id: str):
|
| 10 |
token_received["status"] = True
|
| 11 |
token_received["token"] = accessToken
|
| 12 |
token_received["client_id"] = client_id
|
| 13 |
-
return
|
| 14 |
|
| 15 |
-
# 2) this just drives your UI
|
| 16 |
def check_status():
|
| 17 |
-
return "✅ Code received" if token_received["status"] else "❌
|
| 18 |
-
|
| 19 |
-
def show_token():
|
| 20 |
-
return token_received["token"] if token_received["status"] else ""
|
| 21 |
-
|
| 22 |
-
def show_client_id():
|
| 23 |
-
return token_received["client_id"] if token_received["status"] else ""
|
| 24 |
|
| 25 |
def reset_status():
|
| 26 |
token_received["status"] = False
|
| 27 |
token_received["token"] = ""
|
| 28 |
token_received["client_id"] = ""
|
| 29 |
-
return "❌
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
hidden_btn.
|
| 38 |
-
|
| 39 |
-
status_box
|
| 40 |
-
token_display
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
timer = gr.Timer(1.0)
|
| 53 |
-
timer.tick(fn=
|
| 54 |
-
timer.tick(fn=
|
| 55 |
-
timer.tick(outputs=urn_display)
|
| 56 |
|
| 57 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from Data_Fetching_and_Rendering import fetch_org_urn
|
| 3 |
|
|
|
|
| 4 |
# shared state
|
| 5 |
token_received = {"status": False, "token": "", "client_id": ""}
|
| 6 |
|
|
|
|
| 7 |
def receive_token(accessToken: dict, client_id: str):
|
| 8 |
token_received["status"] = True
|
| 9 |
token_received["token"] = accessToken
|
| 10 |
token_received["client_id"] = client_id
|
| 11 |
+
return "✅ Code received", accessToken, client_id
|
| 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 |
+
# these will be populated by your external POST call
|
| 30 |
+
hidden_token = gr.Textbox(visible=False, elem_id="hidden_token")
|
| 31 |
+
hidden_client_id = gr.Textbox(visible=False, elem_id="hidden_client_id")
|
| 32 |
+
hidden_btn = gr.Button(visible=False, elem_id="hidden_btn")
|
| 33 |
+
|
| 34 |
+
status_box = gr.Textbox(value=check_status(), label="Status", interactive=False)
|
| 35 |
+
token_display = gr.Textbox(value="", label="Received Token", interactive=False)
|
| 36 |
+
client_display = gr.Textbox(value="", label="Client ID", interactive=False)
|
| 37 |
+
urn_display = gr.Textbox(value="", label="Org URN", interactive=False)
|
| 38 |
+
name_display = gr.Textbox(value="", label="Org Name", interactive=False)
|
| 39 |
+
|
| 40 |
+
# when the POST comes in, this hidden button is “clicked”
|
| 41 |
+
hidden_btn.click(
|
| 42 |
+
fn=receive_token,
|
| 43 |
+
inputs=[hidden_token, hidden_client_id],
|
| 44 |
+
outputs=[status_box, token_display, client_display]
|
| 45 |
+
)
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
+
gr.Button("Refresh Status")\
|
| 49 |
+
.click(fn=check_status, outputs=status_box)
|
| 50 |
+
gr.Button("Reset")\
|
| 51 |
+
.click(fn=reset_status,
|
| 52 |
+
outputs=[status_box, token_display, client_display, urn_display, name_display])
|
| 53 |
|
| 54 |
+
# Timer to drive the fetch. Ticks every second, but our function will no-op until status=True
|
| 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 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|