GuglielmoTor commited on
Commit
adb3bbe
Β·
verified Β·
1 Parent(s): 0ef5a0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -52
app.py CHANGED
@@ -1,89 +1,119 @@
 
1
  import gradio as gr
2
  import json
3
  from Data_Fetching_and_Rendering import fetch_and_render_dashboard
 
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
  try:
11
- token_dict = json.loads(accessToken.replace("'", '"')) # crude but works if trusted input
12
  except json.JSONDecodeError as e:
13
- print("Failed to parse accessToken:", e)
14
  return {
15
  "status": "❌ Invalid token format",
16
  "token": "",
17
  "client_id": client_id
18
  }
19
-
20
  token_received["status"] = True
21
  token_received["token"] = token_dict
22
  token_received["client_id"] = client_id
23
  return {
24
- "status": "βœ… Code received",
25
  "token": token_dict.get("access_token", ""),
26
  "client_id": client_id
27
  }
28
 
29
- # 2) poll for status
30
  def check_status():
31
- return "βœ… Code received" if token_received["status"] else "❌ Waiting for code…"
32
 
33
  def show_token():
34
- return token_received["token"] if token_received["status"] else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- def show_client_id():
37
- return token_received["client_id"] if token_received["status"] else ""
38
 
39
- # 3) fetch after token arrives
40
- def fetch_and_render():
41
  if not token_received["status"]:
42
- return "", ""
43
- return fetch_and_render_dashboard(token_received["client_id"], token_received["token"])
44
-
45
- # 4) reset everything
46
- def reset_status():
47
- token_received["status"] = False
48
- token_received["token"] = ""
49
- token_received["client_id"] = ""
50
- return "❌ Waiting for code…", "", "", "", ""
51
-
52
- with gr.Blocks() as demo:
53
- # Hidden inputs for POST
54
- hidden_token = gr.Textbox(visible=False, elem_id="hidden_token")
55
- hidden_client_id = gr.Textbox(visible=False, elem_id="hidden_client_id")
56
- hidden_btn = gr.Button(visible=False, elem_id="hidden_btn")
57
-
58
- # Display elements
59
- status_box = gr.Textbox(value=check_status(), label="Status", interactive=False)
60
- token_display = gr.Textbox(value=show_token(), label="Received Token", interactive=False)
61
- client_display = gr.Textbox(value=show_client_id(), label="Client ID", interactive=False)
62
- urn_display = gr.Textbox(value="", label="Org URN", interactive=False)
63
- name_display = gr.Textbox(value="", label="Org Name", interactive=False)
64
 
65
  # Wire hidden POST handler
66
  hidden_btn.click(
67
  fn=receive_token,
68
- inputs=[hidden_token, hidden_client_id],
69
  outputs=[status_box, token_display, client_display]
70
  )
71
 
72
- with gr.Row():
73
- gr.Button("Refresh Status").click(fn=check_status, outputs=status_box)
74
- gr.Button("Reset").click(
75
- fn=reset_status,
76
- outputs=[status_box, token_display, client_display, urn_display, name_display]
77
- )
78
-
79
- # Timer triggers polling and fetch
80
- # Timer triggers polling and fetch
81
  timer = gr.Timer(1.0)
82
  timer.tick(fn=check_status, outputs=status_box)
83
- timer.tick(fn=show_token, outputs=token_display) # ← ADD BACK
84
- timer.tick(fn=show_client_id, outputs=client_display) # ← ADD BACK
85
- timer.tick(fn=fetch_and_render, outputs=[urn_display])
 
 
 
 
 
 
 
 
 
 
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- # Launch
89
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
1
+ # -*- coding: utf-8 -*-
2
  import gradio as gr
3
  import json
4
  from Data_Fetching_and_Rendering import fetch_and_render_dashboard
5
+ from analytics_fetch_and_rendering import fetch_and_render_analytics
6
 
7
+ # Shared state
8
+ token_received = {"status": False, "token": None, "client_id": None}
9
 
10
+ # --- Handlers for token reception and status ---
11
+ def receive_token(accessToken: str, client_id: str):
12
+ """
13
+ Called by a hidden POST mechanism to supply the OAuth code/token and client ID.
14
+ """
15
  try:
16
+ token_dict = json.loads(accessToken.replace("'", '"'))
17
  except json.JSONDecodeError as e:
 
18
  return {
19
  "status": "❌ Invalid token format",
20
  "token": "",
21
  "client_id": client_id
22
  }
