Spaces:
Running
Running
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) | |