mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 21:42:22 +00:00
Allow economy menu to save figures (#739)
* Allow economy menu to save figures * Allow to export both raw data and plot simultaneously
This commit is contained in:
@@ -4,6 +4,7 @@ __docformat__ = "numpy"
|
||||
import os
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from tabulate import tabulate
|
||||
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.economy import alphavantage_model
|
||||
@@ -26,16 +27,25 @@ def realtime_performance_sector(raw: bool, export: str):
|
||||
df_rtp = df_sectors["Rank A: Real-Time Performance"]
|
||||
|
||||
if raw:
|
||||
print(df_rtp.to_string())
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_rtp.to_frame(),
|
||||
showindex=True,
|
||||
headers=["Sector", "Real-Time Performance"],
|
||||
floatfmt=".5f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_rtp.to_string())
|
||||
|
||||
else:
|
||||
df_rtp.plot(kind="bar")
|
||||
plt.title("Real Time Performance (%) per Sector")
|
||||
plt.tight_layout()
|
||||
plt.grid()
|
||||
if gtff.USE_ION:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -44,3 +54,8 @@ def realtime_performance_sector(raw: bool, export: str):
|
||||
"rtps",
|
||||
df_sectors,
|
||||
)
|
||||
|
||||
if not raw:
|
||||
if gtff.USE_ION:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
""" CNN View """
|
||||
__docformat__ = "numpy"
|
||||
|
||||
import os
|
||||
import matplotlib.pyplot as plt
|
||||
from pandas.plotting import register_matplotlib_converters
|
||||
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.config_plot import PLOT_DPI
|
||||
from gamestonk_terminal.economy import cnn_model
|
||||
from gamestonk_terminal.helper_funcs import plot_autoscale
|
||||
from gamestonk_terminal.helper_funcs import plot_autoscale, export_data
|
||||
|
||||
register_matplotlib_converters()
|
||||
|
||||
|
||||
def fear_and_greed_index(indicator: str):
|
||||
"""Display CNN Fear And Greed Index.
|
||||
def fear_and_greed_index(indicator: str, export: str):
|
||||
"""Display CNN Fear And Greed Index. [Source: CNN Business]
|
||||
|
||||
Parameters
|
||||
----------
|
||||
@@ -21,6 +22,8 @@ def fear_and_greed_index(indicator: str):
|
||||
CNN Fear And Greed indicator or index. From Junk Bond Demand, Market Volatility,
|
||||
Put and Call Options, Market Momentum Stock Price Strength, Stock Price Breadth,
|
||||
Safe Heaven Demand, and Index.
|
||||
export : str
|
||||
Export plot to png,jpg,pdf file
|
||||
"""
|
||||
fig = plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI)
|
||||
|
||||
@@ -31,6 +34,12 @@ def fear_and_greed_index(indicator: str):
|
||||
if indicator:
|
||||
plt.imshow(im)
|
||||
|
||||
export_data(
|
||||
export,
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"feargreed",
|
||||
)
|
||||
|
||||
if gtff.USE_ION:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
|
||||
@@ -263,6 +263,14 @@ FRED:
|
||||
Safe Heaven Demand, and Index.
|
||||
""",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--export",
|
||||
choices=["png", "jpg", "pdf", "svg"],
|
||||
default="",
|
||||
type=str,
|
||||
dest="export",
|
||||
help="Export plot to png,jpg,pdf,svg file",
|
||||
)
|
||||
try:
|
||||
if other_args:
|
||||
if "-" not in other_args[0]:
|
||||
@@ -272,7 +280,10 @@ FRED:
|
||||
if not ns_parser:
|
||||
return
|
||||
|
||||
cnn_view.fear_and_greed_index(indicator=ns_parser.indicator)
|
||||
cnn_view.fear_and_greed_index(
|
||||
indicator=ns_parser.indicator,
|
||||
export=ns_parser.export,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(e, "\n")
|
||||
@@ -595,11 +606,19 @@ FRED:
|
||||
"-g",
|
||||
"--group",
|
||||
type=str,
|
||||
default="Sector",
|
||||
default="sector",
|
||||
dest="group",
|
||||
help="Data group (sector, industry or country)",
|
||||
choices=list(self.d_GROUPS.keys()),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--export",
|
||||
choices=["png", "jpg", "pdf", "svg"],
|
||||
default="",
|
||||
type=str,
|
||||
dest="export",
|
||||
help="Export plot to png,jpg,pdf,svg file",
|
||||
)
|
||||
try:
|
||||
if other_args:
|
||||
if "-" not in other_args[0]:
|
||||
@@ -641,11 +660,13 @@ FRED:
|
||||
)
|
||||
parser.add_argument(
|
||||
"--export",
|
||||
choices=["csv", "json", "xlsx"],
|
||||
choices=["csv", "json", "xlsx"]
|
||||
if "--raw" in other_args
|
||||
else ["png", "jpg", "pdf", "svg"],
|
||||
default="",
|
||||
type=str,
|
||||
dest="export",
|
||||
help="Export dataframe data to csv,json,xlsx file",
|
||||
help="Export data to csv,json,xlsx or png,jpg,pdf,svg file",
|
||||
)
|
||||
try:
|
||||
ns_parser = parse_known_args_and_warn(parser, other_args)
|
||||
@@ -692,11 +713,13 @@ FRED:
|
||||
)
|
||||
parser.add_argument(
|
||||
"--export",
|
||||
choices=["csv", "json", "xlsx"],
|
||||
choices=["csv", "json", "xlsx"]
|
||||
if "--raw" in other_args
|
||||
else ["png", "jpg", "pdf", "svg"],
|
||||
default="",
|
||||
type=str,
|
||||
dest="export",
|
||||
help="Export dataframe data to csv,json,xlsx file",
|
||||
help="Export data to csv,json,xlsx or png,jpg,pdf,svg file",
|
||||
)
|
||||
try:
|
||||
if other_args:
|
||||
|
||||
BIN
gamestonk_terminal/economy/feargreed.jpg
vendored
Normal file
BIN
gamestonk_terminal/economy/feargreed.jpg
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 96 KiB |
@@ -2,6 +2,7 @@ import os
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.economy import finnhub_model
|
||||
from gamestonk_terminal.helper_funcs import export_data
|
||||
|
||||
@@ -63,15 +64,18 @@ def economy_calendar_events(country: str, num: int, impact: str, export: str):
|
||||
df_econ_calendar.replace("", float("NaN"), inplace=True)
|
||||
df_econ_calendar.dropna(how="all", axis=1, inplace=True)
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_econ_calendar,
|
||||
headers=df_econ_calendar.columns,
|
||||
showindex=False,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_econ_calendar,
|
||||
headers=df_econ_calendar.columns,
|
||||
showindex=False,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_econ_calendar.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
|
||||
@@ -6,7 +6,9 @@ import webbrowser
|
||||
|
||||
from PIL import Image
|
||||
from tabulate import tabulate
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.economy import finviz_model
|
||||
from gamestonk_terminal.helper_funcs import export_data
|
||||
|
||||
@@ -41,20 +43,23 @@ def view_group_data(s_group: str, data_type: str, export: str):
|
||||
data_type : str
|
||||
select data type to see data between valuation, performance and spectrum
|
||||
export : str
|
||||
Export dataframe data to csv,json,xlsx file
|
||||
Export data to csv,json,xlsx or png,jpg,pdf,svg file
|
||||
"""
|
||||
if data_type in ("valuation", "performance"):
|
||||
df_group = finviz_model.get_valuation_performance_data(s_group, data_type)
|
||||
print(
|
||||
tabulate(
|
||||
df_group.fillna(""),
|
||||
showindex=False,
|
||||
floatfmt=".2f",
|
||||
headers=df_group.columns,
|
||||
tablefmt="fancy_grid",
|
||||
),
|
||||
"\n",
|
||||
)
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_group.fillna(""),
|
||||
showindex=False,
|
||||
floatfmt=".2f",
|
||||
headers=df_group.columns,
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_group.fillna("").to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
export,
|
||||
@@ -65,8 +70,17 @@ def view_group_data(s_group: str, data_type: str, export: str):
|
||||
|
||||
elif data_type == "spectrum":
|
||||
finviz_model.get_spectrum_data(s_group)
|
||||
print("")
|
||||
|
||||
img = Image.open(s_group + ".jpg")
|
||||
plt.imshow(img)
|
||||
|
||||
export_data(
|
||||
export,
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"spectrum",
|
||||
)
|
||||
|
||||
img.show()
|
||||
|
||||
else:
|
||||
|
||||
@@ -41,7 +41,7 @@ def display_series(series: str, start_date: str, raw: bool, export: str):
|
||||
raw : bool
|
||||
Output only raw data
|
||||
export : str
|
||||
Export dataframe data to csv,json,xlsx file
|
||||
Export data to csv,json,xlsx or png,jpg,pdf,svg file
|
||||
"""
|
||||
if export:
|
||||
l_series_fred = []
|
||||
@@ -91,16 +91,19 @@ def display_series(series: str, start_date: str, raw: bool, export: str):
|
||||
|
||||
if raw:
|
||||
df_fred.index = df_fred.index.strftime("%d/%m/%Y")
|
||||
print(
|
||||
tabulate(
|
||||
df_fred.dropna().to_frame(),
|
||||
showindex=True,
|
||||
headers=[f"{ser}: {ser_title}"],
|
||||
tablefmt="fancy_grid",
|
||||
floatfmt=".2f",
|
||||
),
|
||||
"\n",
|
||||
)
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_fred.dropna().to_frame(),
|
||||
showindex=True,
|
||||
headers=[f"{ser}: {ser_title}"],
|
||||
tablefmt="fancy_grid",
|
||||
floatfmt=".2f",
|
||||
),
|
||||
"\n",
|
||||
)
|
||||
else:
|
||||
print(df_fred.dropna().to_frame().to_string(), "\n")
|
||||
|
||||
else:
|
||||
success += 1
|
||||
@@ -131,11 +134,16 @@ def display_series(series: str, start_date: str, raw: bool, export: str):
|
||||
[val for _, val in p.items()], success_titles, loc="best", prop={"size": 6}
|
||||
)
|
||||
plt.gca().spines["left"].set_visible(False)
|
||||
export_data(
|
||||
export,
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
"series",
|
||||
)
|
||||
if gtff.USE_ION:
|
||||
plt.ion()
|
||||
plt.show()
|
||||
|
||||
if export:
|
||||
if export and raw:
|
||||
df_data = pd.concat(l_series_fred, axis=1)
|
||||
df_data.columns = success_series
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
from gamestonk_terminal import feature_flags as gtff
|
||||
from gamestonk_terminal.economy import wsj_model
|
||||
from gamestonk_terminal.helper_funcs import export_data
|
||||
|
||||
@@ -22,15 +23,18 @@ def display_overview(export: str):
|
||||
print("No overview data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -54,15 +58,18 @@ def display_indices(export: str):
|
||||
print("No indices data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -86,15 +93,18 @@ def display_futures(export: str):
|
||||
print("No futures/commodities data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -118,15 +128,18 @@ def display_usbonds(export: str):
|
||||
print("No US bonds data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -150,15 +163,18 @@ def display_glbonds(export: str):
|
||||
print("No global bonds data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
@@ -182,15 +198,18 @@ def display_currencies(export: str):
|
||||
print("No currencies data available\n")
|
||||
return
|
||||
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
if gtff.USE_TABULATE_DF:
|
||||
print(
|
||||
tabulate(
|
||||
df_data,
|
||||
showindex=False,
|
||||
headers=df_data.columns,
|
||||
floatfmt=".2f",
|
||||
tablefmt="fancy_grid",
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(df_data.to_string(index=False))
|
||||
print("")
|
||||
|
||||
export_data(
|
||||
|
||||
@@ -732,7 +732,9 @@ def find_tickers(submission):
|
||||
return l_tickers_found
|
||||
|
||||
|
||||
def export_data(export_type: str, dir_path: str, func_name: str, df: pd.DataFrame):
|
||||
def export_data(
|
||||
export_type: str, dir_path: str, func_name: str, df: pd.DataFrame = pd.DataFrame()
|
||||
):
|
||||
"""Export data to a file.
|
||||
|
||||
Parameters
|
||||
@@ -753,17 +755,32 @@ def export_data(export_type: str, dir_path: str, func_name: str, df: pd.DataFram
|
||||
full_path = os.path.abspath(
|
||||
os.path.join(
|
||||
export_dir,
|
||||
f"{func_name}_{now.strftime('%Y%m%d_%H%M%S')}.{export_type}",
|
||||
f"{func_name}_{now.strftime('%Y%m%d_%H%M%S')}",
|
||||
)
|
||||
)
|
||||
|
||||
if export_type == "csv":
|
||||
df.to_csv(full_path)
|
||||
elif export_type == "json":
|
||||
df.to_json(full_path)
|
||||
elif export_type in "xlsx":
|
||||
df.to_excel(full_path, index=True, header=True)
|
||||
else:
|
||||
print("Wrong export file specified.\n")
|
||||
if "," not in export_type:
|
||||
export_type += ","
|
||||
|
||||
print(f"Saved file: {full_path}\n")
|
||||
for exp_type in export_type.split(","):
|
||||
if exp_type:
|
||||
saved_path = f"{full_path}.{exp_type}"
|
||||
|
||||
if exp_type == "csv":
|
||||
df.to_csv(saved_path)
|
||||
elif exp_type == "json":
|
||||
df.to_json(saved_path)
|
||||
elif exp_type in "xlsx":
|
||||
df.to_excel(saved_path, index=True, header=True)
|
||||
elif exp_type == "png":
|
||||
plt.savefig(saved_path)
|
||||
elif exp_type == "jpg":
|
||||
plt.savefig(saved_path)
|
||||
elif exp_type == "pdf":
|
||||
plt.savefig(saved_path)
|
||||
elif exp_type == "svg":
|
||||
plt.savefig(saved_path)
|
||||
else:
|
||||
print("Wrong export file specified.\n")
|
||||
|
||||
print(f"Saved file: {saved_path}\n")
|
||||
|
||||
Reference in New Issue
Block a user