Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix CLI Interactive Tables Not Opening #6588

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 7 additions & 25 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 All @@ -396,7 +379,7 @@ def _get_headers(_headers: Union[List[str], pd.Index]) -> List[str]:
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()
Expand All @@ -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 # pylint: disable=protected-access
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
Loading