Skip to content

Commit ef51158

Browse files
skaltmancpsievert
andauthored
Add .update_cell_selection() variation to DataGrid and DataTable components (#219)
* add update cell selection variation * fix function name * update example * add data table variation --------- Co-authored-by: Carson Sievert <[email protected]>
1 parent 165375b commit ef51158

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from palmerpenguins import load_penguins
2+
from shiny import App, reactive, render, ui
3+
4+
penguins = load_penguins()
5+
6+
app_ui = ui.page_fluid(
7+
ui.h2("Palmer Penguins"),
8+
ui.input_action_button("update_selection", "Update selection"),
9+
ui.input_action_button("reset_selection", "Reset selection"),
10+
ui.h5("Current selection: ", {"class": "pt-2"}),
11+
ui.output_code("penguins_selection"),
12+
ui.output_data_frame("penguins_df"),
13+
)
14+
15+
def server(input, output, session):
16+
17+
@render.code
18+
def penguins_selection():
19+
return penguins_df.cell_selection()["rows"] # <<
20+
21+
@render.data_frame
22+
def penguins_df(): # <<
23+
return render.DataGrid(penguins, selection_mode="rows") # <<
24+
25+
@reactive.effect
26+
@reactive.event(input.update_selection)
27+
async def _(): # <<
28+
await penguins_df.update_cell_selection({"type": "row", "rows": [1, 2, 8]}) # <<
29+
30+
@reactive.effect
31+
@reactive.event(input.reset_selection)
32+
async def _(): # <<
33+
await penguins_df.update_cell_selection({"type": "row", "rows": []}) # <<
34+
35+
app = App(app_ui, server)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from palmerpenguins import load_penguins
2+
from shiny import reactive
3+
from shiny.express import input, render, ui
4+
5+
penguins = load_penguins()
6+
7+
ui.h2("Palmer Penguins")
8+
9+
ui.input_action_button("update_selection", "Update row selection")
10+
ui.input_action_button("reset_selection", "Reset row selection")
11+
12+
ui.h5("Current selection: ", {"class": "pt-2"})
13+
14+
@render.code
15+
def _(): # <<
16+
return penguins_df.cell_selection()["rows"] # <<
17+
18+
@render.data_frame
19+
def penguins_df(): # <<
20+
return render.DataGrid(penguins, selection_mode="rows") # <<
21+
22+
@reactive.effect
23+
@reactive.event(input.update_selection)
24+
async def _(): # <<
25+
await penguins_df.update_cell_selection({"type": "row", "rows": [1, 2, 8]}) # <<
26+
27+
@reactive.effect
28+
@reactive.event(input.reset_selection)
29+
async def _(): # <<
30+
await penguins_df.update_cell_selection({"type": "row", "rows": []}) # <<

components/outputs/data-grid/index.qmd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ listing:
203203
file: app-variation-update-sort-core.py
204204
shinylive: https://shinylive.io/py/editor/#code=NobwRAdghgtgpmAXAAjFADugdOgnmAGlQGMB7CAFzkqVQDMAnUmZdKAG3gfWoHMBXAJYQAzskEx0pBhWTtSUACYB9HhAHCRAHQiNmyEQAthucZOmyAgpiIM4UYhUEA3OLeqK4DIkJ061GqLIALxyCioBQqIAFACUftCYykIhyEI4ULxwynTsQorROsjFaYJYhgBMhWAAChxcyDV8UdpgsQRFJenC6PwUyg5O5MoARn0U5NX86IpQVMoiFsK8WoTIqwCqM3NwBkvqq+2dxd0Qvf2DgsNjFBMQ1XYicP2LMsurRKsASnBPsq9OA5tDoQEqlcoAVmqAGF+Aw7JQ9m91CgPsgQKtiOwoCJWqiwOgKABaCqrAC+R1BXTKpD652UZE81UimgWFkOILB6VpFHpswoUByDFgcGZzVZijoHJ08QgCU8dAMXlcDGiPT6RB55yIT1xVwgsUQCTBAAEEZ4GFhGXBjsgFaxxaI2TI4kaqWDinYKHDQSynZKsAC4sUAMTIAA84eNJTNHi8WH5gsYItt9r9ImUktdtrBXp9yHN8YAInMoABxBiCArp2W22OXVxYOB0OhwRx1uwNuBN1yUNVnPpYab87IA5aysE43AQYh25vIZTZ90eqAAdygglk6czdCH23mQdDEajy49JWAAEYiAAGIgYsBkdirFAANk+YE8ImIz+QADEOE8ZJEAA7AAukQyBhpGOYlLER7QXKy71o4Ljds2rbtkhnYoY2cC9hQ-bnFgjzPM6gK8BOJRTjOc6KouhowcUa4blujoZgGw47GR0TAKBcGQceCQ6Bg6CpNY6DRCJySCDqypeLKaxgBQuA8LQSlwAAHhQYBkqBQA
205205
height: 350
206+
- title: Update Row Selection
207+
description: 'The selected rows can be programmatically updated using
208+
the `.update_cell_selection()` method. `.update_cell_selection()` takes one argument,
209+
`selection`, which should be a dictionary specifying `type`,
210+
which can be either `row` (for a single row) or `rows` (for multiple rows),
211+
and `row`, a list of the row indices to select. Note that you will also need
212+
to set `selection_mode` in `render.DataGrid()` to `"row"` or `"rows"`, since
213+
it is `"none"` by default. '
214+
apps:
215+
- title: Preview
216+
file: app-variation-update-selection-core.py
217+
height: 350
218+
- title: Express
219+
file: app-variation-update-selection-express.py
220+
shinylive: https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACVKAG3jtUoHMBXAJYQAzkwExUxOmSZtiUACYB9bhH5DhAHQj1GTYQAsh2MRKky6cKITICAbnG27mh4xjgAPVJeGjxk6TEIVD4yPCZLCAU4OnDBbW1VdREmAF5ZeWUkwREACgBKBIhBDAMAJlzNMAAFdk4mat4crTBCiG0SoRCyJWtbUiUAI1CyUkqwPlQFKAolYTg2OBsBUirwqoBVKZm4COIAd30Fpf721o6BDC7Q3uWB4bJRiHGfOB75xbuz9bAAJTh5hYDkdPqcqm0LqUAKzjADCfDokRkHxOKwgiCYayYICqhDYUF8VQxVVQZAAtGUqgBfCEQAACkWidAwJGi2miNCYSgKiG0TH5ETeCIgLCaGiUChoLIWbDmxy+BWAVQY+xaAF1Me16YyYhhpmQoEp6LBHFE4JzsuLJTy+QLLGRhYKzcyACIzKAAcToAgUuUtInCKK+ShgxGiqWVBxa+X5RQZVmWDncNBoJ018b69jg7gc5Fy1zIGEm+rgctBaLaBOwEEITA5XJ5moFTCg+ygAhk-uEEqlxZ2SkIMrLqLGOLAZGw3CJmLAKqxkdV0+AAEZwmVwgAONU0uOWTNJ82pmzaDOJ7NwXNkfPBUIYV7veWnSvCau1+vc-K8kUC1vtztikQeyLbZZkHNhZSDU5cjHCcpyQGc53wBCoyXbcYyYMAqTVIA
221+
height: 350
222+
- title: Core
223+
file: app-variation-update-selection-core.py
224+
shinylive: https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACVKAG3jtUoHMBXAJYQAzkwExUxOmSZtiUACYB9bhH5DhAHQj1GTYQAsh2MRKkyAgujxM6cKITICAbnBt2ICuHRuDt21XURJgBeWXllQMERAAoASn9odCVBUKZBLCgeOCUaNkEFGO0mEvSBDAMAJiKwAAV2TiZa3mitMDi8YtKMoVQ+MiUHJ1IlACN+slIavlQFKAolYTg2OEcBUk18Jk2AVVn5uH1l1eGITY6ukp6IPoGh9YgxianNuyWBpZW1ja3NgCU4O8jl9TudOhBSmUKgBWGoAYT4dA8Mk+JweiG2WxAm0IbCgwjaGM2qDIAFpKpsAL4XCHdcrEfq3JQkLw1KIaRbHb5ndrgyEZBlkJlzMhQXJ0WBwNktDkKGhg7QJM6eOA0I50Vx0GK9fo2QW3GxLAkPOKIRKQgACHi8dAwLLglyYXjV7JEnJBD3iZtpkJKdjIiIhruESjlduWbHdaKmcWAr2IAHc2gBdbbKy3W7wYEVi+iSx3Olgyt1yr2OyH+wO2Sg2jAAEXmUAA4nQBIVg4auaclDBiF4QvGk+cSubSlb7GtXBhVTQTmmMxOnFO4K5yNqbv0MDMRTlUdylZD8dgIIQnaqmEovfPfSUoAmoAIZMHQzQt-sFoQI1HuTFsWAyNg3CbESYAMAmmw2IOhJMMAACMNiVDYAAcybUo6jrjvcy40LOjgYXYWFwNOq5kOutwYG8cAfF2JqOkeJ5nmql6muWpR3g+T7FiGYbbgczJfnupy-psAFAUgmJgRBEmJtBwCoXEpSJGgqBpFYqAxMpKQCJ2GreHEYCUsmQA
225+
height: 350
206226
- title: Customize Summary Statement
207227
description: 'Set `summary` in `render.DataGrid()` to `False` to remove the statement
208228
"Viewing rows 1 through 10 of 20". Set it to a string template containing `{start}`,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from palmerpenguins import load_penguins
2+
from shiny import App, reactive, render, ui
3+
4+
penguins = load_penguins()
5+
6+
app_ui = ui.page_fluid(
7+
ui.h2("Palmer Penguins"),
8+
ui.input_action_button("update_selection", "Update selection"),
9+
ui.input_action_button("reset_selection", "Reset selection"),
10+
ui.h5("Current selection: ", {"class": "pt-2"}),
11+
ui.output_code("penguins_selection"),
12+
ui.output_data_frame("penguins_df"),
13+
)
14+
15+
def server(input, output, session):
16+
17+
@render.code
18+
def penguins_selection():
19+
return penguins_df.cell_selection()["rows"] # <<
20+
21+
@render.data_frame
22+
def penguins_df(): # <<
23+
return render.DataTable(penguins, selection_mode="rows") # <<
24+
25+
@reactive.effect
26+
@reactive.event(input.update_selection)
27+
async def _(): # <<
28+
await penguins_df.update_cell_selection({"type": "row", "rows": [1, 2, 8]}) # <<
29+
30+
@reactive.effect
31+
@reactive.event(input.reset_selection)
32+
async def _(): # <<
33+
await penguins_df.update_cell_selection({"type": "row", "rows": []}) # <<
34+
35+
app = App(app_ui, server)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from palmerpenguins import load_penguins
2+
from shiny import reactive
3+
from shiny.express import input, render, ui
4+
5+
penguins = load_penguins()
6+
7+
ui.h2("Palmer Penguins")
8+
9+
ui.input_action_button("update_selection", "Update row selection")
10+
ui.input_action_button("reset_selection", "Reset row selection")
11+
12+
ui.h5("Current selection: ", {"class": "pt-2"})
13+
14+
@render.code
15+
def _(): # <<
16+
return penguins_df.cell_selection()["rows"] # <<
17+
18+
@render.data_frame
19+
def penguins_df(): # <<
20+
return render.DataTable(penguins, selection_mode="rows") # <<
21+
22+
@reactive.effect
23+
@reactive.event(input.update_selection)
24+
async def _(): # <<
25+
await penguins_df.update_cell_selection({"type": "row", "rows": [1, 2, 8]}) # <<
26+
27+
@reactive.effect
28+
@reactive.event(input.reset_selection)
29+
async def _(): # <<
30+
await penguins_df.update_cell_selection({"type": "row", "rows": []}) # <<

components/outputs/data-table/index.qmd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ listing:
203203
file: app-variation-update-sort-core.py
204204
shinylive: https://shinylive.io/py/editor/#code=NobwRAdghgtgpmAXAAjFADugdOgnmAGlQGMB7CAFzkqVQDMAnUmZdKAG3gfWoHMBXAJYQAzskEx0pBhWTtSUACYB9HhAHCRAHQiNmyEQAthucZOmyAgpiIM4UYhUEA3OLeqK4DIkJ061GqLIALxyCioBQqIAFACUftCYykIhyEI4ULxwynTsQorROsjFaYJYhgBMhWAAChxcyDV8UdpgsQRFJenC6PwUyg5O5MoARn0U5NX86IpQVMoiFsK8WoTIqwCqM3NwBkvqq+2dxd0Qvf2DgsNjFBMQ1XYicP2LMsurRKsASnBPsq9OA5tDoQEqlcoAVmqAGF+Aw7JQ9m91CgPsgQKtiOwoCJWqiwOgKABaCqrAC+R1BXTKpD652UZE81UimgWFkOILB6VpFHpswoUByDFgcGZzVZijoHJ08QgCU8dAMXlcDGiPT6RB55yIT1xVwgsUQCTBAAEEZ4GFhGXBjsgFaxxaI2TI4kaqWDinYKHDQSynZKsAC4sUAMTIAA84eNJTNHi8WH5gsYItt9r9ImUktdtrBXp9yHN8YAInMoAAVKAjdii9Oy22xy6uLBwOh0OCOet2RtwZuuShqs59LDTfnZAHLWVgnG4CDEO0t5DKbPuj1QADuUEEsnTmbow+28yDoYjUZXHpKwAAjEQAAxEDFgMjsVYoABsnzAnhExBfyAAYhwTxkkQADsAC6RDIGGkY5iUsTHjBcorg2jguD2LZth2yFdqhTZwH2FADucWCPM8zqArwk4lNOs7zoqS6GrBxTrpu26OhmAYjjs5HRMAYHwVBJ4JDoGDoKk1joNEonJIIOrKl4sprGAFC4DwtDKXAAAeFBgGSYFAA
205205
height: 350
206+
- title: Update Row Selection
207+
description: 'The selected rows can be programmatically updated using
208+
the `.update_cell_selection()` method. `.update_cell_select()` takes one argument,
209+
`selection`, which should be a dictionary specifying `type`,
210+
which can be either `row` (for a single row) or `rows` (for multiple rows),
211+
and `row`, a list of the row indices to select. Note that you will also need
212+
to set `selection_mode` in `render.DataTable()` to `"row"` or `"rows"`, since
213+
it is `"none"` by default. '
214+
apps:
215+
- title: Preview
216+
file: app-variation-update-selection-core.py
217+
height: 350
218+
- title: Express
219+
file: app-variation-update-selection-express.py
220+
shinylive: https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACVKAG3jtUoHMBXAJYQAzkwExUxOmSZtiUACYB9bhH5DhAHQj1GTYQAsh2MRKky6cKITICAbnG27mh4xjgAPVJeGjxk6TEIVD4yPCZLCAU4OnDBbW1VdREmAF5ZeWUkwREACgBKBIhBDAMAJlzNMAAFdk4mat4crTBCiG0SoRCyJWtbUiUAI1CyUkqwPlQFKAolYTg2OBsBUirwqoBVKZm4COIAd30Fpf721o6BDC7Q3uWB4bJRiHGfOB75xbuz9bAAJTh5hYDkdPqcqm0LqUAKzjADCfDokRkHxOKwgiCYayYICqhDYUF8VQxVVQZAAtGUqgBfCEQAACkWidAwJGi2miNCYSgKGKYAGImAAeQXaJhiiJvBEQFhNDRKBQ0FkLNhzY5fArAKoMfYtAC6YoFwqKDMoTIw0zIUCU9FgjiicE52TlCp5BqFIul4ssZClEvtzIAIjMoAAVKCDRa5J0icIor5KGDEaKpLUHFr5N1G9r0yx9exwdw0GgnTE5qzLBzuBzkXLXMgYSYWuCq0FotoE7AQQhMDlc13892i8VQfZQAQyaPCeWKxs7JSEZUt1FjHFgMjYbhEzFgbVY1M6rfAACM4TK4QAHLqaZmPdoTXnKw7izY77mKwW4NWyLXgqEMK93jVU522ETtu17bl8l5Q0PXFJgRzHCdZREacG22WYFzYFU41OXJV3XTckG3Xd8GItNDyvDMb20MAqV1IA
221+
height: 350
222+
- title: Core
223+
file: app-variation-update-selection-core.py
224+
shinylive: https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACVKAG3jtUoHMBXAJYQAzkwExUxOmSZtiUACYB9bhH5DhAHQj1GTYQAsh2MRKkyAgujxM6cKITICAbnBt2ICuHRuDt21XURJgBeWXllQMERAAoASn9odCVBUKZBLCgeOCUaNkEFGO0mEvSBDAMAJiKwAAV2TiZa3mitMDi8YtKMoVQ+MiUHJ1IlACN+slIavlQFKAolYTg2OEcBUk18Jk2AVVn5uH1l1eGITY6ukp6IPoGh9YgxianNuyWBpZW1ja3NgCU4O8jl9TudOhBSmUKgBWGoAYT4dA8Mk+JweiG2WxAm0IbCgwjaGM2qDIAFpKpsAL4XCHdcrEfq3JQkLw1KIaRbHb5ndrgyEZBlkJlzMhQXJ0WBwNktDkKGhg7QJM6eOA0I50Vx0GK9fo2QW3GxLAkPOKIRKQgACHi8dAwLLglyYXjV7JEnJBD3iZtpkJKdjIiIhruESjlduWbHdaKmcWAr2IAHc2gBdJgAYiYAB5M+bSlbKDaMCKxfRJY7nSwZW65V6Shns47If7A7YC94MAAReZQAAqUFGKxiwcNXNOShgxC8IXjSfOdazOeVlrs91cGFVNBOJUd+dXcHXrnI2pu-QwMxFOVR3Li2x9THx2AghCdqqYSlr84bd8hUATUAEMjBqGNBnvsCyEBGUbcjE2JgGQ2DcJsRJgAwCabDYM6EkwwAAIw2JUNgABzJtS6YLo6O4rmsa4biclH2NR+5wIeZDHrcGBvHAHyjiajoPk+L5qu+pqfouvqlL+-6AVWIZhueBzMpBV6nDBmzwYhSCYqh6FaYmWHACRN6iYkaCoGkVioDEpkpAII4at4SpgJSyZAA
225+
height: 350
206226
- title: Customize Summary Statement
207227
description: 'Set `summary` in `render.DataGrid()` to `False` to remove the statement
208228
"Viewing rows 1 through 10 of 20". Set it to a string template containing `{start}`,

0 commit comments

Comments
 (0)