[Feature] Improvements to handling charts on the CLI results (#6544)

* better handling of the chart arg: falls back to charting.to_chart() and only then, if not possible to display falls back to the print_rich_table()

* handling chart arguments aka unknown arguments

* propagating changes on return type of parse_simple_args to choices.py for coherency
This commit is contained in:
Henrique Joaquim
2024-06-26 14:08:56 +01:00
committed by GitHub
parent 37903a1cae
commit 5e69ce3d25
5 changed files with 134 additions and 47 deletions

View File

@@ -21,6 +21,7 @@ from openbb_charting.core.backend import create_backend, get_backend
from openbb_cli.config.constants import AVAILABLE_FLAIRS, ENV_FILE_SETTINGS
from openbb_cli.session import Session
from openbb_core.app.model.charts.charting_settings import ChartingSettings
from openbb_core.app.model.obbject import OBBject
from pytz import all_timezones, timezone
from rich.table import Table
@@ -974,3 +975,76 @@ def request(
timeout=timeout,
**kwargs,
)
def parse_unknown_args_to_dict(unknown_args: Optional[List[str]]) -> Dict[str, str]:
"""Parse unknown arguments to a dictionary."""
unknown_args_dict = {}
if unknown_args:
for idx, arg in enumerate(unknown_args):
if arg.startswith("--"):
if idx + 1 < len(unknown_args):
try:
unknown_args_dict[arg.replace("--", "")] = (
eval( # noqa: S307, E501 pylint: disable=eval-used
unknown_args[idx + 1]
)
)
except Exception:
unknown_args_dict[arg] = unknown_args[idx + 1]
else:
session.console.print(
f"Missing value for argument {arg}. Skipping this argument."
)
return unknown_args_dict
def handle_obbject_display(
obbject: OBBject,
chart: bool = False,
export: str = "",
sheet_name: str = "",
**kwargs,
):
"""Handle the display of an OBBject."""
df: pd.DataFrame = pd.DataFrame()
fig: Optional[OpenBBFigure] = None
if chart:
try:
if obbject.chart:
obbject.show(**kwargs)
else:
obbject.charting.to_chart(**kwargs)
if export:
fig = obbject.chart.fig
df = obbject.to_dataframe()
except Exception as e:
session.console.print(f"Failed to display chart: {e}")
else:
df = obbject.to_dataframe()
print_rich_table(
df=df,
show_index=True,
title=obbject.extra.get("command", ""),
export=bool(export),
)
if export and not df.empty:
if sheet_name and isinstance(sheet_name, list):
sheet_name = sheet_name[0]
func_name = (
obbject.extra.get("command", "")
.replace("/", "_")
.replace(" ", "_")
.replace("--", "_")
)
export_data(
export_type=",".join(export),
dir_path=os.path.dirname(os.path.abspath(__file__)),
func_name=func_name,
df=df,
sheet_name=sheet_name,
figure=fig,
)
elif export and df.empty:
session.console.print("[yellow]No data to export.[/yellow]")