From 450b2ee40f873b1cb78e53bbe4ed9136be6f407a Mon Sep 17 00:00:00 2001 From: jose-donato Date: Wed, 8 Mar 2023 16:40:08 +0000 Subject: [PATCH] fix: ooooo --- openbb_terminal/core/plots/backend.py | 54 +++++++++++++++++++ openbb_terminal/core/plots/liveoptions.html | 21 ++++++++ .../stocks/options/options_controller.py | 16 ++++++ 3 files changed, 91 insertions(+) create mode 100644 openbb_terminal/core/plots/liveoptions.html diff --git a/openbb_terminal/core/plots/backend.py b/openbb_terminal/core/plots/backend.py index d26945d4295..a39d03693cd 100644 --- a/openbb_terminal/core/plots/backend.py +++ b/openbb_terminal/core/plots/backend.py @@ -54,6 +54,7 @@ class Backend(pywry.PyWry): super().__init__(daemon=daemon, max_retries=max_retries) self.plotly_html: Path = (PLOTS_CORE_PATH / "plotly_temp.html").resolve() self.table_html: Path = (PLOTS_CORE_PATH / "table.html").resolve() + self.liveoptions_html: Path = (PLOTS_CORE_PATH / "liveoptions.html").resolve() self.inject_path_to_html() self.isatty = ( not JUPYTER_NOTEBOOK @@ -103,6 +104,12 @@ class Backend(pywry.PyWry): return str(self.table_html) return "" + def get_liveoptions_html(self) -> str: + """Get the table html file.""" + if self.liveoptions_html and self.liveoptions_html.exists(): + return str(self.liveoptions_html) + return "" + def get_window_icon(self) -> str: """Get the window icon.""" icon_path = PLOTS_CORE_PATH / "assets" / "Terminal_icon.png" @@ -231,6 +238,53 @@ class Backend(pywry.PyWry): ) ) + def send_liveoptions(self, df_table: pd.DataFrame, title: str = ""): + """Send table data to the backend to be displayed in a table. + + Parameters + ---------- + df_table : pd.DataFrame + Dataframe to send to backend. + title : str, optional + Title to display in the window, 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()) + for col in df_table.columns + ] + + # we add a percentage of max to the min column width + columnwidth = [ + int(x + (max(columnwidth) - min(columnwidth)) * 0.2) for x in columnwidth + ] + + # in case of a very small table we set a min width + width = max(int(min(sum(columnwidth) * 9.7, self.WIDTH + 100)), 800) + + json_data = json.loads(df_table.to_json(orient="split")) + json_data.update(dict(title=title)) + + self.outgoing.append( + json.dumps( + { + "html_path": self.get_liveoptions_html(), + "json_data": json.dumps(json_data), + "width": width, + "height": self.HEIGHT - 100, + **self.get_kwargs(title), + } + ) + ) + def send_html(self, html_str: str = "", html_path: str = "", title: str = ""): """Send HTML to the backend to be displayed in a window. diff --git a/openbb_terminal/core/plots/liveoptions.html b/openbb_terminal/core/plots/liveoptions.html new file mode 100644 index 00000000000..aba4cb2bd6d --- /dev/null +++ b/openbb_terminal/core/plots/liveoptions.html @@ -0,0 +1,21 @@ + + + + + + + Vite + Vue + + + + +
+ + + diff --git a/openbb_terminal/stocks/options/options_controller.py b/openbb_terminal/stocks/options/options_controller.py index 5b76cd65576..c6cb2cc9c68 100644 --- a/openbb_terminal/stocks/options/options_controller.py +++ b/openbb_terminal/stocks/options/options_controller.py @@ -8,6 +8,9 @@ from typing import List, Optional import pandas as pd +from openbb_terminal import ( + plots_backend, +) from openbb_terminal.core.session.current_user import get_current_user from openbb_terminal.custom_prompt_toolkit import NestedCompleter from openbb_terminal.decorators import log_start_end @@ -85,6 +88,7 @@ class OptionsController(BaseController): "vsurf", "greeks", "eodchain", + "live" ] CHOICES_MENUS = [ "pricing", @@ -306,6 +310,18 @@ class OptionsController(BaseController): return ["stocks", f"load {self.ticker}", "options"] return [] + @log_start_end(log=logger) + def call_live(self, other_args: List[str]): + """Process live command""" + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="live", + description="Live option chain.", + ) + + plots_backend().send_liveoptions(df_table=pd.DataFrame(), title="live options") + @log_start_end(log=logger) def call_calc(self, other_args: List[str]): """Process calc command"""