Skip to content

Commit 6ce2f06

Browse files
author
mounika
committed
initial commit
0 parents  commit 6ce2f06

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FHIR_USERNAME=
2+
FHIR_PASSWORD=
3+
FHIR_PORT=5000

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# fhir-condition-finder
2+
3+
This is a FHIR web application written in Python and using Flask as the backend. It can only be used for synthea datasets.
4+
5+
Following are the server port number and their respective datasets
6+
7+
```
8+
9090 -> synthea10 (demo dataset)
9+
9091 -> synthea_ri_adult
10+
9092 -> synthea_ri_peds
11+
```
12+
13+
## 1. Dependencies
14+
This app depends on Python 3 and a few Python packages outlined in the `requirements.txt` file.
15+
16+
## 2. Before running the app
17+
18+
Change these parameters in the .env file
19+
20+
```
21+
FHIR_USERNAME = ???
22+
FHIR_PASSWORD = ???
23+
FHIR_PORT=5000
24+
```
25+
26+
and change the port number according to the dataset in the app.py file
27+
FHIR_SERVER_BASE_URL= "http://pwebmedcit.services.brown.edu:????/fhir"
28+
29+
## 3. Running the App
30+
31+
We begin by creating a Python virtual environment using the `venv` module. This is done by running the command below from the "root" of this repo.
32+
33+
```
34+
python3 -m venv venv # create a virtual environment called venv
35+
36+
source venv/bin/activate # activate our virtual environment
37+
38+
pip3 install -r requirements.txt # install all our dependencies into the virtual environment
39+
```
40+
41+
42+
After the above steps, we should be able to launch our app using the following command.
43+
44+
```
45+
python3 src/app.py
46+
```
47+
48+
49+
This will start the app on port 5000. You can open your preferred browser and see the app running on `http://localhost:5000`
50+
51+

requirements.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
blinker==1.6.2
2+
certifi==2023.7.22
3+
charset-normalizer==3.2.0
4+
click==8.1.7
5+
Flask==2.3.3
6+
idna==3.4
7+
importlib-metadata==6.8.0
8+
itsdangerous==2.1.2
9+
Jinja2==3.1.2
10+
MarkupSafe==2.1.3
11+
python-dotenv==1.0.0
12+
requests==2.31.0
13+
urllib3==2.0.5
14+
Werkzeug==2.3.7
15+
zipp==3.17.0

src/app.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import requests
2+
import os
3+
from flask import Flask, request, render_template
4+
from dotenv import load_dotenv
5+
6+
7+
app = Flask(__name__)
8+
9+
FHIR_SERVER_BASE_URL="http://pwebmedcit.services.brown.edu:9091/fhir"
10+
11+
load_dotenv()
12+
13+
username = os.getenv("FHIR_USERNAME")
14+
password = os.getenv("FHIR_PASSWORD")
15+
16+
17+
def request_patient(patient_id, credentials):
18+
19+
req = requests.get(FHIR_SERVER_BASE_URL + "/Patient/" + str(patient_id), auth = credentials)
20+
21+
print(f"Requests status: {req.status_code}")
22+
23+
response = req.json()
24+
print(response.keys())
25+
26+
return response
27+
28+
def search_patients_by_condition(condition_id, credentials):
29+
# Search for patients with a specific condition
30+
search_url = f"{FHIR_SERVER_BASE_URL}/Condition?code={condition_id}"
31+
req = requests.get(search_url, auth=credentials)
32+
33+
if req.status_code == 200:
34+
conditions = req.json()['entry']
35+
patient_ids = [entry['resource']['subject']['reference'].split('/')[-1] for entry in conditions]
36+
patients = [request_patient(patient_id, credentials) for patient_id in patient_ids]
37+
# Convert patient details into set of tuples to ensure uniqueness
38+
unique_patients = set((patient['id'], patient['name'][0]['given'][0], patient['name'][0]['family'], patient['gender'], patient['birthDate']) for patient in patients)
39+
40+
total_patients = len(unique_patients)
41+
return {'unique_patients': unique_patients, 'total_patients': total_patients}
42+
else:
43+
return None
44+
45+
46+
47+
48+
49+
@app.route('/', methods=['GET', 'POST'])
50+
def index():
51+
result = None
52+
credentials = (username, password)
53+
54+
if request.method == 'POST':
55+
try:
56+
condition_id = request.form['condition_id']
57+
result = search_patients_by_condition(condition_id, credentials)
58+
except ValueError:
59+
result = 'Invalid input. Please enter a valid condition ID.'
60+
61+
return render_template('index.html', result=result)
62+
63+
64+
if __name__ == '__main__':
65+
port_str = os.environ['FHIR_PORT']
66+
port_int = int(port_str)
67+
app.run(debug=True, port=port_int)

src/templates/index.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Patient Finder for a given condition_id</title>
7+
</head>
8+
<body>
9+
<div class=container>
10+
<h1>Patient Finder for a given condition_id</h1>
11+
12+
<form method="post">
13+
<label for="condition_id">Enter a condition ID:</label>
14+
<input type="text" name="condition_id" id="condition_id">
15+
<input type="submit" value="Search">
16+
</form>
17+
18+
{% if result %}
19+
<h3>Total Patients: {{ result['total_patients'] }}</h3>
20+
<h3>Results:</h3>
21+
<ul>
22+
{% for patient in result['unique_patients'] %}
23+
<li>
24+
<p>Patient ID: {{ patient[0] }}</p>
25+
<p>First Name: {{ patient[1] }}</p>
26+
<p>Last Name: {{ patient[2] }}</p>
27+
<p>Gender: {{ patient[3] }}</p>
28+
<p>Birth Date: {{ patient[4] }}</p>
29+
</li>
30+
{% endfor %}
31+
</ul>
32+
{% endif %}
33+
34+
35+
</div>
36+
</body>
37+
</html>
38+
39+
40+
<style>
41+
.container {
42+
display: grid;
43+
place-items: center;
44+
}
45+
</style>

0 commit comments

Comments
 (0)