summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrique Joaquim <henriquecjoaquim@gmail.com>2024-05-14 08:03:17 +0100
committerGitHub <noreply@github.com>2024-05-14 07:03:17 +0000
commit922fc547f5e77e32695970923d35e53c8a6a8521 (patch)
tree3a8f0bdf7166a42ba73f21beb22d0cc8881afb77
parent10dddfd893680d5aa230ffab7ff6b0ed1c8f3f8b (diff)
[BugFix] Fix broken `--sheet-name` argument (#6401)
* remove hold command and its references * remove --local flag as we don't use it anymore @IgorWounds * reset package folder * reset reference * unnecessary line break removed * better handling of obbjects before printing/table/chart also fixes the sheet_name issue when writing to excel * fix linting; also, dataframe creation in the right place * proper handling of sheet name * orient to columns instead --------- Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com> Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
-rw-r--r--cli/openbb_cli/argparse_translator/obbject_registry.py4
-rw-r--r--cli/openbb_cli/controllers/base_platform_controller.py94
2 files changed, 54 insertions, 44 deletions
diff --git a/cli/openbb_cli/argparse_translator/obbject_registry.py b/cli/openbb_cli/argparse_translator/obbject_registry.py
index 1c4cbf80dd9..e1c73fd0c12 100644
--- a/cli/openbb_cli/argparse_translator/obbject_registry.py
+++ b/cli/openbb_cli/argparse_translator/obbject_registry.py
@@ -18,12 +18,14 @@ class Registry:
"""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 c154baa56cd..8ac12f4ed56 100644
--- a/cli/openbb_cli/controllers/base_platform_controller.py
+++ b/cli/openbb_cli/controllers/base_platform_controller.py
@@ -153,54 +153,65 @@ class PlatformController(BaseController):
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 @@ class PlatformController(BaseController):
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")