-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.py
620 lines (544 loc) · 23.7 KB
/
web.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
from audioop import minmax
from xml.dom.pulldom import SAX2DOM
from flask import Flask, g, flash, render_template, url_for, request, session, redirect,jsonify
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler
from random import shuffle
import pandas as pd
from flask_mysqldb import MySQL
from jinja2 import Environment, FileSystemLoader,Template
import MySQLdb.cursors
import re
import math
app = Flask(__name__)
app.config["SECRET_KEY"] = "thisisMysecretkey"
# config DB
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'essks'
mysql = MySQL(app)
#LOGIN LOGOUT REGISTER#
@app.route('/login', methods=['GET', 'POST'])
def login():
msg = ''
if 'loggedin' in session:
return redirect(url_for('index'))
elif request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM user WHERE username = %s AND password = %s', (username, password))
account = cursor.fetchone()
if account:
session['loggedin'] = True
session['id'] = account['userID']
session['username'] = account['username']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM penyakit")
penyakit= cur.fetchall()
cur.execute("SELECT * FROM gejala")
gejala= cur.fetchall()
cur.execute("SELECT * FROM rules")
rules= cur.fetchall()
cur.execute("SELECT * FROM rekammed")
rm = cur.fetchall()
cur.close()
return render_template('index.html', msg = msg, username = username,p=penyakit,g=gejala,r=rules,rm = rm)
else:
msg = "WRONG USERNAME / PASSWORD"
return render_template('login.html', msg=msg)
return render_template('login.html', msg=msg)
@app.route('/logout')
def logout():
session.pop('loggedin', None)
session.pop('id', None)
session.pop('username', None)
return redirect(url_for('login'))
#=====PAGES======#
@app.route("/")
@app.route("/index")
def index():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM penyakit")
penyakit= cur.fetchall()
cur.execute("SELECT * FROM gejala")
gejala= cur.fetchall()
cur.execute("SELECT * FROM rules")
rules= cur.fetchall()
cur.execute("SELECT * FROM rekammed")
rm = cur.fetchall()
cur.close()
if 'loggedin' in session:
return render_template('index.html',p=penyakit,g=gejala,r=rules,rm = rm,username=session['username'])
else:
return render_template('index.html',p=penyakit,g=gejala,r=rules,rm = rm)
@app.route("/keluhan")
def keluhan():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM keluhan")
keluhan = cur.fetchall()
cur.close()
if 'loggedin' in session:
return render_template('keluhan.html',data=keluhan, username=session['username'])
return render_template('keluhan.html',data=keluhan)
@app.route("/iniDiagnosis", methods=["GET","POST"])
def iniDiagnosis():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM keluhan")
k = cur.fetchall()
# Keluhan1 adalah table temp. (tidak ke save di mySQL, tapi value ke store di jinja)
cur.execute("INSERT INTO keluhan1 (gejalaID, penyakitID, gejalaDesc, PenyakitNama, CFU, CFD, CFCOMBI) SELECT rules.gejalaID,rules.penyakitID, keluhan.gejalaDesc, rules.penyakitNama, keluhan.CFU, rules.CFD, (keluhan.CFU*rules.CFD) as CFCOMBI FROM keluhan INNER JOIN rules ON keluhan.gejalaDesc=rules.gejalaDesc WHERE keluhan.CFU != '0' and rules.CFD != '0' AND NOT EXISTS (SELECT * FROM keluhan1) ORDER BY keluhan.gejalaDesc")
cur.execute("SELECT * from keluhan1")
keluhan = cur.fetchall()
cur.execute("SELECT penyakitNama,GROUP_CONCAT(gejalaDesc), GROUP_CONCAT(CFU*CFD) FROM keluhan1 GROUP BY penyakitNama;")
keluhann = cur.fetchall()
cur.execute("SELECT penyakitNama FROM keluhan1 GROUP BY penyakitNama")
keluhannn = cur.fetchall()
# cur.execute("INSERT INTO rekammed2 (gejalaDesc,CFUMAX,CFUMIN,totGejala) SELECT gejalaDesc, MAX(CFU), MIN(CFU), count(gejalaDesc) FROM rekammed WHERE NOT EXISTS (SELECT * FROM rekammed2) GROUP BY rekammed.gejalaDesc;")
cur.close()
if 'loggedin' in session:
return render_template('inidiagnosisnya.html',data=keluhan,dataa=keluhann,dataaa=keluhannn,datak = k, username=session['username'])
return render_template('inidiagnosisnya.html',data=keluhan,dataa=keluhann,dataaa=keluhannn,datak = k)
@app.route("/infogejala")
def infogejala():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM gejala")
gejala = cur.fetchall()
cur.close()
if 'loggedin' in session:
return render_template('infogejala.html',username=session['username'], data=gejala)
return render_template('infogejala.html', data = gejala)
@app.route("/infopenyakit")
def infopenyakit():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM penyakit")
penyakit = cur.fetchall()
cur.close()
if 'loggedin' in session:
return render_template('infopenyakit.html',username=session['username'], data=penyakit)
return render_template('infopenyakit.html', data=penyakit)
@app.route("/basispengetahuan")
def basispengetahuan():
curr = mysql.connection.cursor()
curr.execute("SELECT * FROM rules")
rules = curr.fetchall()
curr.close()
if 'loggedin' in session:
return render_template('basispengetahuan.html',username=session['username'], data=rules)
return render_template('basispengetahuan.html', data=rules)
@app.route("/tambahgejala")
def tambahgejala():
return render_template('tambahgejala.html')
@app.route("/knn")
def knn():
if 'loggedin' in session:
cur = mysql.connect.cursor()
cur.execute("SELECT * from keluhan")
c = cur.fetchall()
cur.execute("SELECT pasienNama, GROUP_CONCAT(gejalaDesc), penyakitDia FROM rekammed GROUP BY pasienNama ORDER BY RAND() LIMIT 5;")
rm = cur.fetchall()
cur.execute("SELECT * FROM rekammed2")
dm = cur.fetchall()
cur.execute("SELECT pasienNama FROM pasien GROUP BY pasienNama")
pasien = cur.fetchall()
cur.execute("SELECT * FROM keluhan3")
k3 = cur.fetchall()
cur.execute("SELECT * FROM rekammed3")
r3 = cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('knn.html',data=rm,datamm=dm,pasien=pasien,k3=k3,r3=r3,c=c,username=session['username'])
return redirect(url_for('login'))
@app.route("/tambahpenyakit")
def tambahpenyakit():
return render_template('tambahpenyakit.html')
@app.route("/diagnosispenyakit")
def diagnosispenyakit():
if 'loggedin' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM gejala")
gejala = cur.fetchall()
cur.execute("select * from keluhan;")
keluhan = cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('diagnosispenyakit.html', data = gejala, datak = keluhan, username=session['username'])
return render_template('login.html')
@app.route("/tentang")
def tentang():
if 'loggedin' in session:
return render_template('tentang.html',username=session['username'])
else:
return render_template('tentang.html')
@app.route("/k")
def k():
if 'loggedin' in session:
return render_template('k.html',username=session['username'])
return redirect(url_for('login'))
@app.route("/riwayatpasien")
def riwayatpasien():
if 'loggedin' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT pasienNama, GROUP_CONCAT(gejalaDesc), penyakitDia FROM rekammed GROUP BY pasienNama ORDER BY rekamID;")
rm = cur.fetchall()
cur.close()
return render_template('riwayatpasien.html',data=rm,username=session['username'])
return redirect(url_for('login'))
@app.route("/riwayatpasien2")
def riwayatpasien2():
if 'loggedin' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM gejala")
gejala = cur.fetchall()
cur.execute("SELECT * FROM ex")
ex = cur.fetchall()
cur.close()
return render_template('riwayatpasien2.html', gejala=gejala, data = ex,username=session['username'])
return redirect(url_for('login'))
@app.route("/knnA")
def knnA():
cur = mysql.connect.cursor()
# Untuk bikin database testing
# cur.execute("CREATE TABLE testing AS (SELECT pasienNama, GROUP_CONCAT(gejalaDesc), penyakitDia FROM rekammed GROUP BY pasienNama ORDER BY RAND() LIMIT 20)")
cur.execute("SELECT * FROM testing")
rm = cur.fetchall()
# cur.execute("INSERT INTO rekammed2 (gejalaDesc,CFUMAX,CFUMIN,totGejala) SELECT gejalaDesc, MAX(CFU), MIN(CFU), count(gejalaDesc) FROM rekammed WHERE NOT EXISTS (SELECT * FROM rekammed2) GROUP BY rekammed.gejalaDesc")
cur.execute("SELECT * FROM rekammed2")
dm = cur.fetchall()
cur.execute("SELECT pasienNama FROM pasien")
pasien = cur.fetchall()
# cur.execute("INSERT INTO keluhan3 (gejalaDesc, CFU,CFUMAX,CFUMIN,NORM) SELECT keluhan.gejalaDesc, keluhan.CFU, rekammed2.CFUMAX,rekammed2.CFUMIN, ((keluhan.CFU-rekammed2.CFUMIN)/(rekammed2.CFUMAX-rekammed2.CFUMIN)) AS 'NORM' from keluhan LEFT JOIN rekammed2 ON rekammed2.gejalaDesc = keluhan.gejalaDesc WHERE NOT EXISTS (SELECT * FROM keluhan3)")
cur.execute("SELECT * FROM keluhan3")
k3 = cur.fetchall()
# cur.execute("INSERT INTO rekammed3 (pasienNama,gejalaDesc,CFU,CFUMAX,CFUMIN,NORM,penyakitDia) SELECT rekammed.pasienNama,rekammed2.gejalaDesc, rekammed.CFU,rekammed2.CFUMAX,rekammed2.CFUMIN, ((rekammed.CFU-rekammed2.CFUMIN)/(rekammed2.CFUMAX-rekammed2.CFUMIN)) AS 'NORM', rekammed.penyakitDia FROM rekammed LEFT JOIN rekammed2 ON rekammed2.gejalaDesc = rekammed.gejalaDesc WHERE NOT EXISTS (SELECT * FROM rekammed3) ORDER BY rekammed.pasienNama")
# cur.execute("UPDATE rekammed3 SET NORM = '0' where NORM is NULL;")
cur.execute("SELECT * FROM rekammed3")
r3 = cur.fetchall()
mysql.connection.commit()
cur.close()
return render_template('knnA.html',data=rm,datamm=dm,pasien=pasien,k3=k3,r3=r3,username=session['username'])
@app.route('/dataset')
def dataset():
items = ReadData('rekammed.txt')
items = pd.DataFrame(items)
return render_template('dataset.html',items=items.to_html(classes='table table-striped table-hover table-bordered table-sm table-responsive-sm'))
@app.route('/setting', methods=['GET','POST'])
def setting():
msg = ""
cur = mysql.connection.cursor()
cur.execute("SELECT * from user")
users = cur.fetchall()
if 'loggedin' in session and session['username'] != 'admin':
return render_template('index.html', username = session['username'] )
elif request.method == 'POST':
nama = request.form['username']
sipass = request.form['password']
cur.execute('SELECT * FROM user WHERE username = %s AND password = %s', (nama, sipass))
account = cur.fetchone()
if account:
flash("USER ALREADY EXIST")
else:
cur.execute("INSERT INTO user(username,password) VALUES(%s,%s)", (nama,sipass))
mysql.connection.commit()
cur.close()
return redirect(url_for('setting'))
else:
return render_template('setting.html', username = session['username'],data=users,msg = msg)
@app.route('/sakti', methods=['GET','POST'])
def sakti():
GP01 = 0
GP02 = 0
GP03 = 0
GP04 = 0
GP05 = 0
GP06 = 0
GP07 = 0
GP08 = 0
GP09 = 0
GP10 = 0
GP11 = 0
GP12 = 0
GP13 = 0
GP14 = 0
GP15 = 0
GP16 = 0
GP17 = 0
GP18 = 0
GP19 = 0
GP20 = 0
GP21 = 0
GP22 = 0
GP23 = 0
GP24 = 0
GP25 = 0
GP26 = 0
GP27 = 0
GP28 = 0
cur = mysql.connect.cursor()
#template data testing
newItem = {'GP01': 0 ,'GP02': 0,'GP03': 0,'GP04': 0,'GP05': 0,'GP06': 0,'GP07': 0,'GP08': 0,'GP09': 0,'GP10': 0,'GP11': 0,'GP12': 0,'GP13': 0,'GP14': 0,'GP15': 0,'GP16': 0,'GP17': 0,'GP18': 0,'GP19': 0,'GP20': 0,'GP21': 0,'GP22': 0,'GP23': 0,'GP24': 0,'GP25': 0,'GP26': 0,'GP27': 0,'GP28': 0}
cur.execute("SELECT keluhan.gejalaDesc, keluhan.CFU, gejala.gejalaID from keluhan inner join gejala ON gejala.gejalaDesc = keluhan.gejalaDesc")
c = cur.fetchall()
cur.execute("SELECT gejala.gejalaID, keluhan.CFU from keluhan inner join gejala on gejala.gejalaDesc = keluhan.gejalaDesc")
da = cur.fetchall()
for dar in da:
if dar[0] == "GP01":
GP01 = float(dar[1])
elif dar[0] == "GP02":
GP02 = float(dar[1])
elif dar[0] == "GP03":
GP03 = float(dar[1])
elif dar[0] == "GP04":
GP04 = float(dar[1])
elif dar[0] == "GP05":
GP05 = float(dar[1])
elif dar[0] == "GP06":
GP06 = float(dar[1])
elif dar[0] == "GP07":
GP07 = float(dar[1])
elif dar[0] == "GP08":
GP08 = float(dar[1])
elif dar[0] == "GP09":
GP09 = float(dar[1])
elif dar[0] == "GP10":
GP10 = float(dar[1])
elif dar[0] == "GP11":
GP11 = float(dar[1])
elif dar[0] == "GP12":
GP12 = float(dar[1])
elif dar[0] == "GP13":
GP13 = float(dar[1])
elif dar[0] == "GP14":
GP14 = float(dar[1])
elif dar[0] == "GP15":
GP15 = float(dar[1])
elif dar[0] == "GP16":
GP16 = float(dar[1])
elif dar[0] == "GP17":
GP17 = float(dar[1])
elif dar[0] == "GP18":
GP18 = float(dar[1])
elif dar[0] == "GP19":
GP19 = float(dar[1])
elif dar[0] == "GP20":
GP20 = float(dar[1])
elif dar[0] == "GP21":
GP21 = float(dar[1])
elif dar[0] == "GP22":
GP22 = float(dar[1])
elif dar[0] == "GP23":
GP23 = float(dar[1])
elif dar[0] == "GP24":
GP24 = float(dar[1])
elif dar[0] == "GP25":
GP25 = float(dar[1])
elif dar[0] == "GP26":
GP26 = float(dar[1])
elif dar[0] == "GP27":
GP27 = float(dar[1])
elif dar[0] == "GP28":
GP28 = float(dar[1])
items = ReadData('rekammed.txt')
# data testing
newItem = {'GP01': GP01 ,'GP02': GP02,'GP03': GP03, 'GP04': GP04,'GP05': GP05,'GP06': GP06,'GP07': GP07,'GP08': GP08,'GP09': GP09,'GP10': GP10,'GP11': GP11,'GP12': GP12,'GP13': GP13,'GP14': GP14,'GP15': GP15,'GP16': GP16,'GP17':GP17,'GP18': GP18,'GP19': GP19,'GP20': GP20,'GP21': GP21,'GP22': GP22,'GP23': GP23,'GP24': GP24,'GP25': GP25,'GP26': GP26,'GP27': GP27,'GP28': GP28}
k = 6
accuracy2, maxi, count, neighbors = Classify(newItem, k, items)
skor = pd.DataFrame(neighbors, columns = ['Skor Euclidean' , 'Diagnosis'])
keputusan = pd.DataFrame(list(count.items()), columns=['Diagnosis', 'Jumlah Kemunculan'])
mysql.connection.commit()
if 'loggedin' in session:
return render_template('sakti.html', keputusan=keputusan.to_html(classes='table table-striped table-hover table-bordered table-sm table-responsive-sm'), skor=skor.to_html(classes='table table-striped table-hover table-bordered table-sm table-responsive-sm'), ni=newItem, data=c, evaluat=accuracy2, items=items, k=k,username=session['username'])
return render_template('sakti.html', keputusan=keputusan.to_html(classes='table table-striped table-hover table-bordered table-sm table-responsive-sm'), skor=skor.to_html(classes='table table-striped table-hover table-bordered table-sm table-responsive-sm'), ni=newItem, data=c, evaluat=accuracy2, items=items, k=k)
#=====FUNCTIONS=====#
@app.route('/hapusPenyakit/<string:id_data>', methods=["GET"])
def hapusPenyakit(id_data):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM penyakit WHERE penyakitID=%s", (id_data,))
mysql.connection.commit()
return redirect(url_for('infopenyakit'))
@app.route('/updatePenyakit', methods=["POST"])
def updatePenyakit():
namaPenyakit = request.form['namaPenyakit']
defPenyakit = request.form['definisPenyakit']
solPenyakit = request.form['solusiPenyakit']
pi1 = request.form['np1']
pi2 = request.form['np2']
pi3 = request.form['np3']
cur = mysql.connection.cursor()
cur.execute("UPDATE penyakit SET penyakitNama=%s WHERE penyakitNama=%s", (namaPenyakit,pi1))
cur.execute("UPDATE penyakit SET penyakitDefinisi=%s WHERE penyakitDefinisi=%s", (defPenyakit,pi2))
cur.execute("UPDATE penyakit SET penyakitSolusi=%s WHERE penyakitSolusi=%s", (solPenyakit,pi3))
mysql.connection.commit()
return redirect(url_for('infopenyakit'))
@app.route('/hapusGejala/<string:id_data>', methods=["GET"])
def hapusGejala(id_data):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM gejala WHERE gejalaID=%s", (id_data,))
mysql.connection.commit()
return redirect(url_for('infogejala'))
@app.route('/hapusGejalaExt/<string:id_data>', methods=["GET"])
def hapusGejalaExt(id_data):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM keluhan WHERE keluhanID=%s", (id_data,))
mysql.connection.commit()
return redirect(url_for('keluhan'))
@app.route('/updateGejala', methods=["POST"])
def updateGejala():
gejala = request.form['gejala']
pi1 = request.form['np1']
cur = mysql.connection.cursor()
cur.execute("UPDATE gejala SET gejalaDesc=%s WHERE gejalaDesc=%s", (gejala,pi1))
mysql.connection.commit()
return redirect(url_for('infogejala'))
@app.route('/updateUser', methods=["POST"])
def updateUser():
uname = request.form['uname']
pi1 = request.form['np1']
passw = request.form['passw']
pi2 = request.form['np2']
cur = mysql.connection.cursor()
cur.execute("UPDATE user SET username=%s WHERE username=%s", (uname,pi1))
cur.execute("UPDATE user SET password=%s WHERE password=%s", (passw,pi2))
mysql.connection.commit()
return redirect(url_for('setting'))
@app.route('/hapusUser/<string:id_data>', methods=["GET"])
def hapusUser(id_data):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM user WHERE userID=%s", (id_data,))
mysql.connection.commit()
return redirect(url_for('setting'))
@app.route("/hapusRekamMedis")
def hapusRekamMedis():
cur = mysql.connection.cursor()
cur.execute("TRUNCATE keluhan")
mysql.connection.commit()
cur.close()
return redirect(url_for('diagnosispenyakit'))
@app.route('/resetDataTesting/<string:id_data>', methods=["GET,POST"])
def resetDataTesting(id_data):
cur = mysql.connection.cursor()
cur.execute("CREATE TABLE testing AS (SELECT pasienNama, GROUP_CONCAT(gejalaDesc), penyakitDia FROM rekammed GROUP BY pasienNama ORDER BY RAND() LIMIT %s", (id_data))
mysql.connection.commit()
cur.close()
return redirect(url_for('knnA'))
@app.route("/simpanPilihanGejala", methods=["POST"])
def simpanPilihanGejala():
gejalaChoosen = request.form["gejalaChoosen"]
gejalanya = request.form["gejalaApaIni"]
if 'radioName' not in request.form:
print('Error: The form is empty')
cur = mysql.connection.cursor()
cur.execute("INSERT INTO keluhan(gejalaDesc, CFU) VALUES(%s,%s)",(gejalanya, gejalaChoosen))
mysql.connection.commit()
cur.close()
return redirect(url_for('diagnosispenyakit'))
@app.route("/simpanFormPenyakit", methods=["POST"])
def simpanFormPenyakit():
idPenyakit = request.form["idPenyakit"]
namaPenyakit = request.form["namaPenyakit"]
definisPenyakit = request.form["definisPenyakit"]
solusiPenyakit = request.form["solusiPenyakit"]
cur = mysql.connection.cursor()
cur.execute("INSERT INTO penyakit(penyakitID,penyakitNama,penyakitSolusi,penyakitDefinisi) VALUES(%s,%s,%s,%s)", (idPenyakit,namaPenyakit,definisPenyakit,solusiPenyakit))
mysql.connection.commit()
cur.close()
return redirect(url_for('infopenyakit'))
@app.route("/simpanFormGejala", methods=["POST"])
def simpanFormGejala():
kodeGejala = request.form["kodeGejala"]
namaGejala = request.form["namaGejala"]
cur = mysql.connection.cursor()
cur.execute("INSERT INTO gejala(gejalaID,gejalaDesc) VALUES(%s,%s)", (kodeGejala,namaGejala))
mysql.connection.commit()
cur.close()
return redirect(url_for('infogejala'))
@app.route("/simpanRekamMedis", methods=["POST"])
def simpanRekamMedis():
namaPasien = request.form["namaPasien"]
umurPasien = request.form["umurPasien"]
genderPasien = request.form["genderPasien"]
np2 = request.form["np2"]
cur = mysql.connection.cursor()
cur.execute("INSERT INTO pasien(pasienNama,pasienGender,pasienUmur) VALUES(%s,%s,%s)", (namaPasien,genderPasien,umurPasien))
cur.execute("INSERT INTO rekammed(gejalaDesc,CFU,rmTEMP) SELECT gejalaDesc,CFU,'1' FROM keluhan")
cur.execute("UPDATE rekammed SET pasienNama=%s WHERE rmTEMP=%s",(namaPasien,'1'))
cur.execute("UPDATE rekammed SET penyakitDia=%s WHERE rmTEMP=%s",(np2,'1'))
cur.execute("UPDATE rekammed SET rmTEMP = '0'")
cur.execute("TRUNCATE keluhan")
mysql.connection.commit()
cur.close()
return redirect(url_for('diagnosispenyakit'))
def ReadData(fileName):
#dataset
f = open(fileName, 'r')
lines = f.read().splitlines()
f.close()
#feature
features = lines[0].split(',')[:-1]
#list
items = []
for i in range(1, len(lines)):
line = lines[i].split(',')
itemFeatures = {'Class': line[-1]}
#Iterate feature
for j in range(len(features)):
# Get the feature at index j
f = features[j]
# Convert feature value to float
v = float(line[j])
# Add feature value to dict
itemFeatures[f] = v
#Append temp dict to items
items.append(itemFeatures)
#shuffle(items)
return items
def EuclideanDistance(x,y):
S=0
for key in x.keys():
S += math.pow(x[key]-y[key], 2)
return math.sqrt(S)
def CalculateNeighborsClass(neighbors,k):
count = {}
for i in range(k):
if neighbors[i][1] not in count:
count[neighbors[i][1]] = 1
else:
count[neighbors[i][1]] += 1
return count
def FindMax(Dict):
maximum = -1
classification = ''
for key in Dict.keys():
if Dict[key] > maximum:
maximum = Dict[key]
classification = key
return classification, maximum
def Classify(nItem, k, Items):
if (k > len(Items)):
return "k larger than list length"
neighbors = []
distance2 = []
for item in Items:
#Find Euclidean Distance
distance = EuclideanDistance(nItem, item)
distance2.append(distance)
#Update neigbors
neigbors = UpdateNeighbors(neighbors,item,distance,k)
#Count number each class
count = CalculateNeighborsClass(neighbors, k)
#find the max in count / class with the most appearances
klas,maxi = FindMax(count)
return klas, maxi, count, neighbors
def UpdateNeighbors(neighbors,item,distance,k):
if len(neighbors) < k:
neighbors.append([distance, item['Class']])
neighbors = sorted(neighbors)
else:
if neighbors[-1][0] > distance:
neighbors[-1] = [distance, item['Class']]
neighbors = sorted(neighbors)
return neighbors
##############################################################################################################
if __name__ == "__main__":
app.run(debug=True)