zsolnai commited on
Commit
8b24ca6
Β·
1 Parent(s): e46c3db

ui: add fetch on click tab

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -20,13 +20,11 @@ def get_data():
20
  df = pd.DataFrame(predictions_list)
21
 
22
  if not df.empty:
23
- df = df.sort_values(
24
- by="probability", ascending=True
25
- ) # Ascending for horizontal bar
26
 
27
- # Create Plotly Chart
28
  fig = px.bar(
29
- df,
30
  x="probability",
31
  y="repo_name",
32
  orientation="h",
@@ -38,12 +36,15 @@ def get_data():
38
  "language": "Language",
39
  },
40
  template="plotly_dark",
 
41
  )
42
  fig.update_layout(
43
- paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)"
 
 
44
  )
45
 
46
- # Prepare Table
47
  table_df = df.sort_values(by="probability", ascending=False).copy()
48
  table_df["repo_name"] = table_df["repo_name"].apply(
49
  lambda x: f'<a href="https://github.com/{x}" target="_blank" style="color: #58a6ff; text-decoration: none; font-weight: bold;">πŸ”— {x}</a>'
@@ -60,23 +61,29 @@ def get_data():
60
  return pd.DataFrame({"Error": [str(e)]}), "⚠️ Error loading data", None
61
 
62
 
63
- with gr.Blocks() as demo:
64
  gr.HTML(
65
- '<div style="text-align: center; padding: 20px;"><h1 style="color: white;">πŸ“ˆ GitHub Trend Predictor</h1></div>'
66
  )
67
- date_display = gr.Markdown(value="Checking for updates...")
68
 
69
- with gr.Tabs():
70
- with gr.Tab("πŸ“Š Visual Forecast"):
71
- chart = gr.Plot() # Stable Plotly container
72
 
73
- with gr.Tab("πŸ“‹ Detailed Rankings"):
 
 
 
 
 
74
  table = gr.Dataframe(datatype=["html", "str", "str"], interactive=False)
75
 
76
- refresh_btn = gr.Button("πŸ”„ Sync Latest Predictions", variant="primary")
 
 
77
  demo.load(fn=get_data, outputs=[table, date_display, chart])
78
- refresh_btn.click(fn=get_data, outputs=[table, date_display, chart])
 
 
79
 
80
  if __name__ == "__main__":
81
- # We remove CSS/Theme from here to ensure the core app starts even if Gradio 6.x config is strict
82
  demo.launch()
 
20
  df = pd.DataFrame(predictions_list)
21
 
22
  if not df.empty:
23
+ # Sort for Chart (Bottom-up for horizontal)
24
+ chart_df = df.sort_values(by="probability", ascending=True)
 
25
 
 
26
  fig = px.bar(
27
+ chart_df,
28
  x="probability",
29
  y="repo_name",
30
  orientation="h",
 
36
  "language": "Language",
37
  },
38
  template="plotly_dark",
39
+ color_discrete_sequence=px.colors.qualitative.Safe,
40
  )
41
  fig.update_layout(
42
+ paper_bgcolor="rgba(0,0,0,0)",
43
+ plot_bgcolor="rgba(0,0,0,0)",
44
+ font_color="#8b949e",
45
  )
46
 
47
+ # Prepare Table (Top-down)
48
  table_df = df.sort_values(by="probability", ascending=False).copy()
49
  table_df["repo_name"] = table_df["repo_name"].apply(
50
  lambda x: f'<a href="https://github.com/{x}" target="_blank" style="color: #58a6ff; text-decoration: none; font-weight: bold;">πŸ”— {x}</a>'
 
61
  return pd.DataFrame({"Error": [str(e)]}), "⚠️ Error loading data", None
62
 
63
 
64
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
65
  gr.HTML(
66
+ '<div style="text-align: center; padding: 10px;"><h1 style="color: #f0f6fc;">πŸ“ˆ GitHub Trend Predictor</h1></div>'
67
  )
 
68
 
69
+ with gr.Row():
70
+ date_display = gr.Markdown(value="Syncing...", style={"text-align": "center"})
 
71
 
72
+ # We wrap the tabs in a variable to attach the listener
73
+ with gr.Tabs() as tabs:
74
+ with gr.Tab("πŸ“Š Visual Forecast", id="chart_tab"):
75
+ chart = gr.Plot()
76
+
77
+ with gr.Tab("πŸ“‹ Detailed Rankings", id="list_tab"):
78
  table = gr.Dataframe(datatype=["html", "str", "str"], interactive=False)
79
 
80
+ # --- EVENT LISTENERS ---
81
+
82
+ # 1. Sync when the page first loads
83
  demo.load(fn=get_data, outputs=[table, date_display, chart])
84
+
85
+ # 2. Sync whenever a user clicks/switches between tabs
86
+ tabs.select(fn=get_data, outputs=[table, date_display, chart])
87
 
88
  if __name__ == "__main__":
 
89
  demo.launch()