Skip to content

Commit 96f1746

Browse files
authored
Merge pull request #3029 from streamlit/fix-1117
Call _repr_html_ when available
2 parents 6684e51 + fdbab9a commit 96f1746

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/streamlit/elements/write.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def write(self, *args, **kwargs):
4343
4444
1. You can pass in multiple arguments, all of which will be written.
4545
2. Its behavior depends on the input types as follows.
46-
3. It returns None, so it's "slot" in the App cannot be reused.
46+
3. It returns None, so its "slot" in the App cannot be reused.
4747
4848
Parameters
4949
----------
@@ -60,14 +60,15 @@ def write(self, *args, **kwargs):
6060
- write(func) : Displays information about a function.
6161
- write(module) : Displays information about the module.
6262
- write(dict) : Displays dict in an interactive widget.
63-
- write(obj) : The default is to print str(obj).
6463
- write(mpl_fig) : Displays a Matplotlib figure.
6564
- write(altair) : Displays an Altair chart.
6665
- write(keras) : Displays a Keras model.
6766
- write(graphviz) : Displays a Graphviz graph.
6867
- write(plotly_fig) : Displays a Plotly figure.
6968
- write(bokeh_fig) : Displays a Bokeh figure.
7069
- write(sympy_expr) : Prints SymPy expression using LaTeX.
70+
- write(htmlable) : Prints _repr_html_() for the object if available.
71+
- write(obj) : Prints str(obj) if otherwise unknown.
7172
7273
unsafe_allow_html : bool
7374
This is a keyword-only argument that defaults to False.
@@ -217,6 +218,11 @@ def flush_buffer():
217218
elif type_util.is_pydeck(arg):
218219
flush_buffer()
219220
self.dg.pydeck_chart(arg)
221+
elif hasattr(arg, "_repr_html_"):
222+
self.dg.markdown(
223+
arg._repr_html_(),
224+
unsafe_allow_html=True,
225+
)
220226
else:
221227
string_buffer.append("`%s`" % str(arg).replace("`", "\\`"))
222228

lib/tests/streamlit/write_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ class StreamlitWriteTest(unittest.TestCase):
3939
trying to check is that the right st.* method gets called
4040
"""
4141

42+
def test_repr_html(self):
43+
"""Test st.write with an object that defines _repr_html_."""
44+
45+
class FakeHTMLable(object):
46+
def _repr_html_(self):
47+
return "<strong>hello world</strong>"
48+
49+
with patch("streamlit.delta_generator.DeltaGenerator.markdown") as p:
50+
st.write(FakeHTMLable())
51+
52+
p.assert_called_once_with(
53+
"<strong>hello world</strong>", unsafe_allow_html=True
54+
)
55+
4256
def test_string(self):
4357
"""Test st.write with a string."""
4458
with patch("streamlit.delta_generator.DeltaGenerator.markdown") as p:

0 commit comments

Comments
 (0)