23
+
24
  token_received["status"] = True
25
  token_received["token"] = token_dict
26
  token_received["client_id"] = client_id
27
  return {
28
+ "status": "βœ… Token received",
29
  "token": token_dict.get("access_token", ""),
30
  "client_id": client_id
31
  }
32
 
 
33
  def check_status():
34
+ return "βœ… Token received" if token_received["status"] else "❌ Waiting for token…"
35
 
36
  def show_token():
37
+ return token_received["token"].get("access_token", "") if token_received["status"] else ""
38
+
39
+ def show_client():
40
+ return token_received["client_id"] or "" if token_received["status"] else ""
41
+
42
+ # --- Guarded fetch functions ---
43
+ def guarded_fetch_dashboard():
44
+ if not token_received["status"]:
45
+ return "<p style='color:red; text-align:center;'>❌ Access denied. No token available. Please send token first.</p>"
46
+ # token_received["client_id"] and token_received["token"] required by fetch function
47
+ html = fetch_and_render_dashboard(
48
+ token_received["client_id"],
49
+ token_received["token"]
50
+ )
51
+ return html
52
 
 
 
53
 
54
+ def guarded_fetch_analytics():
 
55
  if not token_received["status"]:
56
+ # return placeholder for both markdown and plot
57
+ return ("<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>", None)
58
+ count_md, plot = fetch_and_render_analytics(
59
+ token_received["client_id"],
60
+ token_received["token"]
61
+ )
62
+ return count_md, plot
63
+
64
+ # --- Build the Gradio UI ---
65
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
66
+ title="LinkedIn Post Viewer & Analytics") as app:
67
+ gr.Markdown("# πŸš€ LinkedIn Organization Post Viewer & Analytics")
68
+ gr.Markdown("Send your OAuth token via API call, then explore dashboard and analytics.")
69
+
70
+ # Hidden elements: simulate POST endpoint
71
+ hidden_token = gr.Textbox(visible=False, elem_id="hidden_token")
72
+ hidden_client = gr.Textbox(visible=False, elem_id="hidden_client_id")
73
+ hidden_btn = gr.Button(visible=False, elem_id="hidden_btn")
74
+
75
+ status_box = gr.Textbox(value=check_status(), label="Status", interactive=False)
76
+ token_display = gr.Textbox(value=show_token(), label="Access Token", interactive=False)
77
+ client_display = gr.Textbox(value=show_client(), label="Client ID", interactive=False)
78
 
79
  # Wire hidden POST handler
80
  hidden_btn.click(
81
  fn=receive_token,
82
+ inputs=[hidden_token, hidden_client],
83
  outputs=[status_box, token_display, client_display]
84
  )
85
 
86
+ # Polling timer to update status and displays
 
 
 
 
 
 
 
 
87
  timer = gr.Timer(1.0)
88
  timer.tick(fn=check_status, outputs=status_box)
89
+ timer.tick(fn=show_token, outputs=token_display)
90
+ timer.tick(fn=show_client, outputs=client_display)
91
+
92
+ # Tabs for functionality
93
+ with gr.Tabs():
94
+ with gr.TabItem("1️⃣ Dashboard"):
95
+ gr.Markdown("View your organization's recent posts and their engagement statistics.")
96
+ fetch_dashboard_btn = gr.Button("πŸ“Š Fetch Posts & Stats", variant="primary")
97
+ dashboard_html = gr.HTML(value="<p style='text-align: center; color: #555;'>Waiting for token...</p>")
98
+ fetch_dashboard_btn.click(
99
+ fn=guarded_fetch_dashboard,
100
+ inputs=[],
101
+ outputs=[dashboard_html]
102
+ )
103
 
104
+ with gr.TabItem("2️⃣ Analytics"):
105
+ gr.Markdown("View follower count and monthly gains for your organization.")
106
+ fetch_analytics_btn = gr.Button("πŸ“ˆ Fetch Follower Analytics", variant="primary")
107
+ follower_count = gr.Markdown(
108
+ "<p style='text-align: center; color: #555;'>Waiting for token...</p>"
109
+ )
110
+ follower_plot = gr.Plot(visible=False)
111
+ fetch_analytics_btn.click(
112
+ fn=guarded_fetch_analytics,
113
+ inputs=[],
114
+ outputs=[follower_count, follower_plot]
115
+ )
116
 
117
+ # Launch the app
118
+ if __name__ == "__main__":
119
+ app.launch(server_name="0.0.0.0", server_port=7860, share=True)