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

Exception when using .data_view() with cleared table #1351

Closed
pstorozenko opened this issue May 6, 2024 · 0 comments · Fixed by #1377 · May be fixed by #1374
Closed

Exception when using .data_view() with cleared table #1351

pstorozenko opened this issue May 6, 2024 · 0 comments · Fixed by #1377 · May be fixed by #1374
Labels
bug Something isn't working data frame Related to @render.data_frame
Milestone

Comments

@pstorozenko
Copy link

pstorozenko commented May 6, 2024

Hi, discovered some exception thrown when using .data_view() with table that has been recently cleared.

Let me show an example as it clearly demonstrate the issue.

from shiny import App, ui, reactive, render
import pandas as pd

app_ui = ui.page_fluid(
    ui.input_action_button("add_row", "Add row"),
    ui.input_action_button("clear_table", "Clear table"),
    ui.output_text("number_of_selected_rows"),
    ui.output_data_frame("df1"),
)


def server(input):
    df = reactive.Value(pd.DataFrame(columns=["A", "B"]))

    @render.data_frame
    def df1():
        return render.DataGrid(df(), selection_mode="rows")

    @reactive.effect
    @reactive.event(input.add_row)
    def _():
        old_df = df()
        new_df = pd.concat([old_df, pd.DataFrame([[1, 2]], columns=["A", "B"])])
        df.set(new_df)

    @render.text
    def number_of_selected_rows():
        df_selected = df1.data_view(selected=True)
        return f"Selected rows: {len(df_selected)}"

    @reactive.effect
    @reactive.event(input.clear_table)
    def _():
        df.set(pd.DataFrame(columns=["A", "B"]))


app = App(app_ui, server)

When you add a row, click on it and click the clear button you get:

Exception/Error that I got:
Traceback (most recent call last):
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/indexing.py", line 1714, in _get_list_axis
    return self.obj._take_with_is_copy(key, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/generic.py", line 4153, in _take_with_is_copy
    result = self.take(indices=indices, axis=axis)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/generic.py", line 4133, in take
    new_data = self._mgr.take(
               ^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/internals/managers.py", line 891, in take
    indexer = maybe_convert_indices(indexer, n, verify=verify)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/indexers/utils.py", line 282, in maybe_convert_indices
    raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/session/_session.py", line 1220, in output_obs
    value = await renderer.render()
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/render/renderer/_renderer.py", line 255, in render
    value = await self.fn()
            ^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/render/renderer/_renderer.py", line 349, in __call__
    return await self._fn()
           ^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/_utils.py", line 263, in fn_async
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/Projects/InnovationLead/playground/app_df.py", line 28, in number_of_selected_rows
    df_selected = df1.data_view(selected=True)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/render/_data_frame.py", line 285, in data_view
    return self._data_view_selected()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/reactive/_reactives.py", line 283, in __call__
    return _utils.run_coro_sync(self.get_value())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/_utils.py", line 378, in run_coro_sync
    coro.send(None)
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/reactive/_reactives.py", line 293, in get_value
    raise self._error[0]
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/reactive/_reactives.py", line 328, in _run_func
    self._value.append(await self._fn())
                       ^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/_utils.py", line 263, in fn_async
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/render/_data_frame.py", line 524, in self__data_view_selected
    return _subset_data_view(selected=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/shiny/render/_data_frame.py", line 515, in _subset_data_view
    return data.iloc[data_view_indices]
           ~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/indexing.py", line 1191, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/indexing.py", line 1743, in _getitem_axis
    return self._get_list_axis(key, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pasza/miniconda3/envs/shiny/lib/python3.12/site-packages/pandas/core/indexing.py", line 1717, in _get_list_axis
    raise IndexError("positional indexers are out-of-bounds") from err
IndexError: positional indexers are out-of-bounds

I think something's wrong inside shiny, am I right?
Initially, I thought df1.data_view(selected=False) is just None but the error is weird, but it's not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment