From bfd23aa08e313a49ebd7e8f821d4beef2ac89562 Mon Sep 17 00:00:00 2001 From: Dominik Haentsch Date: Tue, 20 Feb 2024 13:43:14 +0100 Subject: [PATCH] feat: better handle spotlight.show() without a dataset --- renumics/spotlight/app.py | 10 +++++----- renumics/spotlight/viewer.py | 2 +- renumics/spotlight_plugins/core/api/table.py | 20 ++++++++++++-------- src/stores/dataset/dataset.ts | 2 ++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/renumics/spotlight/app.py b/renumics/spotlight/app.py index a96bafbb..fa59c40d 100644 --- a/renumics/spotlight/app.py +++ b/renumics/spotlight/app.py @@ -350,11 +350,11 @@ def update(self, config: AppConfig) -> None: self.filebrowsing_allowed = config.filebrowsing_allowed if config.dtypes is not None or config.dataset is not None: - assert self._data_source is not None - self._data_store = DataStore(self._data_source, self._user_dtypes) - self._broadcast(RefreshMessage()) - self._update_issues() - self._update_embeddings() + if self._data_source is not None: + self._data_store = DataStore(self._data_source, self._user_dtypes) + self._broadcast(RefreshMessage()) + self._update_issues() + self._update_embeddings() if config.layout is not None: if self._data_store is not None: dataset_uid = self._data_store.uid diff --git a/renumics/spotlight/viewer.py b/renumics/spotlight/viewer.py index af8b339a..34ecb943 100644 --- a/renumics/spotlight/viewer.py +++ b/renumics/spotlight/viewer.py @@ -144,7 +144,7 @@ def __init__( def show( self, - dataset: Union[PathType, pd.DataFrame, None], + dataset: Union[PathType, pd.DataFrame, None] = None, folder: Optional[PathType] = None, layout: Optional[_LayoutLike] = None, no_browser: bool = False, diff --git a/renumics/spotlight_plugins/core/api/table.py b/renumics/spotlight_plugins/core/api/table.py index 352e2319..ae0aa3bc 100644 --- a/renumics/spotlight_plugins/core/api/table.py +++ b/renumics/spotlight_plugins/core/api/table.py @@ -15,6 +15,7 @@ ComputedColumnNotReady, FilebrowsingNotAllowed, InvalidPath, + Problem, ) from renumics.spotlight.io.path import is_path_relative_to from renumics.spotlight.reporting import emit_timed_event @@ -50,6 +51,16 @@ class Table(BaseModel): router = APIRouter() +class NoDataSource(Problem): + def __init__(self, filebrowsing_allowed: bool): + if filebrowsing_allowed: + text = "No dataset provided. Please select a dataset with `.show(dataset)` or with the filebrowser." + else: + text = "No dataset provided. Please select a dataset with `.show(dataset)`." + + super().__init__("No Dataset", text, 404) + + @router.get( "/", response_model=Table, @@ -65,14 +76,7 @@ def get_table(request: Request) -> ORJSONResponse: app: SpotlightApp = request.app data_store = app.data_store if data_store is None: - return ORJSONResponse( - Table( - uid="", - filename="", - columns=[], - generation_id=-1, - ).model_dump() - ) + raise NoDataSource(app.filebrowsing_allowed) columns = [] for column_name in data_store.column_names: diff --git a/src/stores/dataset/dataset.ts b/src/stores/dataset/dataset.ts index f91933c7..ee536e23 100644 --- a/src/stores/dataset/dataset.ts +++ b/src/stores/dataset/dataset.ts @@ -239,6 +239,7 @@ export const useDataset = create( filename, length: dataframe.length, loading: false, + loadingError: undefined, columns: dataframe.columns, columnsByKey: _.keyBy(dataframe.columns, 'key'), columnData: dataframe.data, @@ -321,6 +322,7 @@ export const useDataset = create( filename, length: dataframe.length, loading: false, + loadingError: undefined, columns: dataframe.columns, columnsByKey: _.keyBy(dataframe.columns, 'key'), columnData: dataframe.data,