33from shiny import App , Inputs , Outputs , Session , reactive , render , req , ui
44
55penguins = palmerpenguins .load_penguins ()
6+ # Slim down the data frame to a few representative columns
7+ penguins = penguins .loc [
8+ penguins ["body_mass_g" ].notnull (),
9+ ["species" , "island" , "body_mass_g" , "year" ],
10+ ]
611
712app_ui = ui .page_fluid (
813 ui .input_select (
1419 ui .input_switch ("gridstyle" , "Grid" , True ),
1520 ui .input_switch ("fullwidth" , "Take full width" , True ),
1621 ui .input_switch ("fixedheight" , "Fixed height" , True ),
22+ ui .input_switch ("filters" , "Filters" , True ),
1723 ui .output_data_frame ("grid" ),
1824 ui .panel_fixed (
1925 ui .output_text_verbatim ("detail" ),
2531
2632
2733def server (input : Inputs , output : Outputs , session : Session ):
28- df : reactive .Value [pd .DataFrame ] = reactive .Value (penguins )
29-
3034 @output
3135 @render .data_frame
3236 def grid ():
3337 height = 350 if input .fixedheight () else None
3438 width = "100%" if input .fullwidth () else "fit-content"
3539 if input .gridstyle ():
3640 return render .DataGrid (
37- df () ,
41+ penguins ,
3842 row_selection_mode = input .selection_mode (),
3943 height = height ,
4044 width = width ,
45+ filters = input .filters (),
4146 )
4247 else :
4348 return render .DataTable (
44- df () ,
49+ penguins ,
4550 row_selection_mode = input .selection_mode (),
4651 height = height ,
4752 width = width ,
53+ filters = input .filters (),
4854 )
4955
50- @reactive .Effect
51- @reactive .event (input .grid_cell_edit )
52- def handle_edit ():
53- edit = input .grid_cell_edit ()
54- df_copy = df ().copy ()
55- df_copy .iat [edit ["row" ], edit ["col" ]] = edit ["new_value" ]
56- df .set (df_copy )
57-
5856 @output
5957 @render .text
6058 def detail ():
@@ -63,7 +61,7 @@ def detail():
6361 and len (input .grid_selected_rows ()) > 0
6462 ):
6563 # "split", "records", "index", "columns", "values", "table"
66- return df () .iloc [list (input .grid_selected_rows ())]
64+ return penguins .iloc [list (input .grid_selected_rows ())]
6765
6866
6967app = App (app_ui , server )
0 commit comments