[Feature] Improved cached results (#6506)

* flag to store cached results, or not

* using keys to reference obbjects in the registry and data processing commands

* out of bonds check

* controlling results, by index or key and displaying charts

* autocompletion

* deps

* forward ref

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
This commit is contained in:
Henrique Joaquim
2024-06-18 15:19:39 +01:00
committed by GitHub
parent 5c79635da2
commit fdfacc8363
10 changed files with 321 additions and 191 deletions

View File

@@ -24,6 +24,7 @@ from openbb_cli.controllers.utils import (
print_rich_table,
remove_file,
system_clear,
validate_register_key,
)
from openbb_cli.session import Session
from prompt_toolkit.formatted_text import HTML
@@ -628,19 +629,77 @@ class BaseController(metaclass=ABCMeta):
"'OBBjects' where all execution results are stored. "
"It is organized as a stack, with the most recent result at index 0.",
)
parser.add_argument("--index", dest="index", help="Index of the result.")
parser.add_argument("--key", dest="key", help="Key of the result.")
parser.add_argument(
"--chart", action="store_true", dest="chart", help="Display chart."
)
parser.add_argument(
"--export", dest="export", help="Export data.", nargs="+", default=None
)
ns_parser = self.parse_simple_args(parser, other_args)
if ns_parser:
results = session.obbject_registry.all
if results:
df = pd.DataFrame.from_dict(results, orient="index")
print_rich_table(
df,
show_index=True,
index_name="stack index",
title="OBBject Results",
)
else:
session.console.print("[info]No results found.[/info]")
if not ns_parser.index and not ns_parser.key:
results = session.obbject_registry.all
if results:
df = pd.DataFrame.from_dict(results, orient="index")
print_rich_table(
df,
show_index=True,
index_name="stack index",
title="OBBject Results",
)
else:
session.console.print("[info]No results found.[/info]")
elif ns_parser.index:
try:
index = int(ns_parser.index)
obbject = session.obbject_registry.get(index)
if obbject:
if ns_parser.chart and obbject.chart:
obbject.show()
else:
title = obbject.extra.get("command", "")
df = obbject.to_dataframe()
print_rich_table(
df=df,
show_index=True,
title=title,
export=ns_parser.export,
)
if ns_parser.chart and not obbject.chart:
session.console.print(
"[info]No chart available.[/info]"
)
else:
session.console.print(
f"[info]No result found at index {index}.[/info]"
)
except ValueError:
session.console.print(
f"[red]Index must be an integer, not '{ns_parser.index}'.[/red]"
)
elif ns_parser.key:
obbject = session.obbject_registry.get(ns_parser.key)
if obbject:
if ns_parser.chart and obbject.chart:
obbject.show()
else:
title = obbject.extra.get("command", "")
df = obbject.to_dataframe()
print_rich_table(
df=df,
show_index=True,
title=title,
export=ns_parser.export,
)
if ns_parser.chart and not obbject.chart:
session.console.print("[info]No chart available.[/info]")
else:
session.console.print(
f"[info]No result found with key '{ns_parser.key}'.[/info]"
)
@staticmethod
def parse_simple_args(parser: argparse.ArgumentParser, other_args: List[str]):
@@ -774,6 +833,22 @@ class BaseController(metaclass=ABCMeta):
help="Number of entries to show in data.",
type=check_positive,
)
parser.add_argument(
"--register_obbject",
dest="register_obbject",
action="store_false",
default=True,
help="Flag to store data in the OBBject registry, True by default.",
)
parser.add_argument(
"--register_key",
dest="register_key",
default="",
help="Key to reference data in the OBBject registry.",
type=validate_register_key,
)
if session.settings.USE_CLEAR_AFTER_CMD:
system_clear()