georad commited on
Commit
607614b
·
verified ·
1 Parent(s): ac35dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -129
app.py CHANGED
@@ -1,153 +1,120 @@
1
  import streamlit as st
2
 
 
3
  import time
4
- from datetime import datetime
5
 
6
  # Set page config
7
- st.set_page_config(page_title="Custom Status Indicator", layout="wide")
8
-
9
- # Initialize session state for our custom status indicator
10
- if 'last_update' not in st.session_state:
11
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
12
- if 'is_running' not in st.session_state:
13
- st.session_state.is_running = False
14
-
15
- # Function to toggle the running state
16
- def toggle_running_state():
17
- st.session_state.is_running = not st.session_state.is_running
18
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
19
-
20
- # Function to simulate a computation/process
21
- def run_process():
22
- with st.spinner("Running process..."):
23
- # Simulate work
24
- time.sleep(3)
25
- st.success("Process completed!")
26
- st.session_state.is_running = False
27
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
28
-
29
- # Create a container for our fixed header
30
- header = st.container()
31
-
32
- # Add CSS to fix our custom status indicator at the top
33
- st.markdown("""
 
 
 
 
 
 
 
 
34
  <style>
35
- /* Fix the first container to serve as our header */
36
- .stApp > div:first-child {
37
- position: sticky !important;
38
- top: 0 !important;
39
- background-color: white !important;
40
- z-index: 999 !important;
41
- padding: 1rem !important;
42
- border-bottom: 1px solid #f0f0f0 !important;
43
- box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
 
 
 
 
 
44
  }
45
 
46
- /* Custom status indicator styles */
47
- .status-indicator {
48
- display: inline-block;
49
  padding: 5px 10px;
50
  border-radius: 4px;
51
- margin-right: 10px;
52
  }
 
53
  .status-running {
54
  background-color: #ffeb3b;
55
  color: #000;
56
  }
 
57
  .status-idle {
58
  background-color: #4caf50;
59
  color: white;
60
  }
 
 
 
 
 
61
  </style>
62
- """, unsafe_allow_html=True)
63
 
64
- # Create our custom status indicator in the header container
65
- with header:
66
- cols = st.columns([3, 1])
67
-
68
- with cols[0]:
69
- st.title("App with Custom Status Indicator")
70
-
71
- with cols[1]:
72
- status_class = "status-running" if st.session_state.is_running else "status-idle"
73
- status_text = "RUNNING" if st.session_state.is_running else "IDLE"
74
-
75
- st.markdown(f"""
76
- <div style="display: flex; align-items: center; justify-content: flex-end;">
77
- <div class="status-indicator {status_class}">{status_text}</div>
78
- <div>Last update: {st.session_state.last_update}</div>
79
- </div>
80
- """, unsafe_allow_html=True)
81
-
82
- if st.button("Run Process" if not st.session_state.is_running else "Stop",
83
- key="run_button",
84
- on_click=toggle_running_state):
85
- # If we're now in running state, execute the process
86
- if st.session_state.is_running:
87
- run_process()
88
-
89
- # Main content area
90
- st.write("Scroll down to test if the custom status indicator stays visible")
91
-
92
- # Add a separator
93
- st.markdown("---")
94
-
95
- # Create some tabs to organize content
96
- tab1, tab2, tab3 = st.tabs(["Tab 1", "Tab 2", "Tab 3"])
97
-
98
- with tab1:
99
- st.header("Tab 1 Content")
100
- st.write("This is the content of tab 1")
101
-
102
- # Add some sample content for scrolling
103
- for i in range(10):
104
- st.write(f"Content line {i} in tab 1")
105
-
106
- if st.button("Run Process in Tab 1"):
107
- with st.spinner("Running process in Tab 1..."):
108
- st.session_state.is_running = True
109
- time.sleep(2)
110
- st.session_state.is_running = False
111
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
112
- st.success("Process completed in Tab 1!")
113
-
114
- with tab2:
115
- st.header("Tab 2 Content")
116
- st.write("This is the content of tab 2")
117
-
118
- # Add some sample content for scrolling
119
- for i in range(10):
120
- st.write(f"Content line {i} in tab 2")
121
-
122
- if st.button("Run Process in Tab 2"):
123
- with st.spinner("Running process in Tab 2..."):
124
- st.session_state.is_running = True
125
- time.sleep(3)
126
- st.session_state.is_running = False
127
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
128
- st.success("Process completed in Tab 2!")
129
-
130
- with tab3:
131
- st.header("Tab 3 Content")
132
- st.write("This is the content of tab 3")
133
-
134
- # Add some sample content for scrolling
135
- for i in range(10):
136
- st.write(f"Content line {i} in tab 3")
137
-
138
- if st.button("Run Process in Tab 3"):
139
- with st.spinner("Running process in Tab 3..."):
140
- st.session_state.is_running = True
141
- time.sleep(4)
142
- st.session_state.is_running = False
143
- st.session_state.last_update = datetime.now().strftime("%H:%M:%S")
144
- st.success("Process completed in Tab 3!")
145
-
146
- # Add more content to ensure we have enough to scroll
147
- st.markdown("---")
148
- st.header("Additional Content")
149
- for i in range(10):
150
- st.write(f"Additional content line {i}")
151
 
