Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,14 @@ import os
|
|
| 4 |
import logging
|
| 5 |
|
| 6 |
# Set up logging
|
| 7 |
-
logging.basicConfig(level=logging.
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Initialize the CSV file if it does not exist
|
| 13 |
if not os.path.exists(CSV_FILE):
|
|
@@ -21,8 +25,11 @@ if not os.path.exists(CSV_FILE):
|
|
| 21 |
# Function to handle registration
|
| 22 |
def register_patient(name, age, phone):
|
| 23 |
try:
|
|
|
|
|
|
|
| 24 |
# Load existing data
|
| 25 |
df = pd.read_csv(CSV_FILE)
|
|
|
|
| 26 |
|
| 27 |
# Generate new patient ID
|
| 28 |
new_id = len(df) + 1
|
|
@@ -34,16 +41,19 @@ def register_patient(name, age, phone):
|
|
| 34 |
"Age": [age],
|
| 35 |
"Phone Number": [phone]
|
| 36 |
})
|
|
|
|
|
|
|
| 37 |
df = pd.concat([df, new_data], ignore_index=True)
|
|
|
|
| 38 |
|
| 39 |
# Save updated data
|
| 40 |
df.to_csv(CSV_FILE, index=False)
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
return f"Patient {name} with ID {new_id} registered successfully!"
|
| 44 |
except Exception as e:
|
| 45 |
logging.error(f"An error occurred while registering patient: {e}")
|
| 46 |
-
return f"An error occurred: {e}"
|
| 47 |
|
| 48 |
# Define Gradio interface
|
| 49 |
with gr.Blocks() as demo:
|
|
@@ -54,8 +64,13 @@ with gr.Blocks() as demo:
|
|
| 54 |
phone_number = gr.Textbox(label="Phone Number", placeholder="Enter phone number")
|
| 55 |
register_button = gr.Button("Register")
|
| 56 |
status = gr.Textbox(label="Status", interactive=False)
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Launch the app
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import logging
|
| 5 |
|
| 6 |
# Set up logging
|
| 7 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 8 |
|
| 9 |
+
# Ensure the "files" directory exists
|
| 10 |
+
FILES_DIR = "files"
|
| 11 |
+
os.makedirs(FILES_DIR, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
# Path to CSV file in the "files" directory of your project
|
| 14 |
+
CSV_FILE = os.path.join(FILES_DIR, "patients.csv")
|
| 15 |
|
| 16 |
# Initialize the CSV file if it does not exist
|
| 17 |
if not os.path.exists(CSV_FILE):
|
|
|
|
| 25 |
# Function to handle registration
|
| 26 |
def register_patient(name, age, phone):
|
| 27 |
try:
|
| 28 |
+
logging.info(f"Starting registration for {name}, Age: {age}, Phone: {phone}")
|
| 29 |
+
|
| 30 |
# Load existing data
|
| 31 |
df = pd.read_csv(CSV_FILE)
|
| 32 |
+
logging.debug(f"Loaded existing data: {df}")
|
| 33 |
|
| 34 |
# Generate new patient ID
|
| 35 |
new_id = len(df) + 1
|
|
|
|
| 41 |
"Age": [age],
|
| 42 |
"Phone Number": [phone]
|
| 43 |
})
|
| 44 |
+
logging.info(f"New patient data: {new_data}")
|
| 45 |
+
|
| 46 |
df = pd.concat([df, new_data], ignore_index=True)
|
| 47 |
+
logging.debug(f"Updated DataFrame: {df}")
|
| 48 |
|
| 49 |
# Save updated data
|
| 50 |
df.to_csv(CSV_FILE, index=False)
|
| 51 |
+
logging.info(f"Data saved to CSV file successfully at {CSV_FILE}.")
|
| 52 |
|
| 53 |
+
return f"Patient {name} with ID {new_id} registered successfully!", CSV_FILE
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
logging.error(f"An error occurred while registering patient: {e}")
|
| 56 |
+
return f"An error occurred: {e}", None
|
| 57 |
|
| 58 |
# Define Gradio interface
|
| 59 |
with gr.Blocks() as demo:
|
|
|
|
| 64 |
phone_number = gr.Textbox(label="Phone Number", placeholder="Enter phone number")
|
| 65 |
register_button = gr.Button("Register")
|
| 66 |
status = gr.Textbox(label="Status", interactive=False)
|
| 67 |
+
download_button = gr.File(label="Download CSV")
|
| 68 |
+
|
| 69 |
+
def update_status_and_file(*inputs):
|
| 70 |
+
message, file_path = register_patient(*inputs)
|
| 71 |
+
return message, file_path
|
| 72 |
+
|
| 73 |
+
register_button.click(fn=update_status_and_file, inputs=[patient_name, age, phone_number], outputs=[status, download_button])
|
| 74 |
|
| 75 |
# Launch the app
|
| 76 |
if __name__ == "__main__":
|