yougandar commited on
Commit
57fc1f7
·
verified ·
1 Parent(s): 89b415b

Update view_consultation.py

Browse files
Files changed (1) hide show
  1. view_consultation.py +19 -58
view_consultation.py CHANGED
@@ -17,25 +17,6 @@ def format_field_as_string(data):
17
  for item in data])
18
  return str(data)
19
 
20
- def expand_field_to_table(field_data, columns):
21
- """Expand the formatted string back into a DataFrame."""
22
- if not field_data:
23
- return pd.DataFrame(columns=columns)
24
-
25
- rows = [item.split(" (") for item in field_data.split("; ")]
26
- formatted_rows = []
27
- for row in rows:
28
- if len(columns) == 4: # Prescription
29
- drug_name = row[0]
30
- frequency, quantity = row[1].replace(")", "").split(" - ")
31
- formatted_rows.append([len(formatted_rows) + 1, drug_name, frequency, quantity])
32
- else: # Lab Investigations
33
- test_name = row[0]
34
- department = row[1].replace(")", "")
35
- formatted_rows.append([len(formatted_rows) + 1, test_name, department])
36
-
37
- return pd.DataFrame(formatted_rows, columns=columns)
38
-
39
  def get_consultation_details(patient_id):
40
  """Retrieve consultation details for a given Patient ID."""
41
  columns = ["Consultation ID", "Patient ID", "Patient Name", "Height", "Weight",
@@ -50,7 +31,7 @@ def get_consultation_details(patient_id):
50
  # Ensure all required columns are present
51
  missing_cols = [col for col in columns if col not in df.columns]
52
  if missing_cols:
53
- return f"Error: Missing columns in consultation data: {', '.join(missing_cols)}", None, None
54
 
55
  # Convert Patient ID to numeric
56
  df["Patient ID"] = pd.to_numeric(df["Patient ID"], errors='coerce')
@@ -59,7 +40,7 @@ def get_consultation_details(patient_id):
59
  consultation = df[df["Patient ID"] == patient_id]
60
 
61
  if consultation.empty:
62
- return f"No consultation records found for Patient ID: {patient_id}.", None, None
63
 
64
  # Rename columns for display purposes
65
  consultation = consultation.rename(columns={"Height": "Height (cms)", "Weight": "Weight (kgs)"})
@@ -68,30 +49,24 @@ def get_consultation_details(patient_id):
68
  consultation["Prescription"] = consultation["Prescription"].apply(format_field_as_string)
69
  consultation["Lab Investigations"] = consultation["Lab Investigations"].apply(format_field_as_string)
70
 
71
- details = consultation.iloc[0].to_dict()
72
-
73
- # Extract prescriptions and lab investigations into tables
74
- prescription_table = expand_field_to_table(details.get("Prescription", ""), ["Row Number", "Drug Name", "Frequency", "Quantity"])
75
- lab_table = expand_field_to_table(details.get("Lab Investigations", ""), ["Row Number", "Test Name", "Department"])
76
-
77
- # Remove Prescription and Lab Investigations from the general info table
78
- general_info = {key: value for key, value in details.items() if key not in ["Prescription", "Lab Investigations"]}
79
- return general_info, prescription_table, lab_table
80
 
81
  except pd.errors.ParserError as e:
82
- return f"Error reading CSV file: {e}", None, None
83
  except Exception as e:
84
- return f"Unexpected error occurred: {e}", None, None
85
  else:
86
- return "Consultation data file does not exist.", None, None
87
 
88
  def view_consultations(patient_id):
89
- details, prescription_table, lab_table = get_consultation_details(patient_id)
90
- if details:
91
- general_info = pd.DataFrame([details])
92
- return general_info, prescription_table, lab_table
93
  else:
94
- return pd.DataFrame(columns=["Message"], data=[[details]]), pd.DataFrame(), pd.DataFrame()
 
95
 
96
  def gradio_interface():
97
  """Create Gradio interface for viewing consultation details."""
@@ -104,29 +79,15 @@ def gradio_interface():
104
  consultations_output = gr.Dataframe(
105
  headers=["Consultation ID", "Patient ID", "Patient Name", "Height (cms)", "Weight (kgs)",
106
  "Diastolic BP", "Systolic BP", "Temperature", "Chief Complaints",
107
- "Revisit Date", "Revisit Time"],
108
- row_count=1,
109
- col_count=11
110
- )
111
- prescription_output = gr.Dataframe(
112
- headers=["Row Number", "Drug Name", "Frequency", "Quantity"],
113
- row_count=10,
114
- col_count=4
115
  )
116
- lab_output = gr.Dataframe(
117
- headers=["Row Number", "Test Name", "Department"],
118
- row_count=10,
119
- col_count=3
120
- )
121
-
122
- def update_consultation_view(patient_id):
123
- general_info, prescription_table, lab_table = view_consultations(patient_id)
124
- return general_info, prescription_table, lab_table
125
 
126
  patient_id_input.change(
127
- fn=update_consultation_view,
128
  inputs=patient_id_input,
129
- outputs=[consultations_output, prescription_output, lab_output]
130
  )
131
 
132
  return demo
@@ -134,4 +95,4 @@ def gradio_interface():
134
  # Run the Gradio app
135
  if __name__ == "__main__":
136
  interface = gradio_interface()
137
- interface.launch()
 
17
  for item in data])
