georad commited on
Commit
e9542e8
Β·
verified Β·
1 Parent(s): 3a40adc

Update pages/type_text.py

Browse files
Files changed (1) hide show
  1. pages/type_text.py +125 -81
pages/type_text.py CHANGED
@@ -10,97 +10,141 @@ import os
10
  os.getenv("HF_TOKEN")
11
 
12
 
13
- # 0. If stStatusWidget is a custom component, make sure it's defined or imported.
14
- # For demonstration, let's create a placeholder function for stStatusWidget.
15
- # Replace this with your actual stStatusWidget or the widgets you want to use.
16
- def stStatusWidget():
17
- col1, col2, col3 = st.columns(3)
18
- with col1:
19
- st.metric("Temperature", "25 Β°C", "1.2 Β°C")
20
- with col2:
21
- st.metric("Humidity", "60%", "-5%")
22
- with col3:
23
- st.info("System Status: OK")
24
-
25
- # 1. Define the CSS for the sticky header
26
- # Note: The background color is important so content doesn't show through.
27
- # You might want to adjust it to match your Streamlit theme.
28
- # Streamlit's default light theme background is #f0f2f6 or white.
29
- # Streamlit's default dark theme background is #0e1117.
30
- # Using a generic white/light gray is often a safe bet.
31
- sticky_header_css = """
32
- <style>
33
- div[data-testid="stVerticalBlock"] div:has(div.sticky-header) {
34
- position: sticky;
35
- top: 2.875rem; /* Adjust this value if you have a page icon or title taking up space */
36
- background-color: white;
37
- z-index: 999;
38
- box-shadow: 0 2px 4px 0 rgba(0,0,0,0.1); /* Optional: for a slight shadow */
39
- padding-bottom: 0.5rem; /* Optional: for some spacing */
40
- }
41
-
42
- .sticky-header {
43
- /* You can add additional styling for the header itself if needed */
44
- /* border-bottom: 1px solid #e6e6e6; */ /* Optional: a bottom border */
45
- }
46
-
47
- /* Nudge the actual page content down a bit if the sticky header is tall */
48
- /* This is optional and depends on your header's height and content */
49
- /*
50
- div[data-testid="stAppViewContainer"] > section:first-of-type {
51
- padding-top: 5rem; /* Adjust this based on your header's height */
52
- }
53
- */
54
-
55
- /*
56
- Alternative for top positioning if you want it absolutely at the top,
57
- but this might conflict with Streamlit's own header bar if you're using st.set_page_config with a title/icon.
58
- If you're not, or want to cover it, top: 0; might be better.
59
- */
60
- /*
61
- div[data-testid="stVerticalBlock"] div:has(div.sticky-header) {
62
- position: sticky;
63
- top: 0; // Use this if you have no page title/icon in Streamlit's native header bar
64
- background-color: white;
65
- z-index: 999;
66
- }
67
- */
68
- </style>
 
 
 
69
  """
70
- st.markdown(sticky_header_css, unsafe_allow_html=True)
71
 
72
- # 2. Create the header content within a div that has the 'sticky-header' class.
73
- # We use st.markdown to open the div, then Streamlit widgets, then st.markdown to close.
 
74
 
75
- st.markdown('<div class="sticky-header">', unsafe_allow_html=True)
 
 
76
 
77
- # --- Your Header Content ---
78
- # st.title("My Application Dashboard") # You can have a title
79
- # st.markdown("---") # Optional separator
 
 
 
 
 
80
 
81
- # Example using columns for layout
82
- header_cols = st.columns([3, 2]) # Adjust ratios as needed
83
- with header_cols[0]:
84
- st.subheader("Dashboard Overview πŸ“Š")
85
- # You could put a logo here: st.image("logo.png", width=100)
86
- with header_cols[1]:
87
- # Call your stStatusWidget or place its constituent widgets here
88
- st.status(label="StatusIcon") # Our placeholder status widget
89
 
90
- st.markdown('</div>', unsafe_allow_html=True)
 
 
 
 
91
 
92
- # 3. Add the rest of your page content to demonstrate scrolling
93
- st.markdown("## Main Content Area")
94
- st.write("Scroll down to see the header stick.")
95
 
