Skip to content

Commit

Permalink
Interactive tables using react (#4386)
Browse files Browse the repository at this point in the history
* feat: experimenting tables with react

* feat: adding new interactive tables

* feat: adding more table features

* small changes

* update

* Update backend.py

* Update backend.py

* fix: dark mode?

* feat: lot of table improvements

* fix: my b

* fix: load only when prod

* feat: naive detect links and dates

* handle index and column names

* increased min width

* codespell

* mypy, pylint

* fix: added new chart, export, etc

* testing

* updates

* fix : add PLOT_OPEN_EXPORT,  USE_INTERACTIVE_DF to user dataclass

* bump pywry version, readd removed `plots_backend().start()`

* req files

* Update unit-test.yml

* Update unit-test.yml

* Update unit-test.yml

* updates

* fixing some date things

* update table

* fix formatting with timezones

* fixing some long prints and tables

* Mickey Mouse

Co-authored-by: teh_coderer <[email protected]>
Co-authored-by: jose-donato <[email protected]>

* fix: ?!?!?

* adding limit arg to all print_rich_table

* tests

* lint

* simple by Default on interactive charts

* playing with some css (thks jose)

* Fix #4426

* Edit some structure

* fix sdk generation

* Update backend.py

* fa menu tested and working

* fix #4460

* fix #4460 drop indices

* update tests

* update tests

* Update helper_funcs.py

* tests

* Update unit-test.yml

* Update etf.ipynb

---------

Co-authored-by: teh_coderer <[email protected]>
Co-authored-by: andrewkenreich <[email protected]>
Co-authored-by: teh_coderer <[email protected]>
Co-authored-by: jose-donato <[email protected]>
Co-authored-by: James Maslek <[email protected]>
  • Loading branch information
6 people committed Apr 12, 2023
1 parent c3eed53 commit 1298161
Show file tree
Hide file tree
Showing 16 changed files with 921 additions and 26 deletions.
837 changes: 837 additions & 0 deletions frontend-components/tables/src/components/Table.tsx

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions openbb_terminal/alternative/oss/runa_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ def display_rossindex(
if df.empty:
console.print("\nError in runa request\n")
else:
plots_backend().send_table(df, title="Runa ROSS Index")

if sortby in runa_model.SORT_COLUMNS:
df = df.sort_values(by=sortby, ascending=ascend)
df = df.head(limit)
Expand Down
15 changes: 14 additions & 1 deletion openbb_terminal/core/plots/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,23 @@ def send_table(self, df_table: pd.DataFrame, title: str = "", source: str = ""):
Source of the data, by default ""
"""
self.loop.run_until_complete(self.check_backend())

if title:
# We remove any html tags and markdown from the title
title = re.sub(r"<[^>]*>", "", title)
title = re.sub(r"\[\/?[a-z]+\]", "", title)

# we get the length of each column using the max length of the column
# name and the max length of the column values as the column width
columnwidth = [
max(len(str(df_table[col].name)), df_table[col].astype(str).str.len().max())
max(
len(str(df_table[col].name)),
df_table[col].astype(str).str.len().max(),
)
for col in df_table.columns
if hasattr(df_table[col], "name") and hasattr(df_table[col], "dtype")
]

# we add a percentage of max to the min column width
columnwidth = [
int(x + (max(columnwidth) - min(columnwidth)) * 0.2) for x in columnwidth
Expand Down
1 change: 0 additions & 1 deletion openbb_terminal/cryptocurrency/defi/defi_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ def call_ldapps(self, other_args: List[str]):
)
if ns_parser:
llama_view.display_defi_protocols(
interactive=ns_parser.interactive,
limit=ns_parser.limit,
sortby=ns_parser.sortby,
ascend=ns_parser.reverse,
Expand Down
1 change: 0 additions & 1 deletion openbb_terminal/cryptocurrency/defi/llama_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def display_grouped_defi_protocols(
@log_start_end(log=logger)
def display_defi_protocols(
sortby: str,
interactive: bool = False,
limit: int = 20,
ascend: bool = False,
description: bool = False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def call_top(self, other_args):
if ns_parser:
if ns_parser.source == "CoinGecko":
pycoingecko_view.display_coins(
interactive=ns_parser.interactive,
sortby=" ".join(ns_parser.sortby),
category=ns_parser.category,
limit=ns_parser.limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
@log_start_end(log=logger)
def display_coins(
category: str,
interactive: bool = False,
limit: int = 250,
sortby: str = "Symbol",
export: str = "",
Expand Down
5 changes: 4 additions & 1 deletion openbb_terminal/economy/fred_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,15 @@ def plot_cpi(
fig.add_scatter(x=df.index, y=df[column].values, name=label)

if raw:
# was a -iloc so we need to flip the index as we use head
df = df.sort_index(ascending=False)
print_rich_table(
df.iloc[-10:],
df,
title=title,
show_index=True,
floatfmt=".3f",
export=bool(export),
limit=limit,
)

export_data(
Expand Down
48 changes: 48 additions & 0 deletions openbb_terminal/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,54 @@ def _get_headers(_headers: Union[List[str], pd.Index]) -> List[str]:
if columns_to_auto_color is None and rows_to_auto_color is None:
df = df.applymap(lambda x: return_colored_value(str(x)))

def _get_headers(_headers: Union[List[str], pd.Index]) -> List[str]:
"""Check if headers are valid and return them."""
output = _headers
if isinstance(_headers, pd.Index):
output = list(_headers)
if len(output) != len(df.columns):
log_and_raise(
ValueError("Length of headers does not match length of DataFrame")
)
return output

if current_user.preferences.USE_INTERACTIVE_DF and plots_backend().isatty:
df_outgoing = df.copy()
# If headers are provided, use them
if headers is not None:
# We check if headers are valid
df_outgoing.columns = _get_headers(headers)

if show_index and index_name not in df_outgoing.columns:
# If index name is provided, we use it
df_outgoing.index.name = index_name or "Index"
df_outgoing = df_outgoing.reset_index()

for col in df_outgoing.columns:
if col == "":
df_outgoing = df_outgoing.rename(columns={col: " "})

plots_backend().send_table(df_table=df_outgoing, title=title)
return

df = df.copy() if not limit else df.copy().iloc[:limit]
if current_user.preferences.USE_COLOR and automatic_coloring:
if columns_to_auto_color:
for col in columns_to_auto_color:
# checks whether column exists
if col in df.columns:
df[col] = df[col].apply(lambda x: return_colored_value(str(x)))
if rows_to_auto_color:
for row in rows_to_auto_color:
# checks whether row exists
if row in df.index:
df.loc[row] = df.loc[row].apply(
lambda x: return_colored_value(str(x))
)

if columns_to_auto_color is None and rows_to_auto_color is None:
df = df.applymap(lambda x: return_colored_value(str(x)))

if current_user.preferences.USE_TABULATE_DF:
table = Table(title=title, show_lines=True, show_header=show_header)

Expand Down
4 changes: 2 additions & 2 deletions openbb_terminal/reports/templates/etf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"import numpy as np\n",
"import datetime\n",
"\n",
"from IPython.display import HTML\n",
"\n",
"from IPython.display import HTML \n",
" \n",
"import openbb_terminal.config_terminal as cfg\n",
"from openbb_terminal.reports import widget_helpers as widgets\n",
"from openbb_terminal.sdk import openbb\n",
Expand Down
2 changes: 1 addition & 1 deletion openbb_terminal/stocks/stocks_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def call_candle(self, other_args: List[str]):
dest="ticker",
help="Ticker to analyze",
type=str,
default=self.ticker,
default=None,
required=not any(x in other_args for x in ["-h", "--help"])
and not self.ticker,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]
[yellow]Unable to check for updates... Check your internet connection and try again...[/yellow]


0 comments on commit 1298161

Please sign in to comment.