18
  return str(data)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def get_consultation_details(patient_id):
21
  """Retrieve consultation details for a given Patient ID."""
22
  columns = ["Consultation ID", "Patient ID", "Patient Name", "Height", "Weight",
 
31
  # Ensure all required columns are present
32
  missing_cols = [col for col in columns if col not in df.columns]
33
  if missing_cols:
34
+ return f"Error: Missing columns in consultation data: {', '.join(missing_cols)}"
35
 
36
  # Convert Patient ID to numeric
37
  df["Patient ID"] = pd.to_numeric(df["Patient ID"], errors='coerce')
 
40
  consultation = df[df["Patient ID"] == patient_id]
41
 
42
  if consultation.empty:
43
+ return f"No consultation records found for Patient ID: {patient_id}."
44
 
45
  # Rename columns for display purposes
46
  consultation = consultation.rename(columns={"Height": "Height (cms)", "Weight": "Weight (kgs)"})
 
49
  consultation["Prescription"] = consultation["Prescription"].apply(format_field_as_string)
50
  consultation["Lab Investigations"] = consultation["Lab Investigations"].apply(format_field_as_string)
51
 
52
+ details = consultation.to_dict(orient="records")
53
+ print("Details:", details) # Debugging line
54
+ return details
 
 
 
 
 
 
55
 
56
  except pd.errors.ParserError as e:
57
+ return f"Error reading CSV file: {e}"
58
  except Exception as e:
59
+ return f"Unexpected error occurred: {e}"
60
  else:
61
+ return "Consultation data file does not exist."
62
 
63
  def view_consultations(patient_id):
64
+ details = get_consultation_details(patient_id)
65
+ if isinstance(details, list) and details:
66
+ return pd.DataFrame(details)
 
67
  else:
68
+ print("View Consultation Error:", details) # Debugging line
69
+ return pd.DataFrame(columns=["Message"], data=[[details]])
70
 
71
  def gradio_interface():
72
  """Create Gradio interface for viewing consultation details."""
 
79
  consultations_output = gr.Dataframe(
80
  headers=["Consultation ID", "Patient ID", "Patient Name", "Height (cms)", "Weight (kgs)",
81
  "Diastolic BP", "Systolic BP", "Temperature", "Chief Complaints",
82
+ "Prescription", "Lab Investigations", "Revisit Date", "Revisit Time"],
83
+ row_count=10,
84
+ col_count=13
 
 
 
 
 
85
  )
 
 
 
 
 
 
 
 
 
86
 
87
  patient_id_input.change(
88
+ fn=view_consultations,
89
  inputs=patient_id_input,
90
+ outputs=consultations_output
91
  )
92
 
93
  return demo
 
95
  # Run the Gradio app
96
  if __name__ == "__main__":
97
  interface = gradio_interface()
98
+ interface.launch()