Skip to content

Instantly share code, notes, and snippets.

@gshotwell
Created February 3, 2025 00:47
Show Gist options
  • Save gshotwell/e1ccb2c481843d7ae711a872cdb43b95 to your computer and use it in GitHub Desktop.
Save gshotwell/e1ccb2c481843d7ae711a872cdb43b95 to your computer and use it in GitHub Desktop.
import ipydatagrid
import pandas as pd
from shinywidgets import output_widget, reactive_read, render_widget
from shiny import App, render, ui
# Create sample data
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': ['a', 'b', 'c', 'd'],
'C': [True, False, True, False]
})
app_ui = ui.page_fluid(
ui.h2("IPyDataGrid Example"),
output_widget("grid"),
ui.output_text("selection")
)
def server(input, output, session):
@render_widget
def grid():
return ipydatagrid.DataGrid(
df,
selection_mode="cell", # Enable cell selection
layout={"height": "200px"} # Set a fixed height
)
@render.text
def selection():
# Read the selection trait reactively
sel = reactive_read(grid.widget, "selections")
if not sel:
return "No cell selected"
return f"Selected cell: {sel}"
app = App(app_ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment