-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlayout_sidebar.qmd
More file actions
263 lines (179 loc) · 7.11 KB
/
layout_sidebar.qmd
File metadata and controls
263 lines (179 loc) · 7.11 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
---
title: "Sidebar"
filters:
- whitphx/stlite
---
## Collapsible Sidebars
The Streamlit sidebar gives us a distinct area on the left-hand side of the screen to use.
```{python}
#| eval: false
import streamlit as st
with st.sidebar: # <1>
st.header("I'm a sidebar") # <2>
st.write("We can use inputs our sidebar too.")
name = st.text_input("What's your name?", value=None)
st.title("Greeting App!")
if name is not None: # <3>
st.write(f"Nice to meet you, {name}!")
else:
st.write("I can't greet you until you enter your name!")
```
1. We can use the `with` notation along with `st.sidebar` to create the sidebar
2. We indent the code we want to exist within the sidebar
3. Once we write a line of code that is not indented, this signals the beginning of code that will just appear in the main area of the streamlit app.
```{stlite-python}
import streamlit as st
with st.sidebar:
st.header("I'm a sidebar")
st.write("We can use inputs our sidebar too.")
name = st.text_input("What's your name?", value=None)
st.title("Greeting App!")
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!")
# This is just here to ensure the resulting embedded app is tall enough to display the sidebar natively
st.container(height=200)
```
### Collapsing, expanding and resizing the sidebar
When hovering over the sidebar, we can see this arrow appear.

Clicking on this collapses the sidebar, making the main body of the app take up the full width.

This can be very handy - but be aware that it may make for a non-intuitive experience for your end users.
Hovering over the point where the sidebar ends and the main part of the app begins, our cursor will change to indicate that the sidebar can be resized. Clicking and dragging will allow us to make the sidebar narrower and wider, within some predefined limits.


## Alternative sidebar syntax
Like a lot of other layout elements, such as columns and tabs, there are multiple ways to refer to the sidebar, and which is best may depend on precisely what you are trying to do with your app.
1. Use the `with` notation, as above.
```{python}
#| eval: false
import streamlit as st
with st.sidebar: # <1>
st.header("I'm a sidebar") # <2>
st.write("We can use inputs our sidebar too.")
name = st.text_input("What's your name?", value=None)
st.title("Greeting App!")
if name is not None: # <3>
st.write(f"Nice to meet you, {name}!")
else:
st.write("I can't greet you until you enter your name!")
```
2. Wherever you would use a component that begins with `st.` (e.g. `st.text()`, `st.number_input()`), replace this with `st.sidebar()` (e.g. `st.sidebar.text()`, `st.sidebar.number_input()`)
The code below is completely equivalent to the code above.
```{python}
#| eval: false
import streamlit as st
st.sidebar.header("I'm a sidebar")
st.sidebar.write("We can use inputs our sidebar too.")
name = st.sidebar.text_input("What's your name?", value=None)
st.title("Greeting App!")
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!")
```
:::{.callout-note}
You can often mix and match the approaches within an app too - though picking one approach may be easier for the next person who interacts with your code to follow. If it's a good way of achieving what you need to, though, then you can go ahead and do this!
:::
## Initial sidebar state
While there are not many ways to customise your sidebar from within streamlit, you can adjust whether it displays as being visible or not by default using `st.set_page_config()`.
:::{.callout-tip}
`st.set_page_config()` has to be the first streamlit command you run after importing streamlit.
You can run other general python commands between the import and setting the page config - but you cannot, for example, use `st.header()` before calling `st.set_page_config()`.
:::
```{python}
#| eval: false
import streamlit as st
st.set_page_config(initial_sidebar_state='collapsed')
with st.sidebar:
st.header("I'm a sidebar")
st.title("Collapsed Sidebar Demo!")
st.write("The sidebar in this app is closed by default. Click on the arrow in the top left of the screen to open it.")
```
```{stlite-python}
import streamlit as st
st.set_page_config(initial_sidebar_state='collapsed')
with st.sidebar:
st.header("I'm a sidebar")
st.title("Collapsed Sidebar Demo!")
st.write("The sidebar in this app is closed by default. Click on the arrow in the top left of the screen to open it.")
```
## Multipage app navigation
Later in the book we discuss multipage apps, which use the sidebar by default for page navigation.
Any things you add to your app's sidebar will just appear below the list of pages.
## Sidebar styling
### Sidebar Colour
The sidebar colour can be updated using Streamlit's [theming](https://docs.streamlit.io/develop/concepts/configuration/theming) feature.
The colour that needs changing is the `secondaryBackgroundColor` in the `config.toml` file.
More detail about theming with `config.toml` can be found in a [later chapter](app_colours.qmd).
### Making the sidebar expander more obvious
With custom CSS, we can make the sidebar expander button more obvious.
```{python}
#| eval: false
import streamlit as st
st.markdown(
"""
<style>
/* Expander */
div[data-testid=stExpander] > details > summary > span > div > p
{
font-size: 30px;
}
</style>
""",
unsafe_allow_html=True
)
with st.sidebar:
st.header("I'm a sidebar")
st.title("Sidebar Demo!")
st.write("The button to expand or close the sidebar is bigger in this example")
```
```{stlite-python}
import streamlit as st
st.markdown(
"""
<style>
/* Expander */
div[data-testid=stExpander] > details > summary > span > div > p
{
font-size: 30px;
}
</style>
""",
unsafe_allow_html=True
)
with st.sidebar:
st.header("I'm a sidebar")
st.title("Sidebar Demo!")
st.write("The button to expand or close the sidebar is bigger in this example")
```
### Sidebar Font colour and size
If using multipage apps, you may need some futher customisations to make the sidebar look good with the colour changes you make. However, more advanced customisations require using CSS.
Here is an example where we change the default font colour of the auto-generated navigation items.
:::{.callout-warning}
As the streamlit library evolves, the names of various elements on the page may change, meaning that these may not remain consistent over time.
:::
```{python}
#| eval: false
# Credit to Amy H in the PenCHORD team for this!
# https://github.com/kailo-beewell/kailo_beewell_dashboard_package/blob/be249c515d5cfd7d168abf14f03927322b72322b/kailo_beewell_dashboard/css/style.css#L2
st.markdown(
"""
<style>
/* Sidebar font color as default is to set non-selected to more transparent */
[data-testid=stSidebarNavItems] > li > div > a > span
{
color: #05291F;
}
/* Sidebar font size */
[data-testid=stSidebarNavItems]
{
font-size: 25px;
}
</style>
""",
unsafe_allow_html=True
)
```