Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
# Function to demonstrate a for loop
|
5 |
+
def demonstrate_for_loop(n):
|
6 |
+
progress = 0
|
7 |
+
for_output = ""
|
8 |
+
for i in range(1, n + 1):
|
9 |
+
if i % 2 == 0:
|
10 |
+
color = "teal"
|
11 |
+
else:
|
12 |
+
color = "orange"
|
13 |
+
for_output += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
|
14 |
+
progress = i / n
|
15 |
+
yield (for_output, progress)
|
16 |
+
|
17 |
+
# Function to demonstrate a while loop
|
18 |
+
def demonstrate_while_loop(n):
|
19 |
+
progress = 0
|
20 |
+
while_output = ""
|
21 |
+
i = 1
|
22 |
+
while i <= n:
|
23 |
+
if i % 2 == 0:
|
24 |
+
color = "teal"
|
25 |
+
else:
|
26 |
+
color = "orange"
|
27 |
+
while_output += f'<span style="color:{color}; font-size:24px; font-weight:bold;">{i} </span>'
|
28 |
+
progress = i / n
|
29 |
+
yield (while_output, progress)
|
30 |
+
i += 1
|
31 |
+
|
32 |
+
# Define the interface
|
33 |
+
def loop_interface(n, loop_type):
|
34 |
+
if loop_type == "For Loop":
|
35 |
+
return demonstrate_for_loop(n)
|
36 |
+
else:
|
37 |
+
return demonstrate_while_loop(n)
|
38 |
+
|
39 |
+
# Gradio Interface
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("# Loop Demonstrator App")
|
42 |
+
|
43 |
+
n_input = gr.Number(label="Enter number:", value=1, precision=0)
|
44 |
+
loop_type_input = gr.Dropdown(choices=["For Loop", "While Loop"], label="Loop Type", value="For Loop")
|
45 |
+
|
46 |
+
result_box = gr.HTML()
|
47 |
+
progress_bar = gr.ProgressBar()
|
48 |
+
|
49 |
+
def run_loop(n, loop_type):
|
50 |
+
if loop_type == "For Loop":
|
51 |
+
generator = demonstrate_for_loop(n)
|
52 |
+
else:
|
53 |
+
generator = demonstrate_while_loop(n)
|
54 |
+
|
55 |
+
for output, progress in generator:
|
56 |
+
result_box.update(output)
|
57 |
+
progress_bar.update(progress)
|
58 |
+
time.sleep(0.5)
|
59 |
+
|
60 |
+
gr.Button("Run Loop").click(run_loop, inputs=[n_input, loop_type_input], outputs=[result_box, progress_bar])
|
61 |
+
|
62 |
+
# Launch the Gradio app
|
63 |
+
demo.launch()
|