152
  # --- SHARED ON ALL PAGES ---
153
  st.logo(image="images/menu_book_60dp_75FBFD.png")
 
1
  import streamlit as st
2
 
3
+
4
  import time
 
5
 
6
  # Set page config
7
+ st.set_page_config(page_title="Reliable Sticky Status", layout="wide")
8
+
9
+ # Create an empty container at the top that will serve as our sticky status container
10
+ sticky_container = st.empty()
11
+
12
+ # Use JavaScript to track the real status widget and mirror its state to our custom container
13
+ js_code = """
14
+ <script>
15
+ // Function to observe changes in the status widget
16
+ function observeStatusWidget() {
17
+ // Try to find the actual status widget
18
+ const statusWidget = document.querySelector('[data-testid="stStatusWidget"]');
19
+ const runningIndicator = document.querySelector('.stStatusWidget span:contains("Running")');
20
+ const stopButton = document.querySelector('button[kind="formSubmit"]');
21
+
22
+ // Update our custom status mirror
23
+ const customStatus = document.getElementById('custom-status-mirror');
24
+ if (customStatus) {
25
+ if (runningIndicator) {
26
+ customStatus.className = 'status-running';
27
+ customStatus.textContent = 'RUNNING';
28
+ } else {
29
+ customStatus.className = 'status-idle';
30
+ customStatus.textContent = 'IDLE';
31
+ }
32
+ }
33
+
34
+ // Continue checking
35
+ setTimeout(observeStatusWidget, 500);
36
+ }
37
+
38
+ // Start observing after a delay to ensure DOM is loaded
39
+ setTimeout(observeStatusWidget, 1000);
40
+ </script>
41
+
42
  <style>
43
+ /* Position the sticky container */
44
+ #sticky-status-container {
45
+ position: fixed;
46
+ top: 0;
47
+ left: 0;
48
+ right: 0;
49
+ background-color: white;
50
+ padding: 10px;
51
+ z-index: 9999;
52
+ border-bottom: 1px solid #ddd;
53
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
54
+ display: flex;
55
+ justify-content: space-between;
56
+ align-items: center;
57
  }
58
 
59
+ /* Style the status indicators */
60
+ #custom-status-mirror {
 
61
  padding: 5px 10px;
62
  border-radius: 4px;
63
+ font-weight: bold;
64
  }
65
+
66
  .status-running {
67
  background-color: #ffeb3b;
68
  color: #000;
69
  }
70
+
71
  .status-idle {
72
  background-color: #4caf50;
73
  color: white;
74
  }
75
+
76
+ /* Add padding to prevent content from being hidden */
77
+ body {
78
+ padding-top: 60px;
79
+ }
80
  </style>
 
81
 
82
+ <div id="sticky-status-container">
83
+ <h3>My Application</h3>
84
+ <div class="status-container">
85
+ <span id="custom-status-mirror" class="status-idle">IDLE</span>
86
+ </div>
87
+ </div>
88
+ """
89
+
90
+ # Add our JavaScript and custom sticky header
91
+ st.components.v1.html(js_code, height=50)
92
+
93
+ # Create a function to simulate a long-running process
94
+ def run_long_process():
95
+ progress_bar = st.progress(0)
96
+ for i in range(100):
97
+ time.sleep(0.05) # Simulate work being done
98
+ progress_bar.progress(i + 1)
99
+ st.success("Process completed!")
100
+
101
+ # Add a spacer to prevent content overlap with our sticky header
102
+ st.markdown("<div style='height: 20px'></div>", unsafe_allow_html=True)
103
+
104
+ # Main content of the application
105
+ st.title("Application with Status Mirror")
106
+ st.write("This app demonstrates a custom sticky status indicator")
107
+
108
+ # Add a button to trigger a process
109
+ if st.button("Run Process"):
110
+ run_long_process()
111
+
112
+ # Add content to demonstrate scrolling
113
+ st.markdown("## Sample Content")
114
+ for i in range(30):
115
+ st.write(f"Content line {i}")
116
+ if i % 10 == 0:
117
+ st.markdown(f"### Section {i//10 + 1}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  # --- SHARED ON ALL PAGES ---
120
  st.logo(image="images/menu_book_60dp_75FBFD.png")