forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_markdown.py
214 lines (155 loc) · 5.3 KB
/
st_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
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# 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.
import streamlit as st
# keep the sidebar collapsed by default to prevent render flakiness
st.set_page_config(initial_sidebar_state="collapsed")
st.markdown(
"This **markdown** is awesome! :sunglasses:", help="This is a help tooltip!"
)
st.markdown("This <b>HTML tag</b> is escaped!")
st.markdown("This <b>HTML tag</b> is not escaped!", unsafe_allow_html=True)
st.markdown("[text]")
st.markdown("[link](href)")
st.markdown("[][]")
st.markdown(
"""
| Col1 | Col2 |
| --------- | ----------- |
| Some | Data |
"""
)
st.markdown(":blue-background[**Bold text within blue background**]")
st.markdown(":red-background[*Italic text within red background*]")
st.markdown(":rainbow-background[[Link](http://example.com) within rainbow background]")
st.markdown(
":green-background[LaTeX math within green background: $ax^2 + bx + c = 0$]"
)
st.markdown(
r"""
Many different markdown formats in one block:
Inline math with $\KaTeX$
$$
ax^2 + bx + c = 0
$$
## :material/home: Some header
| Col1 | Col2 |
| --------- | ----------- |
| Some | :material/description: Data |
Some text
- :blue[blue], :green[green], :red[red], :violet[violet], :orange[orange], :gray[gray], :grey[grey], :rainbow[rainbow]
- :blue-background[blue], :green-background[green], :red-background[red], :violet-background[violet], :orange-background[orange], :gray-background[gray], :grey-background[grey], :rainbow-background[rainbow]
- :material/chevron_right: Markdown can contain material icons :red[:material/local_fire_department:] :green-background[:material/celebration: Yay]
:blue-background[**Bold text within blue background**], :red-background[*Italic text within red background*]
:rainbow-background[[Link](http://example.com) within rainbow background], :green-background[LaTeX math within green background: $ax^2 + bx + c = 0$]
:violet-background[This is a repeating multiline string that wraps within purple background. This is a repeating multiline string that wraps within purple background.]
"""
)
# Headers in markdown tests (originally from the typography-test suite).
with st.container():
st.markdown("# some really long header " + " ".join(["lol"] * 10))
st.markdown(
"""
| Col1 | Col2 | Col3 | Col4 |
| --------- | ----------- | ----------- | ----------- |
| Some | Data | Data | Data |
"""
)
def draw_header_test(join_output):
strings = [
"# Header header1",
"## Header header2",
"### Header header3",
"#### Header header4",
"##### Header header5",
"###### Header header6",
"Quisque vel blandit mi. Fusce dignissim leo purus, in imperdiet lectus suscipit nec.",
]
if join_output:
st.markdown("\n\n".join(strings))
else:
for string in strings:
st.markdown(string)
draw_header_test(True)
with st.sidebar:
st.text_input("This is a label", key="1")
draw_header_test(True)
"---"
with st.container():
st.text("Headers in single st.markdown command")
draw_header_test(True)
"---"
with st.container():
st.text("Headers in multiple st.markdown command")
draw_header_test(False)
"---"
with st.container():
st.text("Headers in columns")
a, b = st.columns(2)
with a:
draw_header_test(True)
with b:
draw_header_test(False)
"---"
with st.container():
st.text("Headers in columns with other elements above")
a, b = st.columns(2)
with a:
st.text("This is some text")
draw_header_test(True)
with b:
st.text("This is some text")
with st.container():
draw_header_test(False)
"---"
with st.container():
st.text("Headers in column beside widget")
a, b = st.columns(2)
with a:
st.write("# Header header")
st.write("## Header header")
with b:
st.text_input("This is a label", key="2")
"---"
with st.container():
st.text("Headers with bold syntax")
strings = [
"# Bold **header1**",
"## Bold **header2**",
"### Bold **header3**",
"#### Bold **header4**",
"##### Bold **header5**",
"###### Bold **header6**",
]
for string in strings:
st.markdown(string)
"---"
st.latex(r"\LaTeX")
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)
""",
help="This is example tooltip displayed on latex.",
)
try:
import sympy
a, b = sympy.symbols("a b")
out = a + b
except Exception:
out = "a + b"
st.latex(out)
st.markdown(
"Images in markdown should stay inside the container width:\n\n"
)