tspsram commited on
Commit
d74a773
1 Parent(s): 5651421

Update app.py

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