Skip to content

Commit

Permalink
Merge pull request #4 from awesome-panel/enhancement/decimal-seperator
Browse files Browse the repository at this point in the history
Support decimal seperator
  • Loading branch information
MarcSkovMadsen authored Nov 25, 2024
2 parents c384bc7 + 1846c4e commit 3b42f7a
Show file tree
Hide file tree
Showing 6 changed files with 4,956 additions and 4,450 deletions.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ markdown_extensions:
kwds:
type: panel
requirements: |
panel-copy-paste>=0.0.2
panel-copy-paste>=0.0.3
link_text: |
► Run
- admonition
Expand Down
9,359 changes: 4,914 additions & 4,445 deletions pixi.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pytest-cov = "*"
mypy = "*"
pandas = ">=2.2.3,<3"
polars = ">=1.14.0,<2"
pyarrow = "*"
[feature.test.tasks]
test = "pytest"
test-coverage = "pytest --cov=panel_copy_paste --cov-report=xml --cov-report=term-missing"
Expand Down
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 3b42f7a

Please sign in to comment.