96
- for i in range(50):
97
- st.write(f"Dummy content line {i+1} to make the page scrollable.")
98
- if (i+1) % 10 == 0:
99
- st.image("https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.png", width=300)
 
100
 
101
- st.markdown("---")
102
- st.write("End of content.")
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
 
106
 
 
10
  os.getenv("HF_TOKEN")
11
 
12
 
13
+ # --- PAGE CONFIG (Optional: Must be the first Streamlit command) ---
14
+ # st.set_page_config(layout="wide") # Example: Use wide mode
15
+
16
+ # --- NAVIGATION LOGIC ---
17
+ # Initialize session state for page navigation if it doesn't exist
18
+ if "page" not in st.session_state:
19
+ st.session_state.page = "Home"
20
+
21
+ # Function to change the current page
22
+ def set_page(page_name):
23
+ st.session_state.page = page_name
24
+
25
+ # --- CSS FOR STICKY NAVBAR ---
26
+ # You might need to adjust 'top' if Streamlit's default header is visible
27
+ # and you want your navbar to be below it. Streamlit's header is approx 3rem-3.5rem.
28
+ # If you've hidden Streamlit's default header, 'top: 0;' should work.
29
+ # Let's assume Streamlit's default header IS visible for this example.
30
+ STREAMLIT_DEFAULT_HEADER_HEIGHT = "3.5rem" # Approximate
31
+
32
+ # Define the CSS for the sticky navbar
33
+ # We will wrap our Streamlit columns/buttons in a div with class "sticky-navbar-container"
34
+ navbar_style = f"""
35
+ <style>
36
+ .sticky-navbar-container {{
37
+ position: sticky;
38
+ top: {STREAMLIT_DEFAULT_HEADER_HEIGHT}; /* Stick below Streamlit's default header */
39
+ background-color: #FFFFFF; /* Or your app's background color e.g. #F0F2F6 */
40
+ z-index: 1001; /* High z-index to keep it on top */
41
+ padding: 0.75rem 1rem; /* Adjust padding as needed */
42
+ box-shadow: 0 2px 4px -1px rgba(0,0,0,0.1); /* Optional: subtle shadow */
43
+ border-bottom: 1px solid #E0E0E0; /* Optional: subtle border */
44
+ width: 100%; /* Ensure it spans the full width */
45
+ }}
46
+ /* Optional: Style for the buttons for a more 'navbar' look */
47
+ .sticky-navbar-container .stButton>button {{
48
+ border: none; /* Remove default button border */
49
+ padding: 0.5rem 1rem; /* Adjust button padding */
50
+ margin: 0 0.25rem; /* Spacing between buttons */
51
+ /* background-color: transparent; */ /* Make button background transparent */
52
+ /* color: #31333F; */ /* Set text color */
53
+ font-weight: 500;
54
+ }}
55
+ .sticky-navbar-container .stButton>button:hover {{
56
+ /* background-color: #E0E0E0; */ /* Hover effect */
57
+ /* color: #000000; */
58
+ }}
59
+ .sticky-navbar-container .stButton>button:focus {{
60
+ /* outline: 2px solid #007bff; */ /* Focus outline */
61
+ /* box-shadow: none; */
62
+ }}
63
+ /* Style for an active-like button (if you want to highlight current page) */
64
+ .sticky-navbar-container .stButton.active-button>button {{
65
+ /* background-color: #007bff; */
66
+ /* color: white; */
67
+ font-weight: bold;
68
+ border-bottom: 2px solid #007BFF; /* Example active indicator */
69
+ }}
70
+
71
+ </style>
72
  """
73
+ st.markdown(navbar_style, unsafe_allow_html=True)
74
 
75
+ # --- NAVBAR HTML STRUCTURE ---
76
+ # Start the div container for the navbar
77
+ st.markdown('<div class="sticky-navbar-container">', unsafe_allow_html=True)
78
 
79
+ # Create columns for navigation items
80
+ # Adjust the column ratios as needed, e.g., for a logo or app title
81
+ nav_cols = st.columns([1, 1, 1, 1, 3]) # 4 nav items, 1 larger space for title/logo
82
 
