From 539ea5f145f0864bece3afa3211f793bf6fedb31 Mon Sep 17 00:00:00 2001 From: didier Date: Sat, 13 Mar 2021 00:41:54 +0000 Subject: [PATCH] Add colors to financial market watch data, similarly to website --- .../fundamental_analysis/market_watch_api.py | 29 ++++++++++++++++++- gamestonk_terminal/helper_funcs.py | 14 +++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gamestonk_terminal/fundamental_analysis/market_watch_api.py b/gamestonk_terminal/fundamental_analysis/market_watch_api.py index 2915af7c63e..795902841a8 100644 --- a/gamestonk_terminal/fundamental_analysis/market_watch_api.py +++ b/gamestonk_terminal/fundamental_analysis/market_watch_api.py @@ -2,7 +2,13 @@ import argparse import requests import pandas as pd from bs4 import BeautifulSoup -from gamestonk_terminal.helper_funcs import get_user_agent, parse_known_args_and_warn +from gamestonk_terminal.helper_funcs import ( + get_user_agent, + parse_known_args_and_warn, + patch_pandas_text_adjustment, + financials_colored_values, +) +from gamestonk_terminal import feature_flags as gtff def income(l_args, s_ticker): @@ -52,6 +58,13 @@ def income(l_args, s_ticker): df_financials = prepare_df_financials(url_financials, ns_parser.b_quarter) + if gtff.USE_COLOR: + df_financials = df_financials.applymap(financials_colored_values) + + patch_pandas_text_adjustment() + pd.set_option("display.max_colwidth", None) + pd.set_option("display.max_rows", None) + print(df_financials.to_string(index=False)) print("") @@ -120,6 +133,13 @@ def balance(l_args, s_ticker): df_financials = prepare_df_financials(url_financials, ns_parser.b_quarter) + if gtff.USE_COLOR: + df_financials = df_financials.applymap(financials_colored_values) + + patch_pandas_text_adjustment() + pd.set_option("display.max_colwidth", None) + pd.set_option("display.max_rows", None) + print(df_financials.to_string(index=False)) print("") @@ -181,6 +201,13 @@ def cash(l_args, s_ticker): df_financials = prepare_df_financials(url_financials, ns_parser.b_quarter) + if gtff.USE_COLOR: + df_financials = df_financials.applymap(financials_colored_values) + + patch_pandas_text_adjustment() + pd.set_option("display.max_colwidth", None) + pd.set_option("display.max_rows", None) + print(df_financials.to_string(index=False)) print("") diff --git a/gamestonk_terminal/helper_funcs.py b/gamestonk_terminal/helper_funcs.py index 4dfae92c5a6..f1a46caab26 100644 --- a/gamestonk_terminal/helper_funcs.py +++ b/gamestonk_terminal/helper_funcs.py @@ -310,6 +310,20 @@ def print_pretty_prediction(df_pred: pd.DataFrame, last_price: float): print(df_pred.to_string()) +def financials_colored_values(val: str) -> str: + if sum(c.isalpha() for c in val) < 2: + if "%" in val: + if "-" in val: + color = Fore.RED + else: + color = Fore.GREEN + return f"{color}{val}{Style.RESET_ALL}" + elif "(" in val: + color = Fore.RED + return f"{color}{val}{Style.RESET_ALL}" + return val + + def check_ohlc(type_ohlc: str) -> str: if bool(re.match("^[ohlca]+$", type_ohlc)): return type_ohlc