-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlayout_expander.qmd
More file actions
235 lines (168 loc) · 8.57 KB
/
layout_expander.qmd
File metadata and controls
235 lines (168 loc) · 8.57 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
---
title: "Expanders"
filters:
- whitphx/stlite
---
The expander allows additional information to be hidden away until the user clicks in the relevant spot.
This can be a handy way to keep an app clear and easy-to-use while allowing users the flexibility to dive deeper into the data or find out additional information about the processes.
```{python}
#| eval: false
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
penguins = load_penguins()
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
with st.expander("Click here to view the underlying data"): # <1>
st.dataframe(penguins[['sex', 'species', 'island', 'bill_length_mm', 'bill_depth_mm']]) # <2>
st.write("This is some text that appears after the expander.") # <3>
```
1. We create an expander object, passing in the text that we want to appear to the user when the expander is collapsed.
2. We indent anything we want to appear within that particular expander.
3. Returning to the previous level of indentation will allow you to continue adding things to your page after the expander.
```{stlite-python}
import micropip
await micropip.install("setuptools")
await micropip.install("palmerpenguins")
await micropip.install("plotly")
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
penguins = load_penguins()
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
with st.expander("Click here to view the underlying data"):
st.dataframe(penguins[['sex', 'species', 'island', 'bill_length_mm', 'bill_depth_mm']])
st.write("This is some text that appears after the expander.") # <3>
```
As with many other layout elements, you can write expanders in a different way to achieve the same result.
```{python}
#| eval: false
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
penguins = load_penguins()
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
extra_data_expander = st.expander("Click here to view the underlying data") # <1>
extra_data_expander.dataframe(penguins[['sex', 'species', 'island', 'bill_length_mm', 'bill_depth_mm']]) # <2>
st.write("This is some text that appears after the expander.") # <3>
extra_data_expander.write("This is some additional text I'm later adding in to the expander") # <4>
```
1. Here, instead of using the 'with' notation, we have saved the output of our `st.expander` to a variable.
2. Then, instead of using `st.dataframe`, we replace `st.` with the name of the variable we just created
3. When we want to return to adding something to the main body of the app, we revert to using `st.`
4. One benefit of using this notation instead of the 'with' notation is that we can later add additional things into the expander, even if we have done other actions first.
```{stlite-python}
import micropip
await micropip.install("setuptools")
await micropip.install("palmerpenguins")
await micropip.install("plotly")
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
penguins = load_penguins()
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
extra_data_expander = st.expander("Click here to view the underlying data")
extra_data_expander.dataframe(penguins[['sex', 'species', 'island', 'bill_length_mm', 'bill_depth_mm']])
st.write("This is some text that appears after the expander.") # <3>
extra_data_expander.write("This is some additional text I'm later adding in to the expander") # <4>
```
## Nesting expanders in other layout elements
Expanders can sit within other layout elements.
```{python}
#| eval: false
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
tab1, tab2 = st.tabs(["Penguin Graphs", "Penguin Facts"])
penguins = load_penguins()
with tab1:
col1, col2 = st.columns(2)
with col1:
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
with st.expander("Click here to see the code for the graph"):
st.code(
"""
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
"""
)
with col2:
fig = px.scatter(penguins, x='flipper_length_mm', y='body_mass_g', color="species",
title=f"Penguins Dataset - Flipper Length (mm) vs Body Weight(g), coloured by Species")
st.plotly_chart(fig)
with st.expander("Click here to see the code for the graph"):
st.code(
"""
fig = px.scatter(penguins, x='flipper_length_mm', y='body_mass_g', color="species",
title=f"Penguins Dataset - Flipper Length (mm) vs Body Weight(g), coloured by Species")
"""
)
with st.expander("Click here to see the underlying data"):
st.dataframe(penguins)
with tab2:
st.header("Penguin Facts")
st.subheader("Gentoo Penguins")
st.write(
"""
The gentoo penguin (JEN-too) (Pygoscelis papua) is a penguin species (or possibly a species complex) in the genus Pygoscelis, most closely related to the Adélie penguin (P. adeliae) and the chinstrap penguin (P. antarcticus). The earliest scientific description was made in 1781 by Johann Reinhold Forster with a type locality in the Falkland Islands. The species calls in a variety of ways, but the most frequently heard is a loud trumpeting, which the bird emits with its head thrown back.
"""
)
expander_video = st.expander("Click here to watch a penguin video")
expander_video.video("https://www.youtube.com/watch?v=nFAK8Vj62WM")
```
```{stlite-python}
import micropip
await micropip.install("setuptools")
await micropip.install("palmerpenguins")
await micropip.install("plotly")
import streamlit as st
from palmerpenguins import load_penguins
import plotly.express as px
tab1, tab2 = st.tabs(["Penguin Graphs", "Penguin Facts"])
penguins = load_penguins()
with tab1:
col1, col2 = st.columns(2)
with col1:
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
st.plotly_chart(fig)
with st.expander("Click here to see the code for the graph"):
st.code(
"""
fig = px.scatter(penguins, x='bill_length_mm', y='bill_depth_mm', color="sex",
title=f"Penguins Dataset - Bill Length (mm) vs Bill Depth (mm), coloured by Sex")
"""
)
with col2:
fig = px.scatter(penguins, x='flipper_length_mm', y='body_mass_g', color="species",
title=f"Penguins Dataset - Flipper Length (mm) vs Body Weight(g), coloured by Species")
st.plotly_chart(fig)
with st.expander("Click here to see the code for the graph"):
st.code(
"""
fig = px.scatter(penguins, x='flipper_length_mm', y='body_mass_g', color="species",
title=f"Penguins Dataset - Flipper Length (mm) vs Body Weight(g), coloured by Species")
"""
)
with st.expander("Click here to see the underlying data"):
st.dataframe(penguins)
with tab2:
st.header("Penguin Facts")
st.subheader("Gentoo Penguins")
st.write(
"""
The gentoo penguin (JEN-too) (Pygoscelis papua) is a penguin species (or possibly a species complex) in the genus Pygoscelis, most closely related to the Adélie penguin (P. adeliae) and the chinstrap penguin (P. antarcticus). The earliest scientific description was made in 1781 by Johann Reinhold Forster with a type locality in the Falkland Islands. The species calls in a variety of ways, but the most frequently heard is a loud trumpeting, which the bird emits with its head thrown back.
"""
)
expander_video = st.expander("Click here to watch a penguin video")
expander_video.video("https://www.youtube.com/watch?v=nFAK8Vj62WM")
```