-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlayout_tabs.qmd
More file actions
274 lines (190 loc) · 6.94 KB
/
layout_tabs.qmd
File metadata and controls
274 lines (190 loc) · 6.94 KB
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
---
title: "Tabs"
filters:
- whitphx/stlite
---
## Tabs in Streamlit
Tabs are an extremely useful way to organise your page into something that is easier to navigate.
Tabs look like this in a Streamlit app.
```{python}
#| eval: False
import streamlit as st
tab1, tab2, tab3 = st.tabs(["This is Tab 1", "This is the Second Tab", "Tab 3 is Here!"])
with tab1:
st.header("I'm Tab 1")
st.write("Here's the 'Back to the Future' poster. Images, videos, data tables and more can be displayed within tabs.")
st.image("https://upload.wikimedia.org/wikipedia/en/d/d2/Back_to_the_Future.jpg")
with tab2:
st.header("I'm Tab 2")
st.write("We can use inputs within tabs too.")
name = st.text_input("What's your name?", value=None)
if name is not None:
st.write(f"Nice to meet you, {name}!")
else:
st.write("I can't greet you until you enter your name!")
with tab3:
if name is not None:
st.write(f"Hello again, {name}!")
st.write("Isn't it cool that variables persist across different tabs? This can be really handy!")
st.video("https://youtu.be/dQw4w9WgXcQ?feature=shared")
else:
st.write("I can't greet you until you enter your name! Go back to tab 2 and do that.")
```
```{stlite-python}
import streamlit as st
tab1, tab2, tab3 = st.tabs(["This is Tab 1", "This is the Second Tab", "Tab 3 is Here!"])
with tab1:
st.header("I'm Tab 1")
st.write("Here's the 'Back to the Future' poster. Images, videos, data tables and more can be displayed within tabs.")
st.image("https://upload.wikimedia.org/wikipedia/en/d/d2/Back_to_the_Future.jpg")
with tab2:
st.header("I'm Tab 2")
st.write("We can use inputs within tabs too.")
name = st.text_input("What's your name?", value=None)
if name is not None:
st.write(f"Nice to meet you, {name}!")
else:
st.write("I can't greet you until you enter your name!")
with tab3:
if name is not None:
st.write(f"Hello again, {name}!")
st.write("Isn't it cool that variables persist across different tabs? This can be really handy!")
st.video("https://youtu.be/dQw4w9WgXcQ?feature=shared")
else:
st.write("I can't greet you until you enter your name! Go back to tab 2 and do that.")
```
## The two main syntax options for streamlit tabs
There are two main ways to put content inside of tabs.
1) Using a 'with' statement and indenting the code that should sit within the tab.
```{python}
#| eval: False
import streamlit as st
tab_a, tab_b = st.tabs(["A Tab", "Another Tab"])
with tab_a:
st.text("This is some content within tab 1")
with tab_b:
st.text("This is some content within tab 2")
```
2) Replacing the `st` in sections like `st.text()` with the variable name for the given tab.
```{python}
#| eval: False
import streamlit as st
tab_a, tab_b = st.tabs(["A Tab", "Another Tab"])
tab_a.text("This is some content within tab 1")
tab_b.text("This is some content within tab 2")
```
The outputs of both of these bits of code are identical!
It's up to you to choose which you prefer.
## Automatically generating variable numbers of tabs
In some instances, you may wish to reactively create a different number of tabs.
In this example, notice what happens to the random numbers as you change the number of tabs being generated.
### Using the 'tab.' syntax
```{python}
#| eval: false
import streamlit as st
import random
number_of_tabs_to_create = st.number_input("Enter the number of tabs you want to create", 2, 5, 3)
tab_list = st.tabs([f"Tab {i+1}" for i in range(number_of_tabs_to_create)])
for idx, tab in enumerate(tab_list):
tab.header(f"This is tab {idx+1}")
tab.write(f"Your random number for this tab is {random.randint(0, 10)}")
```
```{stlite-python}
import streamlit as st
import random
number_of_tabs_to_create = st.number_input("Enter the number of tabs you want to create", 2, 5, 3)
tab_list = st.tabs([f"Tab {i+1}" for i in range(number_of_tabs_to_create)])
for idx, tab in enumerate(tab_list):
tab.header(f"This is tab {idx+1}")
tab.write(f"Your random number for this tab is {random.randint(0, 10)}")
```
### Using the 'with' syntax
It's also possible to do this using the 'with' syntax.
In this example, in every separate tab, we pull back a random wikipedia page.
(However, due to the way this works behind the scenes, we don't get a new page on changing the number of tabs even though the app is rerunning each time)
```{python}
#| eval: false
import streamlit as st
import streamlit.components.v1 as components
number_of_tabs_to_create = st.number_input("Enter the number of tabs you want to create", 2, 5, 3)
tab_list = st.tabs([f"Tab {i+1}" for i in range(number_of_tabs_to_create)])
for idx, tab in enumerate(tab_list):
with tab:
st.header(f"This is tab {idx+1}")
components.iframe("https://commons.wikimedia.org/wiki/Special:Random/File", height=500)
```
```{stlite-python}
import streamlit as st
import streamlit.components.v1 as components
number_of_tabs_to_create = st.number_input("Enter the number of tabs you want to create", 2, 5, 3)
tab_list = st.tabs([f"Tab {i+1}" for i in range(number_of_tabs_to_create)])
for idx, tab in enumerate(tab_list):
with tab:
st.header(f"This is tab {idx+1}")
components.iframe("https://commons.wikimedia.org/wiki/Special:Random/File", height=500)
```
## Custom Styling of Tabs
Here is an example of how to change the tab formatting.
:::{.callout-warning}
This is not a supported part of Streamlit - the ways in which streamlit internally names these tabs may change over time, causing this code to no longer work.
:::
```{python}
#| eval: false
#|
# Credit to user 'Dallas on https://discuss.streamlit.io/t/customizing-the-appearance-of-tabs/48913
import streamlit as st
custom_css = """
<style>
.stTabs [data-baseweb="tab-list"] {
gap: 2px;
}
.stTabs [data-baseweb="tab"] {
height: 50px;
white-space: pre-wrap;
background-color: #32a852;
border-radius: 4px 4px 0px 0px;
gap: 1px;
padding-top: 10px;
padding-bottom: 10px;
}
.stTabs [aria-selected="true"] {
background-color: #912a90;
}
</style>
"""
st.markdown(custom_css, unsafe_allow_html=True)
tab_a, tab_b = st.tabs(["A Tab", "Another Tab"])
with tab_a:
st.text("This is some content within tab 1")
with tab_b:
st.text("This is some content within tab 2")
```
```{stlite-python}
# Credit to user 'Dallas on https://discuss.streamlit.io/t/customizing-the-appearance-of-tabs/48913
import streamlit as st
custom_css = """
<style>
.stTabs [data-baseweb="tab-list"] {
gap: 2px;
}
.stTabs [data-baseweb="tab"] {
height: 50px;
white-space: pre-wrap;
background-color: #32a852;
border-radius: 4px 4px 0px 0px;
gap: 1px;
padding-top: 10px;
padding-bottom: 10px;
}
.stTabs [aria-selected="true"] {
background-color: #912a90;
}
</style>
"""
st.markdown(custom_css, unsafe_allow_html=True)
tab_a, tab_b = st.tabs(["A Tab", "Another Tab"])
with tab_a:
st.text("This is some content within tab 1")
with tab_b:
st.text("This is some content within tab 2")
```