Skip to content

Commit 3b42f7a

Browse files
Merge pull request #4 from awesome-panel/enhancement/decimal-seperator
Support decimal seperator
2 parents c384bc7 + 1846c4e commit 3b42f7a

File tree

6 files changed

+4956
-4450
lines changed

6 files changed

+4956
-4450
lines changed

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ markdown_extensions:
4848
kwds:
4949
type: panel
5050
requirements: |
51-
panel-copy-paste>=0.0.2
51+
panel-copy-paste>=0.0.3
5252
link_text: |
5353
► Run
5454
- admonition

pixi.lock

Lines changed: 4914 additions & 4445 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pixi.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pytest-cov = "*"
2424
mypy = "*"
2525
pandas = ">=2.2.3,<3"
2626
polars = ">=1.14.0,<2"
27+
pyarrow = "*"
2728
[feature.test.tasks]
2829
test = "pytest"
2930
test-coverage = "pytest --cov=panel_copy_paste --cov-report=xml --cov-report=term-missing"

src/panel_copy_paste/_copy_button.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class CopyButton(pn.custom.JSComponent):
5252
5353
if (model.decimal_separator === null) {
5454
model.decimal_separator = getDecimalSeparator();
55-
console.log("set")
5655
}
5756
5857
model.on("_data", (e)=>{

src/panel_copy_paste/_paste_button.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def read_csv(self, data: str) -> "pd.DataFrame":
2222

2323
if not data:
2424
return pd.DataFrame()
25-
return pd.read_csv(StringIO(data), sep="\t", header=None)
25+
decimal = self.decimal_separator or "."
26+
return pd.read_csv(StringIO(data), sep="\t", decimal=decimal)
2627

2728

2829
class PasteButtonBase(pn.custom.JSComponent):
@@ -130,6 +131,37 @@ class PasteToDataFrameButton(PasteButtonBase):
130131
131132
"""
132133

134+
_esm = """
135+
function getDecimalSeparator(locale) {
136+
const numberWithDecimalSeparator = 1.1;
137+
return Intl.NumberFormat(locale)
138+
.formatToParts(numberWithDecimalSeparator)
139+
.find(part => part.type === 'decimal')
140+
.value;
141+
}
142+
143+
export function render({ model, el }) {
144+
const button = model.get_child("button")
145+
el.appendChild(button)
146+
147+
if (model._decimal_separator === null) {
148+
model.decimal_separator = getDecimalSeparator();
149+
}
150+
151+
button.addEventListener('click', (event) => {
152+
navigator.clipboard.readText()
153+
.then(pastedData => {
154+
if (model.data==pastedData){
155+
model.data=pastedData + " ";
156+
} else {
157+
model.data = pastedData;
158+
}
159+
})
160+
161+
});
162+
}
163+
"""
164+
133165
value = param.DataFrame(doc="""The value from the clip board as a Pandas DataFrame.""")
134166
button = pn.custom.Child(constant=True, doc="""A custom Button or ButtonIcon to use.""")
135167
target = param.Parameter(
@@ -138,4 +170,9 @@ class PasteToDataFrameButton(PasteButtonBase):
138170
allow_refs=False,
139171
)
140172

173+
decimal_separator = param.Selector(
174+
default=None,
175+
objects=[None, ".", ","],
176+
doc="""The decimal symbol used when transforming a DataFrame. If not provided set to the decimal symbol of the client.""",
177+
)
141178
_transform_func = read_csv

tests/test_paste_button.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def test_paste_csv_input():
2626
assert not widget.value
2727
assert not target.value
2828
# When
29-
widget.data = """1\t2\t3\t4"""
29+
widget.data = "x\n1.1\n2.2\n"
3030
# Then
31-
expected = pd.DataFrame([{0: 1, 1: 2, 2: 3, 3: 4}])
31+
expected = pd.DataFrame({"x": [1.1, 2.2]})
3232
pd.testing.assert_frame_equal(widget.value, expected)
3333
pd.testing.assert_frame_equal(widget.value, target.value)

0 commit comments

Comments
 (0)