diff --git a/cli/openbb_cli/argparse_translator/obbject_registry.py b/cli/openbb_cli/argparse_translator/obbject_registry.py index 1c4cbf80dd9d..e1c73fd0c127 100644 --- a/cli/openbb_cli/argparse_translator/obbject_registry.py +++ b/cli/openbb_cli/argparse_translator/obbject_registry.py @@ -18,12 +18,14 @@ def _contains_obbject(uuid: str, obbjects: List[OBBject]) -> bool: """Check if obbject with uuid is in the registry.""" return any(obbject.id == uuid for obbject in obbjects) - def register(self, obbject: OBBject): + def register(self, obbject: OBBject) -> bool: """Designed to add an OBBject instance to the registry.""" if isinstance(obbject, OBBject) and not self._contains_obbject( obbject.id, self._obbjects ): self._obbjects.append(obbject) + return True + return False def get(self, idx: int) -> OBBject: """Return the obbject at index idx.""" diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index c154baa56cd0..8ac12f4ed569 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -153,54 +153,65 @@ def method(self, other_args: List[str], translator=translator): ns_parser = self._intersect_data_processing_commands(ns_parser) obbject = translator.execute_func(parsed_args=ns_parser) - df: pd.DataFrame = None + df: pd.DataFrame = pd.DataFrame() fig: OpenBBFigure = None title = f"{self.PATH}{translator.func.__name__}" if obbject: - if session.max_obbjects_exceeded(): - session.obbject_registry.remove() - - # use the obbject to store the command so we can display it later on results - obbject.extra["command"] = f"{title} {' '.join(other_args)}" - - session.obbject_registry.register(obbject) - # we need to force to re-link so that the new obbject - # is immediately available for data processing commands - self._link_obbject_to_data_processing_commands() - # also update the completer - self.update_completer(self.choices_default) - - if session.settings.SHOW_MSG_OBBJECT_REGISTRY and isinstance( - obbject, OBBject - ): - session.console.print("Added OBBject to registry.") - - if hasattr(ns_parser, "chart") and ns_parser.chart: - obbject.show() - fig = obbject.chart.fig - if hasattr(obbject, "to_dataframe"): + + if isinstance(obbject, OBBject) and obbject.results: + if session.max_obbjects_exceeded(): + session.obbject_registry.remove() + session.console.print( + "[yellow]Maximum number of OBBjects reached. The oldest entry was removed.[yellow]" + ) + + # use the obbject to store the command so we can display it later on results + obbject.extra["command"] = f"{title} {' '.join(other_args)}" + + register_result = session.obbject_registry.register(obbject) + + # we need to force to re-link so that the new obbject + # is immediately available for data processing commands + self._link_obbject_to_data_processing_commands() + # also update the completer + self.update_completer(self.choices_default) + + if ( + session.settings.SHOW_MSG_OBBJECT_REGISTRY + and register_result + ): + session.console.print("Added OBBject to registry.") + + # making the dataframe available + # either for printing or exporting (or both) df = obbject.to_dataframe() - elif isinstance(obbject, dict): - df = pd.DataFrame.from_dict(obbject, orient="index") - else: - df = None - elif hasattr(obbject, "to_dataframe"): - df = obbject.to_dataframe() - if isinstance(df.columns, pd.RangeIndex): - df.columns = [str(i) for i in df.columns] - print_rich_table(df=df, show_index=True, title=title) + if hasattr(ns_parser, "chart") and ns_parser.chart: + obbject.show() + fig = obbject.chart.fig if obbject.chart else None + else: + if isinstance(df.columns, pd.RangeIndex): + df.columns = [str(i) for i in df.columns] + + print_rich_table(df=df, show_index=True, title=title) - elif isinstance(obbject, dict): - df = pd.DataFrame.from_dict(obbject, orient="index") - print_rich_table(df=df, show_index=True, title=title) + elif isinstance(obbject, dict): + df = pd.DataFrame.from_dict(obbject, orient="columns") + print_rich_table(df=df, show_index=True, title=title) - elif obbject: - session.console.print(obbject) + elif not isinstance(obbject, OBBject): + session.console.print(obbject) - if hasattr(ns_parser, "export") and ns_parser.export: + if ( + hasattr(ns_parser, "export") + and ns_parser.export + and not df.empty + ): sheet_name = getattr(ns_parser, "sheet_name", None) + if sheet_name and isinstance(sheet_name, list): + sheet_name = sheet_name[0] + export_data( export_type=",".join(ns_parser.export), dir_path=os.path.dirname(os.path.abspath(__file__)), @@ -209,11 +220,8 @@ def method(self, other_args: List[str], translator=translator): sheet_name=sheet_name, figure=fig, ) - - if session.max_obbjects_exceeded(): - session.console.print( - "[yellow]\nMaximum number of OBBjects reached. The oldest entry was removed.[yellow]" - ) + elif hasattr(ns_parser, "export") and ns_parser.export and df.empty: + session.console.print("[yellow]No data to export.[/yellow]") except Exception as e: session.console.print(f"[red]{e}[/]\n")