Skip to content

[Bug]: Reactive event on grid cell is not working #1878

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

Open
andrejcampa81 opened this issue Feb 28, 2025 · 1 comment
Open

[Bug]: Reactive event on grid cell is not working #1878

andrejcampa81 opened this issue Feb 28, 2025 · 1 comment
Labels
bug Something isn't working needs-triage

Comments

@andrejcampa81
Copy link

Component

Reactive Programming

Severity

P1 - High (major feature broken)

Shiny Version

1.2.1

Python Version

3.11

Minimal Reproducible Example

import pandas as pd
import seaborn as sns
import shinyswatch

from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui


def app_ui(req):
    dark = True if "dark" in req.query_params else None

    return ui.page_fluid(
        ui.head_content(
            ui.tags.meta(name="viewport", content="width=device-width, initial-scale=1")
        ),
        light_dark_switcher(dark),
        ui.input_select("dataset", "Dataset", sns.get_dataset_names()),
        ui.input_select(
            "selection_mode",
            "Selection mode",
            {
                "none": "(None)",
                "row": "Single row",
                "rows": "Multiple rows",
            },
            selected="rows",
        ),
        ui.input_switch("editable", "Edit", False),
        ui.input_switch("filters", "Filters", True),
        ui.input_switch("gridstyle", "Grid", True),
        ui.input_switch("fullwidth", "Take full width", True),
        ui.output_data_frame("grid"),
        ui.panel_fixed(
            ui.output_code("detail"),
            right="10px",
            bottom="10px",
        ),
        theme=shinyswatch.theme.darkly if dark else None,
        class_="p-3",
    )


def light_dark_switcher(dark):
    return (
        ui.div(
            ui.a(
                {"class": "btn-primary" if not dark else "btn-outline-primary"},
                "Light",
                href="?" if dark else None,
                class_="btn",
            ),
            ui.a(
                {"class": "btn-primary" if dark else "btn-outline-primary"},
                "Dark",
                href="?dark=1" if not dark else None,
                class_="btn",
            ),
            class_="float-end btn-group",
        ),
    )


def server(input: Inputs, output: Outputs, session: Session):
    df: reactive.value[pd.DataFrame] = reactive.value()

    @reactive.effect
    def update_df():
        return df.set(sns.load_dataset(req(input.dataset())))

    @render.data_frame
    def grid():
        height = 350
        width = "100%" if input.fullwidth() else "fit-content"
        if input.gridstyle():
            return render.DataGrid(
                df(),
                width=width,
                height=height,
                filters=input.filters(),
                editable=input.editable(),
                selection_mode=input.selection_mode(),
            )
        else:
            return render.DataTable(
                df(),
                width=width,
                height=height,
                filters=input.filters(),
                editable=input.editable(),
                selection_mode=input.selection_mode(),
            )

    @reactive.effect
    @reactive.event(input.grid_cell_edit)
    def handle_edit():
        edit = input.grid_cell_edit()
        df_copy = df().copy()
        df_copy.iat[edit["row"], edit["col"]] = edit["new_value"]
        df.set(df_copy)

    @render.code
    def detail():
        selected_rows = grid.cell_selection()["rows"]
        if len(selected_rows) > 0:
            return df().iloc[list(selected_rows)]


app = App(app_ui, server)

Behavior

I have issue geatin reactive event from dataframe editing, so I can not apply changes to the dataframe. Therefore, I have used the example code, but same behaviour:

https://github.com/posit-dev/py-shiny/blob/main/examples/dataframe/app.py

This is never executed, after editing cells
@reactive.effect
@reactive.event(input.grid_cell_edit)
def handle_edit():

Error Messages (if any)

Environment

OS: windows 11
Browser: Brave Version 1.75.180 Chromium: 133.0.6943.126 (Official Build) (64-bit)
Dependencies:
anyio==4.8.0
anywidget==0.9.13
appdirs==1.4.4
asgiref==3.8.1
asttokens==3.0.0
Bottleneck==1.4.2
branca==0.8.1
cachetools==5.5.0
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
clarabel==0.9.0
click==8.1.7
cloudpickle==3.1.0
cmdstanpy==1.2.4
colorama==0.4.6
comm==0.2.2
contourpy==1.3.1
cvxpy==1.6.0
cycler==0.12.1
dask==2024.11.2
decorator==5.1.1
deprecation==2.1.0
ecos==2.0.14
et_xmlfile==2.0.0
executing==2.2.0
fonttools==4.55.0
fsspec==2024.10.0
greenlet==3.1.1
h11==0.14.0
h3==4.2.0
h5py==3.12.1
holidays==0.61
htmltools==0.6.0
idna==3.10
importlib_metadata==8.5.0
importlib_resources==6.4.5
ipyleaflet==0.19.2
ipython==8.31.0
ipywidgets==8.1.5
jedi==0.19.2
Jinja2==3.1.4
joblib==1.4.2
jupyter-leaflet==0.19.2
jupyter_core==5.7.2
jupyterlab_widgets==3.0.13
kiwisolver==1.4.7
linkify-it-py==2.0.3
linopy==0.4.2
llvmlite==0.44.0rc2
locket==1.0.0
lxml==5.3.0
markdown-it-py==3.0.0
MarkupSafe==3.0.2
matplotlib==3.9.2
matplotlib-inline==0.1.7
mdit-py-plugins==0.4.2
mdurl==0.1.2
mpld3==0.5.10
narwhals==1.24.0
numba==0.61.0rc2
numexpr==2.10.2
numpy==1.26.4
opcua==0.98.13
openpyxl==3.1.5
orjson==3.10.15
osqp==0.6.7.post3
packaging==24.2
pandas==2.2.3
parso==0.8.4
partd==1.4.2
pillow==11.0.0
platformdirs==4.3.6
ply==3.11
polars==1.16.0
prompt_toolkit==3.0.50
prophet==1.1.6
psycopg2==2.9.10
psygnal==0.11.1
pure_eval==0.2.3
pvlib==0.11.1
pycparser==2.22
Pygments==2.19.1
Pyomo==6.8.2
pyparsing==3.2.0
python-dateutil==2.9.0.post0
python-multipart==0.0.20
pytz==2024.2
pywin32==308
PyYAML==6.0.2
qdldl==0.1.7.post4
questionary==2.1.0
requests==2.32.3
scikit-learn==1.5.2
scipy==1.14.1
scs==3.2.7
seaborn==0.13.2
shiny==1.2.1
shinyswatch==0.8.0
shinywidgets==0.5.1
six==1.16.0
sniffio==1.3.1
SQLAlchemy==2.0.36
stack-data==0.6.3
stanio==0.5.1
starlette==0.45.3
threadpoolctl==3.5.0
timezonefinder==6.5.8
toolz==1.0.0
tqdm==4.67.1
traitlets==5.14.3
traittypes==0.2.1
typing_extensions==4.12.2
tzdata==2024.2
uc-micro-py==1.0.3
urllib3==2.2.3
uvicorn==0.34.0
watchfiles==1.0.4
wcwidth==0.2.13
websockets==14.2
widgetsnbextension==4.0.13
xarray==2024.11.0
xyzservices==2025.1.0
zipp==3.21.0
@andrejcampa81 andrejcampa81 added the bug Something isn't working label Feb 28, 2025
@GiorgioMiglia
Copy link

It seems that the example uses outdated code (Git Blame shows that the specific lines are two years old, predating v1.0).

The feature you're looking for is @set_patch_fn, which allows you to set a function that gets called when the DataFrame is edited.
You can find more details in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage
Projects
None yet
Development

No branches or pull requests

2 participants