-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatient.py
323 lines (303 loc) · 13.8 KB
/
patient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import streamlit as st
from datetime import datetime, date
import database as db
import pandas as pd
# function to verify patient id
def verify_patient_id(patient_id):
verify = False
conn, c = db.connection()
with conn:
c.execute(
"""
SELECT id
FROM patient_record;
"""
)
for id in c.fetchall():
if id[0] == patient_id:
verify = True
break
conn.close()
return verify
# function to generate unique patient id using current date and time
def generate_patient_id(reg_date, reg_time):
id_1 = ''.join(reg_time.split(':')[::-1])
id_2 = ''.join(reg_date.split('-')[::-1])[2:]
id = f'P-{id_1}-{id_2}'
return id
# function to calculate age using given date of birth
def calculate_age(dob):
today = date.today()
age = today.year - dob.year - ((dob.month, dob.day) > (today.month, today.day))
return age
# function to show the details of patient(s) given in a list (provided as a parameter)
def show_patient_details(list_of_patients):
patient_titles = ['Patient ID', 'Name', 'Age', 'Gender', 'Date of birth (DD-MM-YYYY)',
'Blood group', 'Contact number', 'Alternate contact number',
'Nida_no', 'Weight (kg)', 'Height (cm)', 'Address',
'City', 'State', 'PIN code', "Next of kin's name",
"Next of kin's relation to patient",
"Next of kin's contact number", 'Email ID',
'Date of registration (DD-MM-YYYY)', 'Time of registration (hh:mm:ss)']
if len(list_of_patients) == 0:
st.warning('No data to show')
elif len(list_of_patients) == 1:
patient_details = [x for x in list_of_patients[0]]
series = pd.Series(data = patient_details, index = patient_titles)
st.write(series)
else:
patient_details = []
for patient in list_of_patients:
patient_details.append([x for x in patient])
df = pd.DataFrame(data = patient_details, columns = patient_titles)
st.write(df)
# class containing all the fields and methods required to work with the patients' table in the database
class Patient:
def __init__(self):
self.name = str()
self.id = str()
self.gender = str()
self.age = int()
self.contact_number_1 = str()
self.contact_number_2 = str()
self.date_of_birth = str()
self.blood_group = str()
self.date_of_registration = str()
self.time_of_registration = str()
self.email_id = str()
self.nida_no = str()
self.height = int()
self.weight = int()
self.next_of_kin_name = str()
self.next_of_kin_relation_to_patient = str()
self.next_of_kin_contact_number = str()
self.address = str()
self.city = str()
self.state = str()
self.pin_code = str()
# method to add a new patient record to the database
def add_patient(self):
st.write('Enter patient details:')
self.name = st.text_input('Full name')
gender = st.radio('Gender', ['Female', 'Male', 'Other'])
if gender == 'Other':
gender = st.text_input('Please mention')
self.gender = gender
dob = st.date_input('Date of birth (YYYY/MM/DD)')
st.info('If the required date is not in the calendar, please type it in the box above.')
self.date_of_birth = dob.strftime('%d-%m-%Y') # converts date of birth to the desired string format
self.age = calculate_age(dob)
self.blood_group = st.text_input('Blood group')
self.contact_number_1 = st.text_input('Contact number')
contact_number_2 = st.text_input('Alternate contact number (optional)')
self.contact_number_2 = (lambda phone : None if phone == '' else phone)(contact_number_2)
self.nida_no = st.text_input('nida_no')
self.weight = st.number_input('Weight (in kg)', value = 0, min_value = 0, max_value = 400)
self.height = st.number_input('Height (in cm)', value = 0, min_value = 0, max_value = 275)
self.address = st.text_area('Address')
self.city = st.text_input('City')
self.state = st.text_input('State')
self.pin_code = st.text_input('PIN code')
self.next_of_kin_name = st.text_input("Next of kin's name")
self.next_of_kin_relation_to_patient = st.text_input("Next of kin's relation to patient")
self.next_of_kin_contact_number = st.text_input("Next of kin's contact number")
email_id = st.text_input('Email ID (optional)')
self.email_id = (lambda email : None if email == '' else email)(email_id)
self.date_of_registration = datetime.now().strftime('%d-%m-%Y')
self.time_of_registration = datetime.now().strftime('%H:%M:%S')
self.id = generate_patient_id(self.date_of_registration,
self.time_of_registration)
save = st.button('Save')
# executing SQLite statements to save the new patient record to the database
if save:
conn, c = db.connection()
with conn:
c.execute(
"""
INSERT INTO patient_record
(
id, name, age, gender, date_of_birth, blood_group,
contact_number_1, contact_number_2, nida_no,
weight, height, address,city, state, pin_code,
next_of_kin_name, next_of_kin_relation_to_patient,
next_of_kin_contact_number, email_id,
date_of_registration, time_of_registration
)
VALUES (
:id, :name, :age, :gender, :dob, :blood_group,
:phone_1, :phone_2, :uid, :weight, :height,
:address, :city, :state, :pin,
:kin_name, :kin_relation, :kin_phone, :email_id,
:reg_date, :reg_time
);
""",
{
'id': self.id, 'name': self.name, 'age': self.age,
'gender': self.gender, 'dob': self.date_of_birth,
'blood_group': self.blood_group,
'phone_1': self.contact_number_1,
'phone_2': self.contact_number_2,
'uid': self.nida_no, 'weight': self.weight,
'height': self.height, 'address': self.address,
'city': self.city, 'state': self.state,
'pin': self.pin_code, 'kin_name': self.next_of_kin_name,
'kin_relation': self.next_of_kin_relation_to_patient,
'kin_phone': self.next_of_kin_contact_number,
'email_id': self.email_id,
'reg_date': self.date_of_registration,
'reg_time': self.time_of_registration
}
)
st.success('Patient details saved successfully.')
st.write('Your Patient ID is: ', self.id)
conn.close()
# method to update an existing patient record in the database
def update_patient(self):
id = st.text_input('Enter Patient ID of the patient to be updated')
if id == '':
st.empty()
elif not verify_patient_id(id):
st.error('Invalid Patient ID')
else:
st.success('Verified')
conn, c = db.connection()
# shows the current details of the patient before updating
with conn:
c.execute(
"""
SELECT *
FROM patient_record
WHERE id = :id;
""",
{ 'id': id }
)
st.write('Here are the current details of the patient:')
show_patient_details(c.fetchall())
st.write('Enter new details of the patient:')
self.contact_number_1 = st.text_input('Contact number')
contact_number_2 = st.text_input('Alternate contact number (optional)')
self.contact_number_2 = (lambda phone : None if phone == '' else phone)(contact_number_2)
self.weight = st.number_input('Weight (in kg)', value = 0, min_value = 0, max_value = 400)
self.height = st.number_input('Height (in cm)', value = 0, min_value = 0, max_value = 275)
self.address = st.text_area('Address')
self.city = st.text_input('City')
self.state = st.text_input('State')
self.pin_code = st.text_input('PIN code')
self.next_of_kin_name = st.text_input("Next of kin's name")
self.next_of_kin_relation_to_patient = st.text_input("Next of kin's relation to patient")
self.next_of_kin_contact_number = st.text_input("Next of kin's contact number")
email_id = st.text_input('Email ID (optional)')
self.email_id = (lambda email : None if email == '' else email)(email_id)
update = st.button('Update')
# executing SQLite statements to update this patient's record in the database
if update:
with conn:
c.execute(
"""
SELECT date_of_birth
FROM patient_record
WHERE id = :id;
""",
{ 'id': id }
)
# converts date of birth to the required format for age calculation
dob = [int(d) for d in c.fetchone()[0].split('-')[::-1]]
dob = date(dob[0], dob[1], dob[2])
self.age = calculate_age(dob)
with conn:
c.execute(
"""
UPDATE patient_record
SET age = :age, contact_number_1 = :phone_1,
contact_number_2 = :phone_2, weight = :weight,
height = :height, address = :address, city = :city,
state = :state, pin_code = :pin, next_of_kin_name = :kin_name,
next_of_kin_relation_to_patient = :kin_relation,
next_of_kin_contact_number = :kin_phone, email_id = :email_id
WHERE id = :id;
""",
{
'id': id, 'age': self.age,
'phone_1': self.contact_number_1,
'phone_2': self.contact_number_2,
'weight': self.weight, 'height': self.height,
'address': self.address, 'city': self.city,
'state': self.state, 'pin': self.pin_code,
'kin_name': self.next_of_kin_name,
'kin_relation': self.next_of_kin_relation_to_patient,
'kin_phone': self.next_of_kin_contact_number,
'email_id': self.email_id
}
)
st.success('Patient details updated successfully.')
conn.close()
# method to delete an existing patient record from the database
def delete_patient(self):
id = st.text_input('Enter Patient ID of the patient to be deleted')
if id == '':
st.empty()
elif not verify_patient_id(id):
st.error('Invalid Patient ID')
else:
st.success('Verified')
conn, c = db.connection()
# shows the current details of the patient before deletion
with conn:
c.execute(
"""
SELECT *
FROM patient_record
WHERE id = :id;
""",
{ 'id': id }
)
st.write('Here are the details of the patient to be deleted:')
show_patient_details(c.fetchall())
confirm = st.checkbox('Check this box to confirm deletion')
if confirm:
delete = st.button('Delete')
# executing SQLite statements to delete this patient's record from the database
if delete:
c.execute(
"""
DELETE FROM patient_record
WHERE id = :id;
""",
{ 'id': id }
)
st.success('Patient details deleted successfully.')
conn.close()
# method to show the complete patient record
def show_all_patients(self):
conn, c = db.connection()
with conn:
c.execute(
"""
SELECT *
FROM patient_record;
"""
)
show_patient_details(c.fetchall())
conn.close()
# method to search and show a particular patient's details in the database using patient id
def search_patient(self):
id = st.text_input('Enter Patient ID of the patient to be searched')
if id == '':
st.empty()
elif not verify_patient_id(id):
st.error('Invalid Patient ID')
else:
st.success('Verified')
conn, c = db.connection()
with conn:
c.execute(
"""
SELECT *
FROM patient_record
WHERE id = :id;
""",
{ 'id': id }
)
st.write('Here are the details of the patient you searched for:')
show_patient_details(c.fetchall())
conn.close()