mfallahian commited on
Commit
a97b4c9
·
verified ·
1 Parent(s): 23c53b2

Upload app.py

Browse files

add more charts

Files changed (1) hide show
  1. app.py +57 -27
app.py CHANGED
@@ -5,7 +5,7 @@ import plotly.io as pio
5
 
6
  pio.templates.default = "plotly_white"
7
 
8
- def plot_spyder(scores: dict, title: str = "Group 1 Chart", range_max: float | None = None) -> go.Figure:
9
  if not scores:
10
  raise ValueError("scores dict is empty")
11
 
@@ -21,7 +21,7 @@ def plot_spyder(scores: dict, title: str = "Group 1 Chart", range_max: float | N
21
  theta=labels_loop,
22
  fill="toself",
23
  name=title,
24
- line=dict(width=3)
25
  )
26
  )
27
 
@@ -30,59 +30,89 @@ def plot_spyder(scores: dict, title: str = "Group 1 Chart", range_max: float | N
30
  polar=dict(
31
  radialaxis=dict(
32
  visible=True,
33
- range=[0, range_max if range_max is not None else max(values)]
34
  )
35
  ),
36
- showlegend=False
37
  )
38
 
39
  return fig
40
 
41
  def build_scores(df: pd.DataFrame) -> dict:
42
- # Expect columns: label, value
43
  df = df.dropna(subset=["label", "value"])
44
  df["label"] = df["label"].astype(str).str.strip()
45
  df = df[df["label"] != ""]
46
  return dict(zip(df["label"], df["value"].astype(float)))
47
 
48
- def make_chart(df: pd.DataFrame, title: str, range_max: float) -> go.Figure:
49
- scores = build_scores(df)
50
  rm = None if range_max <= 0 else range_max
51
- return plot_spyder(scores, title=title, range_max=rm)
 
 
 
 
 
52
 
53
  default_df = pd.DataFrame(
54
  {
55
  "label": [
56
  "Affordability", "Control", "Difficulty", "Energy Efficiency Gains",
57
- "Partnership Effort", "Risk Reduction", "Scale", "Adverse Outcome"
58
  ],
59
- "value": [9, 2, 8, 3, 6, 6, 5, 4]
60
  }
61
  )
62
 
63
  with gr.Blocks() as demo:
64
- gr.Markdown("## Radar (Spider) Chart Builder")
 
 
 
65
  with gr.Row():
66
- data_input = gr.Dataframe(
67
- value=default_df,
68
- headers=["label", "value"],
69
- datatype=["str", "number"],
70
- row_count=(8, "dynamic"),
71
- column_count=(2, "fixed"),
72
- interactive=True,
73
- label="Metrics (label, value)"
74
- )
75
  with gr.Column():
76
- title_input = gr.Textbox(value="Group 1 Chart", label="Chart Title")
77
- range_input = gr.Number(value=10, label="Range Max (0 = auto)")
78
- plot_button = gr.Button("Plot")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- plot_output = gr.Plot(label="Radar Chart")
 
 
 
 
 
81
 
82
  plot_button.click(
83
- fn=make_chart,
84
- inputs=[data_input, title_input, range_input],
85
- outputs=[plot_output]
86
  )
87
 
88
  demo.launch()
 
5
 
6
  pio.templates.default = "plotly_white"
7
 
8
+ def plot_spyder(scores: dict, title: str = "Group Chart", range_max: float | None = None) -> go.Figure:
9
  if not scores:
10
  raise ValueError("scores dict is empty")
11
 
 
21
  theta=labels_loop,
22
  fill="toself",
23
  name=title,
24
+ line=dict(width=3),
25
  )
26
  )
27
 
 
30
  polar=dict(
31
  radialaxis=dict(
32
  visible=True,
33
+ range=[0, range_max if range_max is not None else max(values)],
34
  )
35
  ),
36
+ showlegend=False,
37
  )
38
 
39
  return fig
40
 
41
  def build_scores(df: pd.DataFrame) -> dict:
 
42
  df = df.dropna(subset=["label", "value"])
43
  df["label"] = df["label"].astype(str).str.strip()
44
  df = df[df["label"] != ""]
45
  return dict(zip(df["label"], df["value"].astype(float)))
46
 
47
+ def make_charts(df1, t1, df2, t2, df3, t3, df4, t4, range_max):
 
48
  rm = None if range_max <= 0 else range_max
49
+ return (
50
+ plot_spyder(build_scores(df1), t1, rm),
51
+ plot_spyder(build_scores(df2), t2, rm),
52
+ plot_spyder(build_scores(df3), t3, rm),
53
+ plot_spyder(build_scores(df4), t4, rm),
54
+ )
55
 
56
  default_df = pd.DataFrame(
57
  {
58
  "label": [
59
  "Affordability", "Control", "Difficulty", "Energy Efficiency Gains",
60
+ "Partnership Effort", "Risk Reduction", "Scale", "Adverse Outcome",
61
  ],
62
+ "value": [9, 2, 8, 3, 6, 6, 5, 4],
63
  }
64
  )
65
 
66
  with gr.Blocks() as demo:
67
+ gr.Markdown("## Radar (Spider) Chart Builder — 4 Charts")
68
+
69
+ range_input = gr.Number(value=10, label="Shared Range Max (0 = auto)")
70
+
71
  with gr.Row():
 
 
 
 
 
 
 
 
 
72
  with gr.Column():
73
+ df1 = gr.Dataframe(
74
+ value=default_df, headers=["label", "value"],
75
+ datatype=["str", "number"], row_count=(8, "dynamic"),
76
+ column_count=(2, "fixed"), interactive=True, label="Chart 1 Data"
77
+ )
78
+ t1 = gr.Textbox(value="Group 1 Chart", label="Chart 1 Title")
79
+ with gr.Column():
80
+ df2 = gr.Dataframe(
81
+ value=default_df, headers=["label", "value"],
82
+ datatype=["str", "number"], row_count=(8, "dynamic"),
83
+ column_count=(2, "fixed"), interactive=True, label="Chart 2 Data"
84
+ )
85
+ t2 = gr.Textbox(value="Group 2 Chart", label="Chart 2 Title")
86
+
87
+ with gr.Row():
88
+ with gr.Column():
89
+ df3 = gr.Dataframe(
90
+ value=default_df, headers=["label", "value"],
91
+ datatype=["str", "number"], row_count=(8, "dynamic"),
92
+ column_count=(2, "fixed"), interactive=True, label="Chart 3 Data"
93
+ )
94
+ t3 = gr.Textbox(value="Group 3 Chart", label="Chart 3 Title")
95
+ with gr.Column():
96
+ df4 = gr.Dataframe(
97
+ value=default_df, headers=["label", "value"],
98
+ datatype=["str", "number"], row_count=(8, "dynamic"),
99
+ column_count=(2, "fixed"), interactive=True, label="Chart 4 Data"
100
+ )
101
+ t4 = gr.Textbox(value="Group 4 Chart", label="Chart 4 Title")
102
+
103
+ plot_button = gr.Button("Plot All")
104
 
105
+ with gr.Row():
106
+ out1 = gr.Plot(label="Chart 1")
107
+ out2 = gr.Plot(label="Chart 2")
108
+ with gr.Row():
109
+ out3 = gr.Plot(label="Chart 3")
110
+ out4 = gr.Plot(label="Chart 4")
111
 
112
  plot_button.click(
113
+ fn=make_charts,
114
+ inputs=[df1, t1, df2, t2, df3, t3, df4, t4, range_input],
115
+ outputs=[out1, out2, out3, out4],
116
  )
117
 
118
  demo.launch()