diff --git a/examples/dataframe/app.py b/examples/dataframe/app.py index e1d527534..99374627e 100644 --- a/examples/dataframe/app.py +++ b/examples/dataframe/app.py @@ -92,10 +92,10 @@ def handle_edit(): @render.text def detail(): - selected_rows = grid.input_selected_rows() or () + selected_rows = input.grid_selected_rows() or () if len(selected_rows) > 0: # "split", "records", "index", "columns", "values", "table" - return df().iloc[list(grid.input_selected_rows())] + return df().iloc[list(input.grid_selected_rows())] app = App(app_ui, server) diff --git a/shiny/api-examples/data_frame/app-core.py b/shiny/api-examples/data_frame/app-core.py index 83843ed57..a592ef238 100644 --- a/shiny/api-examples/data_frame/app-core.py +++ b/shiny/api-examples/data_frame/app-core.py @@ -44,11 +44,11 @@ def summary_data(): @reactive.calc def filtered_df(): - req(summary_data.input_selected_rows()) + req(input.summary_data_selected_rows()) - # summary_data.selected_rows() is a tuple, so we must convert it to list, + # input.summary_data_selected_rows() is a tuple, so we must convert it to list, # as that's what Pandas requires for indexing. - selected_idx = list(summary_data.input_selected_rows()) + selected_idx = list(input.summary_data_selected_rows()) countries = summary_df.iloc[selected_idx]["country"] # Filter data for selected countries return df[df["country"].isin(countries)] diff --git a/shiny/api-examples/data_frame/app-express.py b/shiny/api-examples/data_frame/app-express.py index 18064e7ee..24a5b47ba 100644 --- a/shiny/api-examples/data_frame/app-express.py +++ b/shiny/api-examples/data_frame/app-express.py @@ -3,7 +3,7 @@ from shinywidgets import render_widget from shiny import reactive, req -from shiny.express import render, ui +from shiny.express import input, render, ui # Load the Gapminder dataset df = px.data.gapminder() @@ -66,12 +66,12 @@ def country_detail_percap(): @reactive.calc def filtered_df(): - req(summary_data.input_selected_rows()) + req(input.summary_data_selected_rows()) - # summary_data.input_selected_rows() is a tuple, so we must convert it to list, + # input.summary_data_selected_rows() is a tuple, so we must convert it to list, # as that's what Pandas requires for indexing. - selected_idx = list(summary_data.input_selected_rows()) + selected_idx = list(input.summary_data_selected_rows()) countries = summary_df.iloc[selected_idx]["country"] # Filter data for selected countries return df[df["country"].isin(countries)] diff --git a/shiny/render/_dataframe.py b/shiny/render/_dataframe.py index 0b70381a3..16c0509f3 100644 --- a/shiny/render/_dataframe.py +++ b/shiny/render/_dataframe.py @@ -2,22 +2,12 @@ import abc import json -from typing import ( - TYPE_CHECKING, - Any, - Literal, - Optional, - Protocol, - Union, - cast, - runtime_checkable, -) +from typing import TYPE_CHECKING, Any, Literal, Protocol, Union, cast, runtime_checkable from htmltools import Tag from .. import ui from .._docstring import add_example, no_example -from ..session._utils import require_active_session from ._dataframe_unsafe import serialize_numpy_dtypes from .renderer import Jsonifiable, Renderer @@ -248,7 +238,8 @@ class data_frame(Renderer[DataFrameResult]): Row selection ------------- When using the row selection feature, you can access the selected rows by using the - `.input_selected_rows()` method, where `` is the render function name that corresponds with the `id=` used in :func:`~shiny.ui.outout_data_frame`. Internally, this method retrieves the selected row value from session's `input._selected_rows()` value. The value returned will be `None` if no rows + `input._selected_rows()` function, where `` is the `id` of the + :func:`~shiny.ui.output_data_frame`. The value returned will be `None` if no rows are selected, or a tuple of integers representing the indices of the selected rows. To filter a pandas data frame down to the selected rows, use `df.iloc[list(input._selected_rows())]`. @@ -267,6 +258,8 @@ class data_frame(Renderer[DataFrameResult]): objects you can return from the rendering function to specify options. """ + # `.input_selected_rows()` method, where `` is the render function name that corresponds with the `id=` used in :func:`~shiny.ui.outout_data_frame`. Internally, this method retrieves the selected row value from session's `input._selected_rows()` value. The value returned will be `None` if no rows + def auto_output_ui(self) -> Tag: return ui.output_data_frame(id=self.output_id) @@ -280,14 +273,14 @@ async def transform(self, value: DataFrameResult) -> Jsonifiable: ) return value.to_payload() - def input_selected_rows(self) -> Optional[tuple[int]]: - """ - When `row_selection_mode` is set to "single" or "multiple" this will return - a tuple of integers representing the rows selected by a user. - """ + # def input_selected_rows(self) -> Optional[tuple[int]]: + # """ + # When `row_selection_mode` is set to "single" or "multiple" this will return + # a tuple of integers representing the rows selected by a user. + # """ - active_session = require_active_session(None) - return active_session.input[self.output_id + "_selected_rows"]() + # active_session = require_active_session(None) + # return active_session.input[self.output_id + "_selected_rows"]() @runtime_checkable diff --git a/tests/playwright/deploys/plotly/app.py b/tests/playwright/deploys/plotly/app.py index 05b4885fd..bf90aa3f7 100644 --- a/tests/playwright/deploys/plotly/app.py +++ b/tests/playwright/deploys/plotly/app.py @@ -61,11 +61,11 @@ def summary_data(): @reactive.calc def filtered_df(): - req(summary_data.input_selected_rows()) + req(input.summary_data_selected_rows()) # input.summary_data_selected_rows() is a tuple, so we must convert it to list, # as that's what Pandas requires for indexing. - selected_idx = list(summary_data.input_selected_rows()) + selected_idx = list(input.summary_data_selected_rows()) countries = summary_df.iloc[selected_idx]["country"] # Filter data for selected countries return df[df["country"].isin(countries)] diff --git a/tests/playwright/shiny/bugs/0676-row-selection/app.py b/tests/playwright/shiny/bugs/0676-row-selection/app.py index 91fbebc97..1c8fee0fd 100644 --- a/tests/playwright/shiny/bugs/0676-row-selection/app.py +++ b/tests/playwright/shiny/bugs/0676-row-selection/app.py @@ -1,3 +1,7 @@ +from __future__ import annotations + +from typing import cast + import pandas as pd from shiny import App, Inputs, Outputs, Session, render, ui @@ -33,7 +37,7 @@ def grid(): @render.table def detail(): - selected_rows = grid.input_selected_rows() or () + selected_rows = cast("tuple[int]", input.grid_selected_rows() or ()) if len(selected_rows) > 0: return df.iloc[list(selected_rows)]