From d2fb7f7207e67df26eb653a3fb63e005bbe40c99 Mon Sep 17 00:00:00 2001 From: Theodore Aptekarev Date: Sun, 3 Oct 2021 18:48:07 +0300 Subject: Demonstrate hack to return a chart --- gamestonk_terminal/stocks/stocks_controller.py | 2 +- gamestonk_terminal/stocks/stocks_helper.py | 7 +++++-- .../kernel/gamestonk_kernel/gamestonk_kernel.py | 24 +++++++++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/gamestonk_terminal/stocks/stocks_controller.py b/gamestonk_terminal/stocks/stocks_controller.py index 0eeeb4884d1..ba50a0f5285 100644 --- a/gamestonk_terminal/stocks/stocks_controller.py +++ b/gamestonk_terminal/stocks/stocks_controller.py @@ -209,7 +209,7 @@ Market {('CLOSED', 'OPEN')[b_is_stock_market_open()]} def call_candle(self, other_args: List[str]): """Process candle command""" - candle( + return candle( self.ticker + "." + self.suffix if self.suffix else self.ticker, other_args, ) diff --git a/gamestonk_terminal/stocks/stocks_helper.py b/gamestonk_terminal/stocks/stocks_helper.py index 43be8afc846..8452ccc6cc2 100644 --- a/gamestonk_terminal/stocks/stocks_helper.py +++ b/gamestonk_terminal/stocks/stocks_helper.py @@ -458,9 +458,10 @@ def candle(s_ticker: str, other_args: List[str]): ) if gtff.USE_ION: - plt.ion() + # plt.ion() + plt.ioff() - mpf.plot( + candle_plot = mpf.plot( df_stock, type="candle", mav=(20, 50), @@ -475,8 +476,10 @@ def candle(s_ticker: str, other_args: List[str]): update_width_config=dict( candle_linewidth=1.0, candle_width=0.8, volume_linewidth=1.0 ), + returnfig=True, ) print("") + return candle_plot except Exception as e: print(e, "\n") diff --git a/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py b/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py index 39a6b40ae5d..83db4039bed 100644 --- a/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py +++ b/jupyterlab/kernel/gamestonk_kernel/gamestonk_kernel.py @@ -6,6 +6,14 @@ from ipykernel.kernelbase import Kernel from terminal import TerminalController, bootup +def _to_svg(fig): + """Return a svg image from a matplotlib figure.""" + imgdata = io.BytesIO() + fig.savefig(imgdata, format="svg") + imgdata.seek(0) + return imgdata.getvalue().decode("utf-8") + + class GamestonkKernel(Kernel): """The Gamestonk Kernel.""" @@ -57,12 +65,26 @@ class GamestonkKernel(Kernel): # True - Quit or Reset based on flag # False - Keep loop and show help menu + if process_input is not None and isinstance(process_input, tuple): + # We expect to receive a matplotlib figure in `process_input` + fig, _ = process_input + svg_fig = _to_svg(fig) + + content = { + "data": {"image/svg+xml": svg_fig}, + "metadata": { + "image/html": {"width": 600, "height": 400}, + "isolated": True, + }, + } + self.send_response(self.iopub_socket, "display_data", content) if process_input is not None and not isinstance( process_input, bool ): - # Quit terminal + # We expect to receive a controller in `process_input` self.t_controller = process_input if process_input is True and isinstance(process_input, bool): + # We expect to receive a True boolean in `process_input` self.t_controller = TerminalController() self.t_controller.print_help() -- cgit v1.2.3