|
import streamlit as st |
|
import time |
|
|
|
|
|
def update_progress_bar_color(progress): |
|
if progress < 0.3: |
|
return '#800080' |
|
elif progress < 0.7: |
|
return '#00ffff' |
|
else: |
|
return '#00ff00' |
|
|
|
|
|
def demonstrate_for_loop(n): |
|
progress_bar = st.progress(0) |
|
st.markdown('<h3 style="color:#1E90FF;">For Loop Executing...</h3>', unsafe_allow_html=True) |
|
|
|
result = "" |
|
for i in range(1, n + 1): |
|
color = "teal" if i % 2 == 0 else "orange" |
|
result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>' |
|
|
|
|
|
progress = i / n |
|
progress_bar.progress(progress) |
|
|
|
|
|
st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">' |
|
f'<strong>For Loop result:</strong> {result}</div>', unsafe_allow_html=True) |
|
|
|
time.sleep(0.5) |
|
|
|
|
|
def demonstrate_while_loop(n): |
|
progress_bar = st.progress(0) |
|
st.markdown('<h3 style="color:#1E90FF;">While Loop Executing...</h3>', unsafe_allow_html=True) |
|
|
|
result = "" |
|
i = 1 |
|
while i <= n: |
|
color = "teal" if i % 2 == 0 else "orange" |
|
result += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>' |
|
|
|
|
|
progress = i / n |
|
progress_bar.progress(progress) |
|
|
|
|
|
st.markdown(f'<div style="background-color:#fffacd; padding:20px; border-radius:10px; box-shadow: 0px 4px 12px rgba(0,0,0,0.1);">' |
|
f'<strong>While Loop result:</strong> {result}</div>', unsafe_allow_html=True) |
|
|
|
time.sleep(0.5) |
|
i += 1 |
|
|
|
|
|
st.title("Loop Demonstrator App") |
|
st.markdown("This app demonstrates a for loop and a while loop with colorful outputs.") |
|
|
|
|
|
n = st.number_input("Enter a number:", min_value=1, value=1) |
|
loop_type = st.selectbox("Select Loop Type:", ["For Loop", "While Loop"]) |
|
|
|
|
|
if st.button('Run Loop'): |
|
if loop_type == 'For Loop': |
|
demonstrate_for_loop(n) |
|
else: |
|
demonstrate_while_loop(n) |
|
|