Skip to content

Commit e199e54

Browse files
committed
Fix #260, #264 (based on implementation by @mr-easy)
1 parent dceac70 commit e199e54

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

lightweight_charts/widgets.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import html
23

34
from .util import parse_event_message
45
from lightweight_charts import abstract
@@ -33,12 +34,14 @@ def callback(self, message):
3334
emit_callback(self.win, message)
3435

3536
try:
36-
from streamlit.components.v1 import html
37+
from streamlit.components.v1 import html as sthtml
3738
except ImportError:
38-
html = None
39+
sthtml = None
3940

4041
try:
4142
from IPython.display import HTML, display
43+
import warnings
44+
warnings.filterwarnings("ignore", category=UserWarning, module="IPython.core.display")
4245
except ImportError:
4346
HTML = None
4447

@@ -129,9 +132,9 @@ def __init__(self, width=None, height=None, inner_width=1, inner_height=1, scale
129132
super().__init__(width, height, inner_width, inner_height, scale_candles_only, toolbox)
130133

131134
def _load(self):
132-
if html is None:
135+
if sthtml is None:
133136
raise ModuleNotFoundError('streamlit.components.v1.html was not found, and must be installed to use StreamlitChart.')
134-
html(f'{self._html}</script></body></html>', width=self.width, height=self.height)
137+
sthtml(f'{self._html}</script></body></html>', width=self.width, height=self.height)
135138

136139

137140
class JupyterChart(StaticLWC):
@@ -153,4 +156,6 @@ def __init__(self, width: int = 800, height=350, inner_width=1, inner_height=1,
153156
def _load(self):
154157
if HTML is None:
155158
raise ModuleNotFoundError('IPython.display.HTML was not found, and must be installed to use JupyterChart.')
156-
display(HTML(f'{self._html}</script></body></html>'))
159+
html_code = html.escape(f"{self._html}</script></body></html>")
160+
iframe = f'<iframe width="{self.width}" height="{self.height}" frameBorder="0" srcdoc="{html_code}"></iframe>'
161+
display(HTML(iframe))

0 commit comments

Comments
 (0)