Skip to content

Commit 774f6da

Browse files
st.space embedded apps (#1363)
* Component cleanup example * st.space
1 parent 671f55f commit 774f6da

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import streamlit as st
2+
3+
JS = """
4+
export default function(component) {
5+
const { setStateValue, parentElement } = component;
6+
const sidebar = document.querySelector('section.stSidebar');
7+
const initialState = sidebar.getAttribute('aria-expanded') === 'true';
8+
9+
// Create observer to watch for aria-expanded attribute changes
10+
const observer = new MutationObserver((mutations) => {
11+
mutations.forEach((mutation) => {
12+
if (mutation.type === 'attributes' && mutation.attributeName === 'aria-expanded') {
13+
const newIsExpanded = sidebar.getAttribute('aria-expanded') === 'true';
14+
setStateValue('expanded', newIsExpanded);
15+
}
16+
});
17+
});
18+
19+
// Start observing
20+
observer.observe(sidebar, {
21+
attributes: true,
22+
attributeFilter: ['aria-expanded']
23+
});
24+
25+
// Set initial state
26+
setStateValue('expanded', initialState);
27+
28+
// Cleanup function to remove the observer
29+
return () => {
30+
observer.disconnect();
31+
};
32+
33+
};
34+
"""
35+
36+
my_component = st.components.v2.component(
37+
"sidebar_expansion_detector",
38+
js=JS,
39+
)
40+
41+
st.sidebar.write("Sidebar content")
42+
result = my_component(on_expanded_change=lambda: None)
43+
result
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import streamlit as st
2+
3+
with st.container(horizontal=True):
4+
st.button("Left")
5+
st.space("stretch")
6+
st.button("Right")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import streamlit as st
2+
3+
left, middle, right = st.columns(3)
4+
5+
left.space("medium")
6+
left.button("Left button", width="stretch")
7+
8+
middle.space("small")
9+
middle.text_input("Middle input")
10+
11+
right.audio_input("Right uploader")

0 commit comments

Comments
 (0)