Fix #260, #264 (based on implementation by @mr-easy)

This commit is contained in:
louisnw
2024-02-24 12:36:22 +00:00
parent dceac70913
commit e199e54c7a

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import html
from .util import parse_event_message from .util import parse_event_message
from lightweight_charts import abstract from lightweight_charts import abstract
@ -33,12 +34,14 @@ if QWebEngineView:
emit_callback(self.win, message) emit_callback(self.win, message)
try: try:
from streamlit.components.v1 import html from streamlit.components.v1 import html as sthtml
except ImportError: except ImportError:
html = None sthtml = None
try: try:
from IPython.display import HTML, display from IPython.display import HTML, display
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="IPython.core.display")
except ImportError: except ImportError:
HTML = None HTML = None
@ -129,9 +132,9 @@ class StreamlitChart(StaticLWC):
super().__init__(width, height, inner_width, inner_height, scale_candles_only, toolbox) super().__init__(width, height, inner_width, inner_height, scale_candles_only, toolbox)
def _load(self): def _load(self):
if html is None: if sthtml is None:
raise ModuleNotFoundError('streamlit.components.v1.html was not found, and must be installed to use StreamlitChart.') raise ModuleNotFoundError('streamlit.components.v1.html was not found, and must be installed to use StreamlitChart.')
html(f'{self._html}</script></body></html>', width=self.width, height=self.height) sthtml(f'{self._html}</script></body></html>', width=self.width, height=self.height)
class JupyterChart(StaticLWC): class JupyterChart(StaticLWC):
@ -153,4 +156,6 @@ class JupyterChart(StaticLWC):
def _load(self): def _load(self):
if HTML is None: if HTML is None:
raise ModuleNotFoundError('IPython.display.HTML was not found, and must be installed to use JupyterChart.') raise ModuleNotFoundError('IPython.display.HTML was not found, and must be installed to use JupyterChart.')
display(HTML(f'{self._html}</script></body></html>')) html_code = html.escape(f"{self._html}</script></body></html>")
iframe = f'<iframe width="{self.width}" height="{self.height}" frameBorder="0" srcdoc="{html_code}"></iframe>'
display(HTML(iframe))