fix: Edge case of temp html files deleted when multiple terminals open and one is closed (#4448)

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
This commit is contained in:
teh_coderer
2023-03-10 05:35:15 -06:00
committed by GitHub
parent 6a26d4e908
commit 49b0140df2
2 changed files with 34 additions and 20 deletions

View File

@@ -5,11 +5,11 @@ import os
import i18n
# IMPORTATION INTERNAL
from openbb_terminal.base_helpers import load_env_vars, strtobool, load_env_files
from openbb_terminal.base_helpers import load_env_files, load_env_vars, strtobool
from openbb_terminal.core.config.paths import MISCELLANEOUS_DIRECTORY
from openbb_terminal.core.config.paths_helper import init_userdata
from openbb_terminal.core.plots.backend import plots_backend
from openbb_terminal.core.session.current_user import get_current_user
from openbb_terminal.core.config.paths_helper import init_userdata
from .helper_classes import TerminalStyle as _TerminalStyle
@@ -21,12 +21,14 @@ theme = _TerminalStyle(
current_user.preferences.RICH_STYLE,
)
# Start the plots backend
plots_backend().start(load_env_vars("DEBUG_MODE", strtobool, False))
def start_required_configurations():
"""Starts the required configurations for the terminal to work"""
load_env_files()
init_userdata()
plots_backend().start(load_env_vars("DEBUG_MODE", strtobool, False))
# Logging section

View File

@@ -75,21 +75,23 @@ class Backend(pywry.PyWry):
for html_file, temp_file in zip(
["plotly.html", "table.html"], [self.plotly_html, self.table_html]
):
with open(PLOTS_CORE_PATH / html_file, encoding="utf-8") as file: # type: ignore
html = file.read()
html = html.replace("{{MAIN_PATH}}", str(PLOTS_CORE_PATH.as_uri()))
try:
with open(PLOTS_CORE_PATH / html_file, encoding="utf-8") as file: # type: ignore
html = file.read()
html = html.replace("{{MAIN_PATH}}", str(PLOTS_CORE_PATH.as_uri()))
# We create a temporary file to inject the path to the script tag
# This is so we don't have to modify the original file
# The file is deleted at program exit.
with open(temp_file, "w", encoding="utf-8") as file: # type: ignore
file.write(html)
def get_plotly_html(self) -> str:
"""Get the plotly html file."""
if self.plotly_html and self.plotly_html.exists():
return str(self.plotly_html)
return ""
# We create a temporary file to inject the path to the script tag
# This is so we don't have to modify the original file
# The file is deleted at program exit.
with open(temp_file, "w", encoding="utf-8") as file: # type: ignore
file.write(html)
except FileNotFoundError as error:
console.print(
f"[bold red]{html_file} file not found, check the path:[/]"
f"[green]{PLOTS_CORE_PATH / html_file}[/]"
)
self.max_retries = 0 # pylint: disable=W0201
raise error
def get_pending(self) -> list:
"""Get the pending data that has not been sent to the backend."""
@@ -99,11 +101,21 @@ class Backend(pywry.PyWry):
self.init_engine: list = []
return pending
def get_plotly_html(self) -> str:
"""Get the plotly html file."""
if not self.plotly_html.exists():
self.inject_path_to_html()
return self.get_plotly_html()
return str(self.plotly_html)
def get_table_html(self) -> str:
"""Get the table html file."""
if self.table_html and self.table_html.exists():
return str(self.table_html)
return ""
if not self.table_html.exists():
self.inject_path_to_html()
return self.get_table_html()
return str(self.table_html)
def get_window_icon(self) -> str:
"""Get the window icon."""