Skip to content

Commit

Permalink
Revert API changes made in Selected rows method #1121 (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Mar 4, 2024
1 parent d612e1b commit 67290e9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/dataframe/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions shiny/api-examples/data_frame/app-core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 4 additions & 4 deletions shiny/api-examples/data_frame/app-express.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)]
31 changes: 12 additions & 19 deletions shiny/render/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
`<data_frame_renderer>.input_selected_rows()` method, where `<data_frame_renderer>` 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.<id>_selected_rows()` value. The value returned will be `None` if no rows
`input.<id>_selected_rows()` function, where `<id>` 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.<id>_selected_rows())]`.
Expand All @@ -267,6 +258,8 @@ class data_frame(Renderer[DataFrameResult]):
objects you can return from the rendering function to specify options.
"""

# `<data_frame_renderer>.input_selected_rows()` method, where `<data_frame_renderer>` 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.<id>_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)

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/playwright/deploys/plotly/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 5 additions & 1 deletion tests/playwright/shiny/bugs/0676-row-selection/app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)]

Expand Down

0 comments on commit 67290e9

Please sign in to comment.