Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decimal seperator #4

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading