XPMaster commited on
Commit
6a2658f
·
1 Parent(s): 5220193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -25
app.py CHANGED
@@ -2,35 +2,26 @@ import streamlit as st
2
 
3
  st.title('Togglable Sidebar Example')
4
 
5
- # Apply CSS to hide the sidebar initially or based on session state
6
- if 'hide_sidebar' not in st.session_state:
7
- st.session_state.hide_sidebar = True
8
-
9
- hide_css = """
10
- <style>
11
- div[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
12
- display: none !important;
13
- }
14
- </style>
15
- """
16
-
17
- show_css = """
18
  <style>
19
- div[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
20
- display: block !important;
21
  }
22
  </style>
23
  """
24
 
25
- if st.session_state.hide_sidebar:
26
- st.markdown(hide_css, unsafe_allow_html=True)
27
- else:
28
- st.markdown(show_css, unsafe_allow_html=True)
29
 
30
- # Toggle sidebar visibility
31
- if st.button('Toggle Sidebar'):
32
- st.session_state.hide_sidebar = not st.session_state.hide_sidebar
33
 
34
- # Just add your sidebar items as usual, and they will be hidden/shown based on the toggle state
35
- st.sidebar.title('Sidebar')
36
- st.sidebar.write('This is the sidebar content.')
 
 
 
 
 
2
 
3
  st.title('Togglable Sidebar Example')
4
 
5
+ # This is the CSS to hide the sidebar
6
+ css_code = """
 
 
 
 
 
 
 
 
 
 
 
7
  <style>
8
+ [data-testid="stSidebar"] {
9
+ display: none;
10
  }
11
  </style>
12
  """
13
 
14
+ # Initialize the session state variable if it doesn't exist
15
+ if 'show_sidebar' not in st.session_state:
16
+ st.session_state.show_sidebar = False
 
17
 
18
+ if st.button("Toggle Sidebar"):
19
+ st.session_state.show_sidebar = not st.session_state.show_sidebar
 
20
 
21
+ # If we want to show the sidebar, we'll use Streamlit's standard sidebar functions.
22
+ # If we want to hide it, we'll inject the CSS code to hide the sidebar.
23
+ if st.session_state.show_sidebar:
24
+ st.sidebar.title("Sidebar")
25
+ st.sidebar.write("This is the sidebar content.")
26
+ else:
27
+ st.markdown(css_code, unsafe_allow_html=True)