Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +88 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
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 |
+
|
| 12 |
+
labels = list(scores.keys())
|
| 13 |
+
values = list(scores.values())
|
| 14 |
+
|
| 15 |
+
labels_loop = labels + [labels[0]]
|
| 16 |
+
values_loop = values + [values[0]]
|
| 17 |
+
|
| 18 |
+
fig = go.Figure(
|
| 19 |
+
data=go.Scatterpolar(
|
| 20 |
+
r=values_loop,
|
| 21 |
+
theta=labels_loop,
|
| 22 |
+
fill="toself",
|
| 23 |
+
name=title,
|
| 24 |
+
line=dict(width=3)
|
| 25 |
+
)
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
fig.update_layout(
|
| 29 |
+
title=title,
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
plotly
|