|
| 1 | +from chartlets import Component, Input, State, Output |
| 2 | +from chartlets.components import Box, Typography, Table |
| 3 | + |
| 4 | +from server.context import Context |
| 5 | +from server.panel import Panel |
| 6 | + |
| 7 | +from chartlets.components.table import TableColumn, TableRowData |
| 8 | + |
| 9 | +panel = Panel(__name__, title="Panel F") |
| 10 | + |
| 11 | + |
| 12 | +# noinspection PyUnusedLocal |
| 13 | +@panel.layout() |
| 14 | +def render_panel( |
| 15 | + ctx: Context, |
| 16 | +) -> Component: |
| 17 | + columns: list[TableColumn] = [ |
| 18 | + {"id": "id", "label": "ID"}, |
| 19 | + {"id": "firstName", "label": "First Name", "align": "left"}, |
| 20 | + {"id": "lastName", "label": "Last Name", "align": "center"}, |
| 21 | + {"id": "age", "label": "Age"}, |
| 22 | + ] |
| 23 | + |
| 24 | + rows: list[TableRowData] = [ |
| 25 | + { |
| 26 | + "id": 1, |
| 27 | + "data": {"id": "1", "firstName": "John", "lastName": "Doe", "age": 30}, |
| 28 | + }, |
| 29 | + { |
| 30 | + "id": 2, |
| 31 | + "data": {"id": "2", "firstName": "Jane", "lastName": "Smith", "age": 25}, |
| 32 | + }, |
| 33 | + { |
| 34 | + "id": 3, |
| 35 | + "data": {"id": "3", "firstName": "Peter", "lastName": "Jones", "age": 40}, |
| 36 | + }, |
| 37 | + ] |
| 38 | + |
| 39 | + table = Table(id="table", rows=rows, columns=columns, hover=True) |
| 40 | + |
| 41 | + title_text = Typography(id="title_text", children=["Basic Table"]) |
| 42 | + info_text = Typography(id="info_text", children=["Click on any row."]) |
| 43 | + |
| 44 | + return Box( |
| 45 | + style={ |
| 46 | + "display": "flex", |
| 47 | + "flexDirection": "column", |
| 48 | + "width": "100%", |
| 49 | + "height": "100%", |
| 50 | + "gap": "6px", |
| 51 | + }, |
| 52 | + children=[title_text, table, info_text], |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +# noinspection PyUnusedLocal |
| 57 | +@panel.callback(Input("table"), Output("info_text", "children")) |
| 58 | +def update_info_text( |
| 59 | + ctx: Context, |
| 60 | + table_row: int, |
| 61 | +) -> list[str]: |
| 62 | + return [f"The clicked row value is {table_row}."] |
0 commit comments