From 615398b3a2dfb9c86471ad34dcbd0aeb2ca2b630 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:09:47 -0700 Subject: [PATCH] fix interactive tables not working --- .../controllers/base_platform_controller.py | 2 ++ cli/openbb_cli/controllers/utils.py | 30 ++++--------------- cli/openbb_cli/session.py | 19 ++++++++++++ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index 45ddea4bfca0..2f476bdc3c64 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -232,6 +232,8 @@ def method(self, other_args: List[str], translator=translator): fig = obbject.chart.fig if obbject.chart else None if not export: obbject.show() + elif session.settings.USE_INTERACTIVE_DF and not export: + obbject.charting.table() else: if isinstance(df.columns, pd.RangeIndex): df.columns = [str(i) for i in df.columns] diff --git a/cli/openbb_cli/controllers/utils.py b/cli/openbb_cli/controllers/utils.py index 5ab5a817a5ec..a6e03f12d924 100644 --- a/cli/openbb_cli/controllers/utils.py +++ b/cli/openbb_cli/controllers/utils.py @@ -16,11 +16,8 @@ import numpy as np import pandas as pd import requests -from openbb import obb -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 @@ -303,20 +300,6 @@ def return_colored_value(value: str): return f"{value}" -def _get_backend(): - """Get the Platform charting backend.""" - try: - return get_backend() - except ValueError: - # backend might not be created yet - charting_settings = ChartingSettings( - system_settings=obb.system, user_settings=obb.user # type: ignore - ) - create_backend(charting_settings) - get_backend().start(debug=charting_settings.debug_mode) - return get_backend() - - # pylint: disable=too-many-arguments def print_rich_table( # noqa: PLR0912 df: pd.DataFrame, @@ -385,7 +368,7 @@ def print_rich_table( # noqa: PLR0912 isinstance(df[col].iloc[x], pd.Timestamp) for x in range(min(10, len(df))) ): - df[col] = pd.to_numeric(df[col], errors="ignore") + df[col] = df[col].apply(pd.to_numeric) except (ValueError, TypeError): df[col] = df[col].astype(str) @@ -414,10 +397,7 @@ def _get_headers(_headers: Union[List[str], pd.Index]) -> List[str]: if col == "": df_outgoing = df_outgoing.rename(columns={col: " "}) - # ensure everything on the dataframe is a string - df_outgoing = df_outgoing.applymap(str) - - _get_backend().send_table( + session._backend.send_table( # type: ignore df_table=df_outgoing, title=title, theme=session.user.preferences.table_style, @@ -1014,12 +994,14 @@ def handle_obbject_display( if obbject.chart: obbject.show(**kwargs) else: - obbject.charting.to_chart(**kwargs) + obbject.charting.to_chart(**kwargs) # type: ignore if export: - fig = obbject.chart.fig + fig = obbject.chart.fig # type: ignore df = obbject.to_dataframe() except Exception as e: session.console.print(f"Failed to display chart: {e}") + elif session.settings.USE_INTERACTIVE_DF: + obbject.charting.table() # type: ignore else: df = obbject.to_dataframe() print_rich_table( diff --git a/cli/openbb_cli/session.py b/cli/openbb_cli/session.py index d78c83222a45..8b49537fb584 100644 --- a/cli/openbb_cli/session.py +++ b/cli/openbb_cli/session.py @@ -5,7 +5,9 @@ from typing import Optional from openbb import obb +from openbb_charting.core.backend import create_backend, get_backend from openbb_core.app.model.abstract.singleton import SingletonMeta +from openbb_core.app.model.charts.charting_settings import ChartingSettings from openbb_core.app.model.user_settings import UserSettings as User from prompt_toolkit import PromptSession @@ -17,11 +19,26 @@ from openbb_cli.models.settings import Settings +def _get_backend(): + """Get the Platform charting backend.""" + try: + return get_backend() + except ValueError: + # backend might not be created yet + charting_settings = ChartingSettings( + system_settings=obb.system, user_settings=obb.user # type: ignore + ) + create_backend(charting_settings) + get_backend().start(debug=charting_settings.debug_mode) # type: ignore + return get_backend() + + class Session(metaclass=SingletonMeta): """Session class.""" def __init__(self): """Initialize session.""" + self._obb = obb self._settings = Settings() self._style = Style( @@ -34,6 +51,8 @@ def __init__(self): self._prompt_session = self._get_prompt_session() self._obbject_registry = Registry() + self._backend = _get_backend() + @property def user(self) -> User: """Get platform user."""