Skip to content

Commit

Permalink
Merge pull request #417 from Renumics/feature/read-df-after-close
Browse files Browse the repository at this point in the history
Feature/read df after close
  • Loading branch information
neindochoh authored Feb 12, 2024
2 parents f80f304 + b9731bd commit 597c10e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions renumics/spotlight/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class Viewer:
_host: str
_requested_port: Union[int, Literal["auto"]]
_server: Optional[Server]
_df: Optional[pd.DataFrame]

def __init__(
self,
Expand All @@ -139,6 +140,7 @@ def __init__(
self._host = host
self._requested_port = port
self._server = None
self._df = None

def show(
self,
Expand Down Expand Up @@ -177,6 +179,7 @@ def show(
embedders (disabled by default).
"""

self._df = None
if is_pathtype(dataset):
dataset = Path(dataset).absolute()
if dataset.is_dir():
Expand Down Expand Up @@ -259,6 +262,7 @@ def close(self, wait: Union[bool, Literal["forever"]] = False) -> None:
else:
self._server.wait_for_frontend_disconnect()

self._df = self._server.get_df()
_VIEWERS.remove(self)
self._server.stop()
self._server = None
Expand Down Expand Up @@ -292,8 +296,7 @@ def df(self) -> Optional[pd.DataFrame]:
"""
if self._server:
return self._server.get_df()

return None
return self._df

@property
def host(self) -> str:
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/pandas/test_pandas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pandas as pd

from renumics import spotlight


def test_read_df() -> None:
df = pd.DataFrame({"foo": range(4)})
viewer = spotlight.show(df, wait=False, no_browser=True)
df_after_close = viewer.df
viewer.close()
assert df_after_close is not None
assert "foo" in df_after_close


def test_read_df_after_close() -> None:
df = pd.DataFrame({"foo": range(4)})
viewer = spotlight.show(df, wait=False, no_browser=True)
viewer.close()
df_after_close = viewer.df
assert df_after_close is not None
assert "foo" in df_after_close

0 comments on commit 597c10e

Please sign in to comment.