mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
automatically color table based on values - parameter added to print_help_text
This commit is contained in:
@@ -12,6 +12,7 @@ from datetime import date as d
|
||||
import types
|
||||
from collections.abc import Iterable
|
||||
import os
|
||||
import math
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
@@ -218,6 +219,86 @@ def similar(a: str, b: str) -> float:
|
||||
return SequenceMatcher(None, a, b).ratio()
|
||||
|
||||
|
||||
def is_a_number(value: str):
|
||||
"""Return whether a string is a number (int or float) or contains one
|
||||
|
||||
Parameters
|
||||
----------
|
||||
value: str
|
||||
string to be checked
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool:
|
||||
True if the string has a number (NaN not included), false otherwise.
|
||||
"""
|
||||
if " " in value:
|
||||
for v in value.split(" "):
|
||||
try:
|
||||
int(v)
|
||||
return True
|
||||
except ValueError:
|
||||
try:
|
||||
my_n = float(v)
|
||||
if math.isnan(my_n):
|
||||
pass
|
||||
else:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
# Did not find any digit
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
int(value)
|
||||
return True
|
||||
except ValueError:
|
||||
try:
|
||||
my_n = float(value)
|
||||
if math.isnan(my_n):
|
||||
return False
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def is_a_zero(value: str):
|
||||
"""Return whether a string has the value 0 or not
|
||||
|
||||
Parameters
|
||||
----------
|
||||
value: str
|
||||
string to be checked
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool:
|
||||
True if the string has a number (NaN not included), false otherwise.
|
||||
"""
|
||||
if " " in value:
|
||||
for v in value.split(" "):
|
||||
try:
|
||||
my_num = float(v)
|
||||
if math.isnan(my_num):
|
||||
pass
|
||||
else:
|
||||
if my_num == 0:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
# Did not find any zero
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
my_num = float(value)
|
||||
if math.isnan(my_num):
|
||||
return False
|
||||
if my_num == 0:
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def print_rich_table(
|
||||
df: pd.DataFrame,
|
||||
show_index: bool = False,
|
||||
@@ -226,6 +307,7 @@ def print_rich_table(
|
||||
headers: Union[List[str], pd.Index] = None,
|
||||
floatfmt: Union[str, List[str]] = ".2f",
|
||||
show_header: bool = True,
|
||||
automatic_coloring: bool = False,
|
||||
):
|
||||
"""Prepare a table from df in rich.
|
||||
|
||||
@@ -245,10 +327,23 @@ def print_rich_table(
|
||||
Float number formatting specs as string or list of strings. Defaults to ".2f"
|
||||
show_header: bool
|
||||
Whether to show the header row.
|
||||
automatic_coloring: bool
|
||||
Automatically color a table based on positive and negative values
|
||||
"""
|
||||
if obbff.USE_TABULATE_DF:
|
||||
table = Table(title=title, show_lines=True, show_header=show_header)
|
||||
|
||||
if obbff.USE_COLOR and automatic_coloring:
|
||||
df = df.applymap(
|
||||
lambda x: f"[red]{x}[/red]"
|
||||
if "-" in str(x) and is_a_number(str(x))
|
||||
else f"[green]{x}[/green]"
|
||||
if is_a_number(str(x)) and not is_a_zero(str(x))
|
||||
else f"[yellow]{x}[/yellow]"
|
||||
if is_a_number(str(x))
|
||||
else f"{x}"
|
||||
)
|
||||
|
||||
if show_index:
|
||||
table.add_column(index_name)
|
||||
|
||||
|
||||
@@ -149,6 +149,7 @@ def display_income_statement(
|
||||
if not ratios
|
||||
else f"{'QoQ' if quarterly else 'YoY'} Change of {symbol} Income Statement",
|
||||
show_index=True,
|
||||
automatic_coloring=True,
|
||||
)
|
||||
|
||||
export_data(export, os.path.dirname(os.path.abspath(__file__)), "income", df_income)
|
||||
|
||||
@@ -125,6 +125,7 @@ def display_fundamentals(
|
||||
fundamentals.iloc[:, :limit].applymap(lambda x: "-" if x == "nan" else x),
|
||||
show_index=True,
|
||||
title=f"{symbol} {title_str}",
|
||||
automatic_coloring=True,
|
||||
)
|
||||
export_data(
|
||||
export, os.path.dirname(os.path.abspath(__file__)), statement, fundamentals
|
||||
|
||||
@@ -535,6 +535,8 @@ def get_financial_growth(
|
||||
)
|
||||
|
||||
df_fa = clean_metrics_df(df_fa, num=limit)
|
||||
|
||||
df_fa = df_fa[df_fa.columns[::-1]]
|
||||
# Invalid API Keys
|
||||
except ValueError as e:
|
||||
console.print(e)
|
||||
|
||||
@@ -231,6 +231,7 @@ def display_income_statement(
|
||||
if not ratios
|
||||
else f"{'QoQ' if quarterly else 'YoY'} Change of {symbol.upper()} Income Statement",
|
||||
show_index=True,
|
||||
automatic_coloring=True,
|
||||
)
|
||||
|
||||
pd.set_option("display.max_colwidth", None)
|
||||
@@ -481,6 +482,7 @@ def display_financial_ratios(
|
||||
headers=list(ratios.columns),
|
||||
title=f"{symbol.upper()} Ratios",
|
||||
show_index=True,
|
||||
automatic_coloring=True,
|
||||
)
|
||||
|
||||
export_data(
|
||||
@@ -512,7 +514,6 @@ def display_financial_statement_growth(
|
||||
growth = fmp_model.get_financial_growth(symbol, limit, quarterly)
|
||||
|
||||
if not growth.empty:
|
||||
growth = growth[growth.columns[::-1]]
|
||||
print_rich_table(
|
||||
growth,
|
||||
headers=list(growth.columns),
|
||||
|
||||
@@ -116,6 +116,7 @@ def display_fundamentals(
|
||||
title=f"{symbol} {title_str}"
|
||||
if not ratios
|
||||
else f"{'QoQ' if quarterly else 'YoY'} Change of {symbol} {title_str}",
|
||||
automatic_coloring=True,
|
||||
)
|
||||
|
||||
export_data(
|
||||
|
||||
@@ -481,6 +481,7 @@ def display_fundamentals(
|
||||
formatted_df.iloc[:, :limit].applymap(lambda x: "-" if x == "nan" else x),
|
||||
show_index=True,
|
||||
title=f"{symbol} {title_str} Currency: {symbol_currency}",
|
||||
automatic_coloring=True,
|
||||
)
|
||||
export_data(
|
||||
export, os.path.dirname(os.path.abspath(__file__)), statement, fundamentals
|
||||
|
||||
Reference in New Issue
Block a user