forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.py
279 lines (215 loc) · 9.27 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright 2018-2022 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import cast
from typing import Optional
from typing import TYPE_CHECKING
from typing import Union
from streamlit import type_util
from streamlit.proto.Markdown_pb2 import Markdown as MarkdownProto
from .utils import clean_text
if TYPE_CHECKING:
import sympy
from streamlit.delta_generator import DeltaGenerator
class MarkdownMixin:
def markdown(self, body: str, unsafe_allow_html: bool = False) -> "DeltaGenerator":
"""Display string formatted as Markdown.
Parameters
----------
body : str
The string to display as Github-flavored Markdown. Syntax
information can be found at: https://github.github.com/gfm.
This also supports:
* Emoji shortcodes, such as `:+1:` and `:sunglasses:`.
For a list of all supported codes,
see https://share.streamlit.io/streamlit/emoji-shortcodes.
* LaTeX expressions, by wrapping them in "$" or "$$" (the "$$"
must be on their own lines). Supported LaTeX functions are listed
at https://katex.org/docs/supported.html.
unsafe_allow_html : bool
By default, any HTML tags found in the body will be escaped and
therefore treated as pure text. This behavior may be turned off by
setting this argument to True.
That said, we *strongly advise against it*. It is hard to write
secure HTML, so by using this argument you may be compromising your
users' security. For more information, see:
https://github.com/streamlit/streamlit/issues/152
*Also note that `unsafe_allow_html` is a temporary measure and may
be removed from Streamlit at any time.*
If you decide to turn on HTML anyway, we ask you to please tell us
your exact use case here:
https://discuss.streamlit.io/t/96
This will help us come up with safe APIs that allow you to do what
you want.
Example
-------
>>> st.markdown('Streamlit is **_really_ cool**.')
"""
markdown_proto = MarkdownProto()
markdown_proto.body = clean_text(body)
markdown_proto.allow_html = unsafe_allow_html
dg = self.dg._enqueue("markdown", markdown_proto)
return cast("DeltaGenerator", dg)
def header(self, body: str, anchor: Optional[str] = None) -> "DeltaGenerator":
"""Display text in header formatting.
Parameters
----------
body : str
The text to display.
anchor : str
The anchor name of the header that can be accessed with #anchor
in the URL. If omitted, it generates an anchor using the body.
Example
-------
>>> st.header('This is a header')
"""
header_proto = MarkdownProto()
if anchor is None:
header_proto.body = f"## {clean_text(body)}"
else:
header_proto.body = f'<h2 data-anchor="{anchor}">{clean_text(body)}</h2>'
header_proto.allow_html = True
dg = self.dg._enqueue("markdown", header_proto)
return cast("DeltaGenerator", dg)
def subheader(self, body: str, anchor: Optional[str] = None) -> "DeltaGenerator":
"""Display text in subheader formatting.
Parameters
----------
body : str
The text to display.
anchor : str
The anchor name of the header that can be accessed with #anchor
in the URL. If omitted, it generates an anchor using the body.
Example
-------
>>> st.subheader('This is a subheader')
"""
subheader_proto = MarkdownProto()
if anchor is None:
subheader_proto.body = f"### {clean_text(body)}"
else:
subheader_proto.body = f'<h3 data-anchor="{anchor}">{clean_text(body)}</h3>'
subheader_proto.allow_html = True
dg = self.dg._enqueue("markdown", subheader_proto)
return cast("DeltaGenerator", dg)
def code(self, body: str, language: Optional[str] = "python") -> "DeltaGenerator":
"""Display a code block with optional syntax highlighting.
(This is a convenience wrapper around `st.markdown()`)
Parameters
----------
body : str
The string to display as code.
language : str
The language that the code is written in, for syntax highlighting.
If omitted, the code will be unstyled.
Example
-------
>>> code = '''def hello():
... print("Hello, Streamlit!")'''
>>> st.code(code, language='python')
"""
code_proto = MarkdownProto()
markdown = "```%(language)s\n%(body)s\n```" % {
"language": language or "",
"body": body,
}
code_proto.body = clean_text(markdown)
dg = self.dg._enqueue("markdown", code_proto)
return cast("DeltaGenerator", dg)
def title(self, body: str, anchor: Optional[str] = None) -> "DeltaGenerator":
"""Display text in title formatting.
Each document should have a single `st.title()`, although this is not
enforced.
Parameters
----------
body : str
The text to display.
anchor : str
The anchor name of the header that can be accessed with #anchor
in the URL. If omitted, it generates an anchor using the body.
Example
-------
>>> st.title('This is a title')
"""
title_proto = MarkdownProto()
if anchor is None:
title_proto.body = f"# {clean_text(body)}"
else:
title_proto.body = f'<h1 data-anchor="{anchor}">{clean_text(body)}</h1>'
title_proto.allow_html = True
dg = self.dg._enqueue("markdown", title_proto)
return cast("DeltaGenerator", dg)
def caption(self, body: str, unsafe_allow_html: bool = False) -> "DeltaGenerator":
"""Display text in small font.
This should be used for captions, asides, footnotes, sidenotes, and
other explanatory text.
Parameters
----------
body : str
The text to display.
unsafe_allow_html : bool
By default, any HTML tags found in strings will be escaped and
therefore treated as pure text. This behavior may be turned off by
setting this argument to True.
That said, *we strongly advise against it*. It is hard to write secure
HTML, so by using this argument you may be compromising your users'
security. For more information, see:
https://github.com/streamlit/streamlit/issues/152
**Also note that `unsafe_allow_html` is a temporary measure and may be
removed from Streamlit at any time.**
If you decide to turn on HTML anyway, we ask you to please tell us your
exact use case here:
https://discuss.streamlit.io/t/96 .
This will help us come up with safe APIs that allow you to do what you
want.
Example
-------
>>> st.caption('This is a string that explains something above.')
"""
caption_proto = MarkdownProto()
caption_proto.body = clean_text(body)
caption_proto.allow_html = unsafe_allow_html
caption_proto.is_caption = True
dg = self.dg._enqueue("markdown", caption_proto)
return cast("DeltaGenerator", dg)
def latex(self, body: Union[str, "sympy.Expr"]) -> "DeltaGenerator":
# This docstring needs to be "raw" because of the backslashes in the
# example below.
r"""Display mathematical expressions formatted as LaTeX.
Supported LaTeX functions are listed at
https://katex.org/docs/supported.html.
Parameters
----------
body : str or SymPy expression
The string or SymPy expression to display as LaTeX. If str, it's
a good idea to use raw Python strings since LaTeX uses backslashes
a lot.
Example
-------
>>> st.latex(r'''
... a + ar + a r^2 + a r^3 + \cdots + a r^{n-1} =
... \sum_{k=0}^{n-1} ar^k =
... a \left(\frac{1-r^{n}}{1-r}\right)
... ''')
"""
if type_util.is_sympy_expession(body):
import sympy
body = sympy.latex(body)
latex_proto = MarkdownProto()
latex_proto.body = "$$\n%s\n$$" % clean_text(body)
dg = self.dg._enqueue("markdown", latex_proto)
return cast("DeltaGenerator", dg)
@property
def dg(self) -> "DeltaGenerator":
"""Get our DeltaGenerator."""
return cast("DeltaGenerator", self)