tspsram commited on
Commit
575af79
·
verified ·
1 Parent(s): 6701a56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+
4
+ # Function to update the progress bar color based on the progress
5
+ def update_progress_bar_color(progress):
6
+ if progress < 0.3:
7
+ return '#800080' # Purple
8
+ elif progress < 0.7:
9
+ return '#00ffff' # Cyan
10
+ else:
11
+ return '#00ff00' # Lime Green
12
+
13
+ # Function to demonstrate a for loop with colorful output
14
+ def demonstrate_for_loop(n):
15
+ result = ""
16
+ for i in range(1, n + 1):
17
+ color = "teal" if i % 2 == 0 else "orange"
18
+ result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
19
+
20
+ # Update the progress bar
21
+ progress = i / n
22
+ progress_color = update_progress_bar_color(progress)
23
+
24
+ # Display results
25
+ st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">'
26
+ f'<strong>For Loop result:</strong> {result}</div>', unsafe_allow_html=True)
27
+
28
+ # Progress bar
29
+ st.progress(progress)
30
+ st.markdown(f'<style>.stProgress {{"background-color": "{progress_color}";}}</style>', unsafe_allow_html=True)
31
+
32
+ time.sleep(0.5) # Simulate execution time
33
+
34
+ # Function to demonstrate a while loop with colorful output
35
+ def demonstrate_while_loop(n):
36
+ result = ""
37
+ i = 1
38
+ while i <= n:
39
+ color = "teal" if i % 2 == 0 else "orange"
40
+ result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
41
+
42
+ # Update the progress bar
43
+ progress = i / n
44
+ progress_color = update_progress_bar_color(progress)
45
+
46
+ # Display results
47
+ st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">'
48
+ f'<strong>While Loop result:</strong> {result}</div>', unsafe_allow_html=True)
49
+
50
+ # Progress bar
51
+ st.progress(progress)
52
+ st.markdown(f'<style>.stProgress {{"background-color": "{progress_color}";}}</style>', unsafe_allow_html=True)
53
+
54
+ time.sleep(0.5) # Simulate execution time
55
+ i += 1
56
+
57
+ # Streamlit app layout
58
+ st.title("Loop Demonstrator App")
59
+ st.markdown("This app demonstrates a for loop and a while loop with colorful outputs.")
60
+
61
+ # User inputs
62
+ n = st.number_input("Enter a number:", min_value=1, value=1)
63
+ loop_type = st.selectbox("Select Loop Type:", ["For Loop", "While Loop"])
64
+
65
+ # Button to trigger the loop demonstration
66
+ if st.button('Run Loop'):
67
+ if loop_type == 'For Loop':
68
+ demonstrate_for_loop(n)
69
+ else:
70
+ demonstrate_while_loop(n)