Skip to content

Commit

Permalink
[BugFix] Fix broken --sheet-name argument (#6401)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Igor Radovanovic <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent 10dddfd commit 922fc54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
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

0 comments on commit 922fc54

Please sign in to comment.