plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.29k stars 2.05k forks source link

when cell_selectable=False table text cannot be copied to the clipboard in data table #1978

Open ned2 opened 2 years ago

ned2 commented 2 years ago

Describe the bug

When a DataTable is initialised with cell_selectable=False, text cannot be copied to the clipboard. This is independent of the issue in #1975 where cells in columns with presentation value of input do not appear to be visually selected. The text can actually be selected, however when attempting to copy the selection, nothing is added to the clipboard.

Expected behavior When cell_selectable is false, text selected inside DataTable cells should be able to be copied to the clipboard as is normally handled by the browser.

Minimal Example Text in any cell (regardless of presentation) cannot be copied to the clipboard.

import dash
import pandas as pd
from dash import html
from dash.dash_table import DataTable

url = "https://github.com/plotly/datasets/raw/master/26k-consumer-complaints.csv"
df = pd.read_csv(url, nrows=100)
columns = [{"name": col, "id": col, "selectable": True} for col in df.columns]
columns[2]["presentation"] = "markdown"

app = dash.Dash(__name__)
app.layout = html.Div(
    [
        DataTable(
            columns=columns,
            data=df.to_dict("records"),
            cell_selectable=False,
        )
    ]
)

if __name__ == "__main__":
    app.run_server(debug=True, port=8050)

Environment

dash                          2.3.0
dash-bootstrap-components     1.0.3
dash-core-components          2.0.0
dash-html-components          2.0.0
dash-table                    5.0.0
- OS: Ubuntu 21.10
- Browser Chrome
- Version 99.0.4844.51
ned2 commented 2 years ago

I did a bit of digging and it looks like the issue is that the onCopy event handler in dash-table/components/ControlledTable/index.tsx makes the assumption that the DataTable is in cell_selectable=True mode, as it always defers to TableClipboardHelper.toClipboard, which takes data from its selectedCells param, however this will always be empty when cell_selectable=False.

I'm wondering if what's needed is for the copy event handling logic to opt-out of using the DataTable's copying machinery and defer to the browser's default handling?

@alexcjohnson is this something we could address with a relatively small change? If so I'd be up for trying to put together a PR. Or is this opening a broader question that needs untangling first?

alexcjohnson commented 2 years ago

@ned2 thanks for the investigation! (and apologies for the slow reply, this came in during a vacation and then slipped off my radar πŸ™ˆ ) I think you're right that these events should just revert to the browser when cell_selectable=False. And if this is easy to change, by all means go for it!

benedikt-budig commented 2 years ago

@ned2 thanks for your hints, I have created a pull request that solves this issue: https://github.com/plotly/dash/pull/2114

@alexcjohnson I'd be very happy to get some feedback on it from you, particularly since this is my first PR for dash πŸ™‚

AnnMarieW commented 2 years ago

Will copy/paste of partial cell content be fixed with this PR too, or will it still be a separate issue?

benedikt-budig commented 2 years ago

@AnnMarieW the fix from the PR will only affect copy/paste behavior in situations where no cell has been selected, so I assume it will not (completely) fix the issue you mentioned.

However, feel free to try it out yourself: the fix has already been included in the latest dash release πŸ‘