Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix broken --sheet-name argument #6401

Merged
merged 16 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli/openbb_cli/argparse_translator/obbject_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
94 changes: 51 additions & 43 deletions cli/openbb_cli/controllers/base_platform_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)),
Expand All @@ -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")
Expand Down
Loading