Skip to content

Commit

Permalink
fix saving from GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Nov 3, 2023
1 parent ae5de8f commit 3a20f19
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tabulous/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.5.5"
__version__ = "0.5.6.dev0"

from tabulous.widgets import TableViewer, TableViewerWidget
from tabulous.core import (
Expand Down
19 changes: 11 additions & 8 deletions tabulous/_io.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import annotations

from typing import Union, TYPE_CHECKING
from typing import Union
from pathlib import Path

if TYPE_CHECKING:
import pandas as pd
import pandas as pd

PathLike = Union[str, Path, bytes]

Expand Down Expand Up @@ -38,14 +36,19 @@ def save_file(path: PathLike, df: pd.DataFrame) -> None:
"""Save current table."""
path = Path(path)
suf = path.suffix
# if index is not edited, do not save it
index = type(df.index) is not pd.RangeIndex

if suf in (".csv", ".txt", ".dat"):
df.to_csv(path)
df.to_csv(path, index=index)
elif suf in (".tsv",):
df.to_csv(path, sep="\t", index=index)
elif suf in (".xlsx", ".xls", "xml"):
df.to_excel(path)
df.to_excel(path, index=index)
elif suf in (".html",):
df.to_html(path)
df.to_html(path, index=index)
elif suf in (".parquet", ".pq"):
df.to_parquet(path)
df.to_parquet(path, index=index)
else:
raise ValueError(f"Extension {suf} not supported.")

Expand Down
11 changes: 10 additions & 1 deletion tabulous/commands/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ def open_spreadsheet(viewer: TableViewerBase):
def save_table(viewer: TableViewerBase):
"""Save current table data"""
if table := viewer.current_table:
path = viewer.history_manager.openFileDialog(mode="w", caption="Save table")
path = viewer.history_manager.openFileDialog(
mode="w",
caption="Save table",
filter=(
"Text (*.csv; *.tsv; *.txt);;"
"Excel (*.xlsx; *.xls);;"
"HTML (*.html; *.htm);;"
"All files (*.*)"
),
)
if path:
table.save(path)
return None
Expand Down

0 comments on commit 3a20f19

Please sign in to comment.