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