| 174 | # --- SELECTBOX COMPONENT --- |
| 175 | @workflow.atom(dependencies=["load_data"]) |
| 176 | def selectbox_demo(load_data): |
| 177 | df = load_data |
| 178 | text("## 5. Adding Interactivity with `selectbox()`") |
| 179 | text( |
| 180 | "Use the dropdown menu below to select a column from the dataset and visualize its distribution." |
| 181 | ) |
| 182 | |
| 183 | # Create a selectbox for choosing a column to visualize |
| 184 | column_choice = selectbox( |
| 185 | label="Choose a column to visualize", |
| 186 | options=df.select_dtypes(include=["number"]).columns.tolist(), |
| 187 | ) |
| 188 | |
| 189 | # Create and display a histogram based on the selected column |
| 190 | fig = px.histogram(df, x=column_choice, title=f"Distribution of {column_choice}") |
| 191 | |
| 192 | # Display the plot |
| 193 | plotly(fig) |
| 194 | |
| 195 | text( |
| 196 | """ |
| 197 | The `selectbox()` component allows users to select from a list of options. |
| 198 | **Example:** |
| 199 | ```python |
| 200 | from preswald import selectbox |
| 201 | choice = selectbox( |
| 202 | label="Choose Dataset", |
| 203 | options=["Dataset A", "Dataset B", "Dataset C"] |
| 204 | ) |
| 205 | print(f"User selected: {choice}") |
| 206 | ``` |
| 207 | In this example, selecting a column updates the histogram dynamically! """ |
| 208 | ) |
| 209 | |
| 210 | |
| 211 | # --- SEPARATOR COMPONENT --- |