Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert API changes made in Selected rows method #1121 #1174

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading