dataframe icon indicating copy to clipboard operation
dataframe copied to clipboard

can't copy shortened dataframe cell - ellipsis problem

Open revintec opened this issue 2 years ago • 1 comments

using Jupyter Lab 4.0.2 and latest versions(should be) of kotlin jupyter and kotlin/dataframe (don't know how to get kotlin jupyter and kotlin/dataframe version, but I just installed them last week

suppose you have a long text in cell, the current implementation shortens it by truncating the original text this made it very hard to copy the original text using CMD-C/Ctrl-C after text selection image the copied text is(incorrectly truncated) homed_search_word_recommend_12042_v22...

  1. is there a better way to grab the full text?
  2. I think the cell should be rendered by the browser, instead of truncated by the backend code, here a demo image the copied text in the above example is(correctly the full text) DeepAnswerModel_10794_v1_r144669_sail_deep_answer_0

ref: https://stackoverflow.com/questions/9789723/css-text-overflow-in-a-table-cell and https://codepen.io/markchitty/pen/RNZbRE

it would be better if the cell is middle truncated instead of end truncated, but the majority of browsers don't support middle truncation, implementing it would require more code

revintec avatar Jul 17 '23 04:07 revintec

Hi. Truncation on the frontend seems reasonable. Thank you for the example. I don't know why it's done on the backend now, so i'll have to investigate a little. In the meantime, I think full text is available in the HTML code as a tooltip image So, i can suggest to try using a custom script that will put cell content in a clipboard upon double click, for example. I tried to save output with this script as an HTML page and it works, but in Datalore cell output gets wrapped in an iframe that blocks interactions with a clipboard. Not sure if it's going to work in Jupyter Lab.

val copyCellContentScript = DataFrameHtmlData(
        script = """
            (function() {
                let formattedSpans = document.querySelectorAll('span.formatted');

                formattedSpans.forEach(function(span) {
                    span.addEventListener('dblclick', function(e) {
                        let textToCopy = e.target.title;
                        // Use the Clipboard API where available
                        if (navigator.clipboard) {
                            navigator.clipboard.writeText(textToCopy)
                            .then(function() {
                                console.log('Copying to clipboard was successful!');
                            }, function(err) {
                                console.error('Could not copy text: ', err);
                            });
                        } else {
                            // Fallback for browsers that don't support clipboard API
                            let textArea = document.createElement('textarea');
                            textArea.value = textToCopy;
                            document.body.appendChild(textArea);
                            textArea.focus();
                            textArea.select();
                            try {
                                document.execCommand('copy');
                                console.log('Copying to clipboard was successful!');
                            } catch (err) {
                                console.error('Could not copy text: ', err);
                            }
                            // Cleaning up the textarea
                            document.body.removeChild(textArea);
                        }
                    });
                });
            })();
        """.trimIndent()
    )

And then df.toHTML().plus(copyCellContentScript)

koperagen avatar Jul 17 '23 10:07 koperagen