Rsnarsna commited on
Commit
2f48198
·
verified ·
1 Parent(s): 143934c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -28
app.py CHANGED
@@ -300,19 +300,19 @@ def read_email():
300
  print(f"Error reading emails: {e}")
301
 
302
 
303
- # Background loop for continuous email extraction
304
- def email_processing_loop(a):
305
- print('looping')
306
- # global running
307
- while a:
308
  try:
309
- read_email()
310
  print("Emails processed")
311
  except Exception as e:
312
  print(f"Error in email processing: {e}")
313
- time.sleep(10) # Check emails every minute
314
 
315
- # HTML content embedded directly in app.py using Jinja2-style templating
316
  html_content = """
317
  <!DOCTYPE html>
318
  <html lang="en">
@@ -336,8 +336,6 @@ html_content = """
336
  </head>
337
  <body>
338
  <h1>Email Processing Status: {{ status }}</h1>
339
-
340
- <!-- Display status -->
341
  <button onclick="window.location.href='/start'">Start Processing</button>
342
  <button class="stop" onclick="window.location.href='/stop'">Stop Processing</button>
343
  </body>
@@ -347,40 +345,35 @@ html_content = """
347
  # Endpoint to display the home page with the email processor's status
348
  @app.get("/", response_class=HTMLResponse)
349
  async def home():
350
- logging.info('home')
351
- print('home')
352
-
353
- status = " Ready to start ! " #"Running" if running else "Stopped"
354
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
355
 
356
  # Endpoint to start the email processing loop
357
  @app.get("/start", response_class=HTMLResponse)
358
  async def start_processing():
359
- print('start process!...')
360
- logging.info('start')
361
-
362
- # global running
363
- # if not running:
364
- # running = True
365
- # # threading.Thread(target=email_processing_loop, daemon=True).start()
366
-
367
  status = "Running"
368
- email_processing_loop("True")
369
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
370
 
371
  # Endpoint to stop the email processing loop
372
  @app.get("/stop", response_class=HTMLResponse)
373
  async def stop_processing():
374
- print('stop process!...')
375
- # global running
376
- # running = False
377
-
378
- email_processing_loop("False")
379
  status = "Stopped"
380
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
381
 
 
382
  # Start the email processing loop when the app runs
383
  if __name__ == "__main__":
 
 
384
  print('starting')
385
  logging.info('starting project!...')
386
  # running = True
 
300
  print(f"Error reading emails: {e}")
301
 
302
 
303
+ # Function for continuous email extraction
304
+ def email_processing_loop():
305
+ global running
306
+ print('Starting email processing loop...')
307
+ while running:
308
  try:
309
+ read_email() # Your email extraction logic here
310
  print("Emails processed")
311
  except Exception as e:
312
  print(f"Error in email processing: {e}")
313
+ time.sleep(10) # Check for emails every 10 seconds
314
 
315
+ # HTML content with Jinja2-style templating
316
  html_content = """
317
  <!DOCTYPE html>
318
  <html lang="en">
 
336
  </head>
337
  <body>
338
  <h1>Email Processing Status: {{ status }}</h1>
 
 
339
  <button onclick="window.location.href='/start'">Start Processing</button>
340
  <button class="stop" onclick="window.location.href='/stop'">Stop Processing</button>
341
  </body>
 
345
  # Endpoint to display the home page with the email processor's status
346
  @app.get("/", response_class=HTMLResponse)
347
  async def home():
348
+ global running
349
+ status = "Running" if running else "Stopped"
 
 
350
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
351
 
352
  # Endpoint to start the email processing loop
353
  @app.get("/start", response_class=HTMLResponse)
354
  async def start_processing():
355
+ global running, loop_thread
356
+ if not running:
357
+ running = True
358
+ loop_thread = threading.Thread(target=email_processing_loop, daemon=True)
359
+ loop_thread.start()
 
 
 
360
  status = "Running"
 
361
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
362
 
363
  # Endpoint to stop the email processing loop
364
  @app.get("/stop", response_class=HTMLResponse)
365
  async def stop_processing():
366
+ global running
367
+ if running:
368
+ running = False
 
 
369
  status = "Stopped"
370
  return HTMLResponse(content=html_content.replace("{{ status }}", status), status_code=200)
371
 
372
+
373
  # Start the email processing loop when the app runs
374
  if __name__ == "__main__":
375
+ running = False
376
+ loop_thread = None
377
  print('starting')
378
  logging.info('starting project!...')
379
  # running = True