|
| 1 | +import altair as alt |
| 2 | +import pandas as pd |
| 3 | +from chartlets import Component, Input, State, Output |
| 4 | +from chartlets.components import VegaChart, Box, Select, Typography |
| 5 | + |
| 6 | +from server.context import Context |
| 7 | +from server.panel import Panel |
| 8 | + |
| 9 | + |
| 10 | +panel = Panel(__name__, title="Panel G") |
| 11 | + |
| 12 | + |
| 13 | +@panel.layout(State("@app", "selectedDatasetId")) |
| 14 | +def render_panel( |
| 15 | + ctx: Context, |
| 16 | + selected_dataset_id: str = "", |
| 17 | +) -> Component: |
| 18 | + dataset = ctx.datasets.get(selected_dataset_id) |
| 19 | + variable_names, selected_var_names = get_variable_names(dataset) |
| 20 | + |
| 21 | + select = Select( |
| 22 | + id="selected_variable_name", |
| 23 | + value=[], |
| 24 | + label="Variable", |
| 25 | + options=[(v, v) for v in variable_names], |
| 26 | + style={"flexGrow": 0, "minWidth": 120}, |
| 27 | + multiple=True, |
| 28 | + tooltip="Select the variables of the test dataset", |
| 29 | + ) |
| 30 | + control_group = Box( |
| 31 | + style={ |
| 32 | + "display": "flex", |
| 33 | + "flexDirection": "row", |
| 34 | + "padding": 4, |
| 35 | + "justifyContent": "center", |
| 36 | + "gap": 4, |
| 37 | + }, |
| 38 | + children=[select], |
| 39 | + ) |
| 40 | + |
| 41 | + text = update_info_text(ctx, selected_dataset_id) |
| 42 | + info_text = Typography(id="info_text", children=text) |
| 43 | + |
| 44 | + return Box( |
| 45 | + style={ |
| 46 | + "display": "flex", |
| 47 | + "flexDirection": "column", |
| 48 | + "width": "100%", |
| 49 | + "height": "100%", |
| 50 | + }, |
| 51 | + children=[info_text, control_group], |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def get_variable_names( |
| 56 | + dataset: pd.DataFrame, |
| 57 | + prev_var_name: str | None = None, |
| 58 | +) -> tuple[list[str], list[str]]: |
| 59 | + """Get the variable names and the selected variable name |
| 60 | + for the given dataset and previously selected variable name. |
| 61 | + """ |
| 62 | + |
| 63 | + if dataset is not None: |
| 64 | + var_names = [v for v in dataset.keys() if v != "x"] |
| 65 | + else: |
| 66 | + var_names = [] |
| 67 | + |
| 68 | + if prev_var_name and prev_var_name in var_names: |
| 69 | + var_name = prev_var_name |
| 70 | + elif var_names: |
| 71 | + var_name = var_names[0] |
| 72 | + else: |
| 73 | + var_name = "" |
| 74 | + |
| 75 | + return var_names, var_name |
| 76 | + |
| 77 | + |
| 78 | +@panel.callback( |
| 79 | + Input("@app", "selectedDatasetId"), |
| 80 | + Input("selected_variable_name", "value"), |
| 81 | + Output("info_text", "children"), |
| 82 | +) |
| 83 | +def update_info_text( |
| 84 | + ctx: Context, |
| 85 | + dataset_id: str = "", |
| 86 | + selected_var_names: list[str] | None = None, |
| 87 | +) -> list[str]: |
| 88 | + |
| 89 | + if selected_var_names is not None: |
| 90 | + text = ", ".join(map(str, selected_var_names)) |
| 91 | + return [f"The dataset is {dataset_id} and the selected variables are: {text}"] |
| 92 | + else: |
| 93 | + return [f"The dataset is {dataset_id} and no variables are selected."] |
0 commit comments