Skip to content

Commit

Permalink
copy paste
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen committed Nov 25, 2024
1 parent e4b61aa commit 1846c4e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/panel_copy_paste/_copy_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class CopyButton(pn.custom.JSComponent):
if (model.decimal_separator === null) {
model.decimal_separator = getDecimalSeparator();
console.log("set")
}
model.on("_data", (e)=>{
Expand Down
39 changes: 38 additions & 1 deletion src/panel_copy_paste/_paste_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def read_csv(self, data: str) -> "pd.DataFrame":

if not data:
return pd.DataFrame()
return pd.read_csv(StringIO(data), sep="\t", header=None)
decimal = self.decimal_separator or "."
return pd.read_csv(StringIO(data), sep="\t", decimal=decimal)


class PasteButtonBase(pn.custom.JSComponent):
Expand Down Expand Up @@ -130,6 +131,37 @@ class PasteToDataFrameButton(PasteButtonBase):
"""

_esm = """
function getDecimalSeparator(locale) {
const numberWithDecimalSeparator = 1.1;
return Intl.NumberFormat(locale)
.formatToParts(numberWithDecimalSeparator)
.find(part => part.type === 'decimal')
.value;
}
export function render({ model, el }) {
const button = model.get_child("button")
el.appendChild(button)
if (model._decimal_separator === null) {
model.decimal_separator = getDecimalSeparator();
}
button.addEventListener('click', (event) => {
navigator.clipboard.readText()
.then(pastedData => {
if (model.data==pastedData){
model.data=pastedData + " ";
} else {
model.data = pastedData;
}
})
});
}
"""

value = param.DataFrame(doc="""The value from the clip board as a Pandas DataFrame.""")
button = pn.custom.Child(constant=True, doc="""A custom Button or ButtonIcon to use.""")
target = param.Parameter(
Expand All @@ -138,4 +170,9 @@ class PasteToDataFrameButton(PasteButtonBase):
allow_refs=False,
)

decimal_separator = param.Selector(
default=None,
objects=[None, ".", ","],
doc="""The decimal symbol used when transforming a DataFrame. If not provided set to the decimal symbol of the client.""",
)
_transform_func = read_csv
4 changes: 2 additions & 2 deletions tests/test_paste_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def test_paste_csv_input():
assert not widget.value
assert not target.value
# When
widget.data = """1\t2\t3\t4"""
widget.data = "x\n1.1\n2.2\n"
# Then
expected = pd.DataFrame([{0: 1, 1: 2, 2: 3, 3: 4}])
expected = pd.DataFrame({"x": [1.1, 2.2]})
pd.testing.assert_frame_equal(widget.value, expected)
pd.testing.assert_frame_equal(widget.value, target.value)

0 comments on commit 1846c4e

Please sign in to comment.