diff --git a/cli/openbb_cli/controllers/base_platform_controller.py b/cli/openbb_cli/controllers/base_platform_controller.py index 45ddea4bfca..2f476bdc3c6 100644 --- a/cli/openbb_cli/controllers/base_platform_controller.py +++ b/cli/openbb_cli/controllers/base_platform_controller.py @@ -232,6 +232,8 @@ class PlatformController(BaseController): 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 5ab5a817a5e..46beed4b795 100644 --- a/cli/openbb_cli/controllers/utils.py +++ b/cli/openbb_cli/controllers/utils.py @@ -16,11 +16,8 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union 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) @@ -396,7 +379,7 @@ def print_rich_table( # noqa: PLR0912 output = list(_headers) if len(output) != len(df.columns): raise ValueError("Length of headers does not match length of DataFrame.") - return output + return output # type: ignore if session.settings.USE_INTERACTIVE_DF: df_outgoing = df.copy() @@ -414,10 +397,7 @@ def print_rich_table( # noqa: PLR0912 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 # pylint: disable=protected-access 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 d78c83222a4..8b49537fb58 100644 --- a/cli/openbb_cli/session.py +++ b/cli/openbb_cli/session.py @@ -5,7 +5,9 @@ from pathlib import Path 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.config.style import Style 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 @@ class Session(metaclass=SingletonMeta): self._prompt_session = self._get_prompt_session() self._obbject_registry = Registry() + self._backend = _get_backend() + @property def user(self) -> User: """Get platform user."""