khurrameycon commited on
Commit
91db2c5
·
verified ·
1 Parent(s): 62bbc3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -21
app.py CHANGED
@@ -1,49 +1,70 @@
1
- # main.py
2
  import gradio as gr
3
  import threading
 
4
  from app_logic import run_my_application
5
 
6
  # Global variables for tracking status
7
  status_message = "Idle"
8
  is_running = False
9
 
10
- def execute_application():
 
 
 
11
  global status_message, is_running
12
  is_running = True
13
- status_message = "Python application is running..."
14
-
 
15
  try:
16
  # Call the function from app_logic.py
17
  result = run_my_application()
18
- status_message = result
 
 
 
 
 
 
 
 
 
19
  except Exception as e:
 
20
  status_message = f"An error occurred: {str(e)}"
 
21
  finally:
22
  is_running = False
23
 
24
- def start_program():
 
 
 
25
  global is_running
26
  if not is_running:
27
- threading.Thread(target=execute_application).start()
28
  return "Program started. Check status below."
29
  else:
30
  return "Program is already running!"
31
 
32
- def get_status():
33
- # Return the current status of the program
34
- return status_message
35
-
36
  # Gradio interface
37
- with gr.Blocks() as app:
38
- gr.Markdown("# Python Program Runner")
39
- gr.Markdown("Click the button to start your Python program. Status will update below.")
 
 
 
 
 
 
 
 
 
 
40
 
41
- start_button = gr.Button("Start Program")
42
- status_output = gr.Textbox(label="Status", value="Idle", interactive=False)
43
 
44
- # Event bindings
45
- start_button.click(start_program, outputs=status_output)
46
- status_update = gr.Textbox(label="Current Status", interactive=False)
47
- start_button.click(get_status, outputs=status_update)
48
 
49
- app.launch()
 
 
1
  import gradio as gr
2
  import threading
3
+ import time
4
  from app_logic import run_my_application
5
 
6
  # Global variables for tracking status
7
  status_message = "Idle"
8
  is_running = False
9
 
10
+ def execute_application(status_update):
11
+ """
12
+ Execute the application logic while updating the status dynamically.
13
+ """
14
  global status_message, is_running
15
  is_running = True
16
+ status_message = "Python application initiated..."
17
+ status_update(status_message) # Update status
18
+
19
  try:
20
  # Call the function from app_logic.py
21
  result = run_my_application()
22
+
23
+ for i in range(5): # Simulate progress updates
24
+ time.sleep(1) # Replace this with actual task updates if available
25
+ status_message = f"Processing step {i + 1}/5..."
26
+ status_update(status_message)
27
+
28
+ # When the program completes, set the final status
29
+ status_message = f"Process completed! Result: {result}"
30
+ status_update(status_message)
31
+
32
  except Exception as e:
33
+ # Handle exceptions and update status
34
  status_message = f"An error occurred: {str(e)}"
35
+ status_update(status_message)
36
  finally:
37
  is_running = False
38
 
39
+ def start_program(status_update):
40
+ """
41
+ Start the program in a new thread.
42
+ """
43
  global is_running
44
  if not is_running:
45
+ threading.Thread(target=execute_application, args=(status_update,)).start()
46
  return "Program started. Check status below."
47
  else:
48
  return "Program is already running!"
49
 
 
 
 
 
50
  # Gradio interface
51
+ def create_interface():
52
+ """
53
+ Create a Gradio interface with real-time status updates.
54
+ """
55
+ with gr.Blocks() as app:
56
+ gr.Markdown("# Python Program Runner")
57
+ gr.Markdown("Click the button to start your Python program. Status will update below.")
58
+
59
+ start_button = gr.Button("Start Program")
60
+ status_output = gr.Textbox(label="Status", value="Idle", interactive=False)
61
+
62
+ # Set up dynamic updates
63
+ start_button.click(fn=start_program, inputs=status_output, outputs=status_output, queue=True)
64
 
65
+ return app
 
66
 
67
+ # Launch the application
68
+ if __name__ == "__main__":
69
+ create_interface().launch()
 
70