83
+ # Navigation buttons
84
+ with nav_cols[0]:
85
+ # Add 'active-button' class conditionally for styling
86
+ active_class = "active-button" if st.session_state.page == "Home" else ""
87
+ if st.button("🏠 Home", on_click=set_page, args=("Home",), use_container_width=True, key="nav_home", type="secondary"): # type can be "primary" or "secondary"
88
+ pass # on_click handles the logic
89
+ # Applying class via JS hack (if needed, st.button doesn't directly support class attribute)
90
+ if active_class: st.markdown(f"<script>document.querySelector('[data-testid=\"stButton\"] button[key*=\"nav_home\"]').closest('.stButton').classList.add('{active_class}');</script>", unsafe_allow_html=True)
91
 
 
 
 
 
 
 
 
 
92
 
93
+ with nav_cols[1]:
94
+ active_class = "active-button" if st.session_state.page == "Dashboard" else ""
95
+ if st.button("πŸ“ˆ Dashboard", on_click=set_page, args=("Dashboard",), use_container_width=True, key="nav_dashboard"):
96
+ pass
97
+ if active_class: st.markdown(f"<script>document.querySelector('[data-testid=\"stButton\"] button[key*=\"nav_dashboard\"]').closest('.stButton').classList.add('{active_class}');</script>", unsafe_allow_html=True)
98
 
 
 
 
99
 
100
+ with nav_cols[2]:
101
+ active_class = "active-button" if st.session_state.page == "Settings" else ""
102
+ if st.button("βš™οΈ Settings", on_click=set_page, args=("Settings",), use_container_width=True, key="nav_settings"):
103
+ pass
104
+ if active_class: st.markdown(f"<script>document.querySelector('[data-testid=\"stButton\"] button[key*=\"nav_settings\"]').closest('.stButton').classList.add('{active_class}');</script>", unsafe_allow_html=True)
105
 
 
 
106
 
107
+ with nav_cols[3]:
108
+ active_class = "active-button" if st.session_state.page == "About" else ""
109
+ if st.button("πŸ‘€ About", on_click=set_page, args=("About",), use_container_width=True, key="nav_about"):
110
+ pass
111
+ if active_class: st.markdown(f"<script>document.querySelector('[data-testid=\"stButton\"] button[key*=\"nav_about\"]').closest('.stButton').classList.add('{active_class}');</script>", unsafe_allow_html=True)
112
+
113
+
114
+ # Optional: App title or logo in the navbar
115
+ with nav_cols[4]:
116
+ st.markdown("<h3 style='text-align:right; margin:0; padding-top:0.1rem;'>My Sticky App</h3>", unsafe_allow_html=True)
117
+
118
+ # Close the div container for the navbar
119
+ st.markdown('</div>', unsafe_allow_html=True)
120
+
121
+
122
+ # --- MAIN PAGE CONTENT ---
123
+ # Display content based on the selected page
124
+ if st.session_state.page == "Home":
125
+ st.header("🏠 Home Page")
126
+ st.write("Welcome to the home page! This navbar will stick to the top as you scroll.")
127
+ for i in range(50):
128
+ st.write(f"Home: Scrollable content line {i+1}")
129
+ elif st.session_state.page == "Dashboard":
130
+ st.header("πŸ“ˆ Dashboard")
131
+ st.write("This is your main dashboard. Notice the sticky navigation.")
132
+ for i in range(50):
133
+ st.write(f"Dashboard: Scrollable content line {i+1}")
134
+ elif st.session_state.page == "Settings":
135
+ st.header("βš™οΈ Settings")
136
+ st.write("Configure your application settings here.")
137
+ for i in range(50):
138
+ st.write(f"Settings: Scrollable content line {i+1}")
139
+ elif st.session_state.page == "About":
140
+ st.header("πŸ‘€ About Us")
141
+ st.write("Information about this application and its sticky navigation feature.")
142
+ for i in range(50):
143
+ st.write(f"About: Scrollable content line {i+1}")
144
+
145
+ # --- FOOTER (Example) ---
146
+ st.markdown("---")
147
+ st.write("This is the footer of the page.")
148
 
149
 
150