Spaces:
Sleeping
Sleeping
Commit
路
7dae805
1
Parent(s):
5be3cef
Include Multiselect
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from bokeh.plotting import figure
|
| 4 |
-
from bokeh.models import ColumnDataSource
|
| 5 |
from bokeh.palettes import Category10
|
| 6 |
-
from bokeh.transform import factor_cmap
|
| 7 |
|
| 8 |
# --- Define Styles ---
|
| 9 |
st.markdown(
|
|
@@ -32,27 +31,17 @@ st.markdown('<h2 class="sub-title">Donut</h2>', unsafe_allow_html=True)
|
|
| 32 |
st.markdown(
|
| 33 |
"""
|
| 34 |
<p class="custom-text">
|
| 35 |
-
Explore how Donut
|
| 36 |
</p>
|
| 37 |
""",
|
| 38 |
unsafe_allow_html=True
|
| 39 |
)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
df = pd.read_csv("data/data.csv")
|
| 43 |
-
|
| 44 |
-
source = ColumnDataSource(data=dict(
|
| 45 |
-
x=df['x'],
|
| 46 |
-
y=df['y'],
|
| 47 |
-
label=df['label'],
|
| 48 |
-
img=df['img']
|
| 49 |
-
))
|
| 50 |
-
|
| 51 |
unique_labels = df['label'].unique().tolist()
|
| 52 |
-
palette = Category10[len(unique_labels)] if len(unique_labels) <= 10 else Category10[10]
|
| 53 |
-
|
| 54 |
|
| 55 |
-
#
|
| 56 |
TOOLTIPS = """
|
| 57 |
<div>
|
| 58 |
<div>
|
|
@@ -64,8 +53,53 @@ TOOLTIPS = """
|
|
| 64 |
</div>
|
| 65 |
"""
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from bokeh.plotting import figure
|
| 4 |
+
from bokeh.models import ColumnDataSource
|
| 5 |
from bokeh.palettes import Category10
|
|
|
|
| 6 |
|
| 7 |
# --- Define Styles ---
|
| 8 |
st.markdown(
|
|
|
|
| 31 |
st.markdown(
|
| 32 |
"""
|
| 33 |
<p class="custom-text">
|
| 34 |
+
Explore how Donut perceives real data.
|
| 35 |
</p>
|
| 36 |
""",
|
| 37 |
unsafe_allow_html=True
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Cargar el CSV
|
| 41 |
df = pd.read_csv("data/data.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
unique_labels = df['label'].unique().tolist()
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Definir tooltips para la imagen y la etiqueta
|
| 45 |
TOOLTIPS = """
|
| 46 |
<div>
|
| 47 |
<div>
|
|
|
|
| 53 |
</div>
|
| 54 |
"""
|
| 55 |
|
| 56 |
+
# Crear contenedor para el gr谩fico
|
| 57 |
+
plot_placeholder = st.empty()
|
| 58 |
+
|
| 59 |
+
# Funci贸n que genera y muestra el gr谩fico a partir de una lista de labels
|
| 60 |
+
def render_plot(selected_labels):
|
| 61 |
+
if not selected_labels:
|
| 62 |
+
st.write("No data to display. Please select at least one subset.")
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
filtered_data = df[df['label'].isin(selected_labels)]
|
| 66 |
+
p = figure(width=400, height=400, tooltips=TOOLTIPS, title="")
|
| 67 |
+
|
| 68 |
+
num_labels = len(selected_labels)
|
| 69 |
+
# Ajuste de la paleta
|
| 70 |
+
if num_labels < 3:
|
| 71 |
+
palette = Category10[3][:num_labels]
|
| 72 |
+
elif num_labels in [3, 4, 5, 6, 7, 8, 9, 10]:
|
| 73 |
+
palette = Category10[num_labels]
|
| 74 |
+
else:
|
| 75 |
+
palette = Category10[10][:num_labels]
|
| 76 |
|
| 77 |
+
# Graficar cada label por separado para poder asignar su color y legend_label
|
| 78 |
+
for label, color in zip(selected_labels, palette):
|
| 79 |
+
subset = filtered_data[filtered_data['label'] == label]
|
| 80 |
+
source = ColumnDataSource(data=dict(
|
| 81 |
+
x=subset['x'],
|
| 82 |
+
y=subset['y'],
|
| 83 |
+
label=subset['label'],
|
| 84 |
+
img=subset['img']
|
| 85 |
+
))
|
| 86 |
+
p.scatter('x', 'y', size=8, source=source, color=color, legend_label=label)
|
| 87 |
+
|
| 88 |
+
p.legend.title = "Subsets"
|
| 89 |
+
p.legend.location = "top_right"
|
| 90 |
+
p.legend.click_policy = "hide"
|
| 91 |
+
|
| 92 |
+
plot_placeholder.bokeh_chart(p)
|
| 93 |
+
|
| 94 |
+
# Mostrar inicialmente el gr谩fico con todas las etiquetas
|
| 95 |
+
render_plot(unique_labels)
|
| 96 |
+
|
| 97 |
+
# --- Desplegable (multiselect) colocado debajo del gr谩fico ---
|
| 98 |
+
selected_labels = st.multiselect(
|
| 99 |
+
"Select Subsets to Visualize:",
|
| 100 |
+
options=unique_labels,
|
| 101 |
+
default=unique_labels
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
# Al cambiar la selecci贸n, volver a renderizar el gr谩fico
|
| 105 |
+
render_plot(selected_labels)
|