Skip to content

Commit

Permalink
fix interactive tables not working
Browse files Browse the repository at this point in the history
  • Loading branch information
deeleeramone committed Jul 15, 2024
1 parent 9be525d commit 615398b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
2 changes: 2 additions & 0 deletions cli/openbb_cli/controllers/base_platform_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 6 additions & 24 deletions cli/openbb_cli/controllers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions cli/openbb_cli/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -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."""
Expand Down

0 comments on commit 615398b

Please sign in to comment.