awacke1 commited on
Commit
06b5ab1
·
1 Parent(s): 3ad6153

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py CHANGED
@@ -139,3 +139,131 @@ if query:
139
  fhir_client.create(exercise_questionnaire_response)
140
 
141
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  fhir_client.create(exercise_questionnaire_response)
140
 
141
  """)
142
+
143
+
144
+ st.markdown("""
145
+ from hl7apy.parser import parse_message
146
+ from fhirpy import SyncFHIRClient
147
+ from fhirpy.base.exceptions import OperationOutcome
148
+ from fhir.resources import Bundle, Patient, Observation
149
+ from fhirclient.models.fhirreference import FHIRReference
150
+ from fhirclient.models.codeableconcept import CodeableConcept
151
+ from fhirclient.models.fhirsearch import FHIRSearch
152
+ from fhirclient.models.observation import Observation as FhirObservation
153
+ from fhirclient.models.questionnaire import QuestionnaireResponse as FhirQuestionnaireResponse
154
+ from fhirclient.models.fhirabstractbase import FHIRValidationError
155
+ import webbrowser
156
+
157
+ # Set up the FHIR server base URL
158
+ fhir_server_url = "https://fhirtest.uhn.ca/baseDstu3"
159
+
160
+ # Set up the SMART on FHIR launch URL
161
+ smart_launch_url = "https://launch.smarthealthit.org/v/r4/sim/eyJhIjoiMSIsImYiOiI5LjUuMTQwMDkiLCJlIjoiMi4wLjAiLCJzIjoibmV3LXNzbCIsInQiOiJkYXRhc2V0In0/fhir"
162
+
163
+ # Define the LOINC code for the test observation
164
+ test_observation_loinc = "29463-7"
165
+
166
+ # Define the Exercise Assessment questionnaire ID
167
+ exercise_questionnaire_id = "exercise-questionnaire"
168
+
169
+ # Define the Exercise Assessment questionnaire response ID prefix
170
+ exercise_response_prefix = "exercise-response"
171
+
172
+ # Define the Exercise Assessment observation code
173
+ exercise_observation_code = "8867-4"
174
+
175
+ # Define the SMART on FHIR launch parameters
176
+ smart_launch_params = {
177
+ "iss": fhir_server_url,
178
+ "launch": "12345",
179
+ "patient": "Patient/123",
180
+ "scope": "patient/*.read",
181
+ "aud": "https://example.com/fhir"
182
+ }
183
+
184
+ # Create a FHIR client
185
+ client = SyncFHIRClient(fhir_server_url)
186
+
187
+ # Receive an HL7 v2.x message
188
+ hl7_message = b"MSH|^~\&|HIS|FHIRCLIENT|HIS|FHIRCLIENT|20230101010101||ORU^R01|123|P|2.5.1||||||||\nPID|||1234^^^^MR||DOE^JOHN|||||||||||||||\nOBR|1|1234||^^^29463-7^GLU^L|||20230101010101|||||||||||||||||||||||||||||\nOBX|1|ST|29463-7^GLU^L|1|100|mg/dL|||||F\n"
189
+ message = parse_message(hl7_message)
190
+
191
+ # Convert the HL7 v2.x message to FHIR resources
192
+ patient = Patient(
193
+ id=message.pid.pid_3.value,
194
+ name=[{"given": [message.pid.pid_5.value.split("^")[1]], "family": message.pid.pid_5.value.split("^")[0]}],
195
+ birthDate=message.pid.pid_7.value
196
+ )
197
+ observation = Observation(
198
+ code=CodeableConcept(
199
+ coding=[{"system": "http://loinc.org", "code": test_observation_loinc, "display": "GLUCOSE"}],
200
+ text="Glucose"
201
+ ),
202
+ valueQuantity={
203
+ "value": float(message.obx[0].obx_5.value),
204
+ "unit": message.obx[0].obx_6.value
205
+ ),
206
+ subject=FHIRReference({"reference": f"Patient/{patient.id}"})
207
+ )
208
+
209
+ Create a bundle with the Patient and Observation resources
210
+ bundle = Bundle(type="collection", entry=[{"resource": patient}, {"resource": observation}])
211
+
212
+ Save the bundle to the FHIR server
213
+ try:
214
+ response = client.create(bundle)
215
+ st.write("Observation saved to FHIR server:")
216
+ st.json(response.as_json())
217
+ except OperationOutcome as error:
218
+ st.write("Error saving observation to FHIR server:")
219
+ st.json(error.as_json())
220
+
221
+ Render the Exercise Assessment using the FHIR resources
222
+ exercise_questionnaire_response_id = f"{exercise_response_prefix}-{observation.code.coding[0].code}"
223
+ exercise_questionnaire_response = FhirQuestionnaireResponse(
224
+ questionnaire=FHIRReference(f"Questionnaire/{exercise_questionnaire_id}"),
225
+ status="in-progress",
226
+ subject=FHIRReference({"reference": f"Patient/{patient.id}"})
227
+ )
228
+ exercise_questionnaire_response.identifier = [{"value": exercise_questionnaire_response_id}]
229
+ exercise_questionnaire_response.item = [
230
+ {
231
+ "linkId": "1",
232
+ "text": "How many minutes of aerobic exercise did you do today?",
233
+ "type": "integer"
234
+ },
235
+ {
236
+ "linkId": "2",
237
+ "text": "How many minutes of strength training did you do today?",
238
+ "type": "integer"
239
+ }
240
+ ]
241
+
242
+ Save the Exercise Assessment to the FHIR server
243
+ try:
244
+ response = client.create(exercise_questionnaire_response)
245
+ st.write("Exercise Assessment saved to FHIR server:")
246
+ st.json(response.as_json())
247
+ except (OperationOutcome, FHIRValidationError) as error:
248
+ st.write("Error saving Exercise Assessment to FHIR server:")
249
+ st.json(error.as_json())
250
+
251
+ Generate the SMART on FHIR launch URL with launch parameters
252
+ smart_launch_url_with_params = f"{smart_launch_url}?{'&'.join([f'{key}={value}' for key, value in smart_launch_params.items()])}"
253
+
254
+ Display the SMART on FHIR launch URL
255
+ st.write("SMART on FHIR launch URL:")
256
+ st.write(smart_launch_url_with_params)
257
+
258
+ Open the SMART on FHIR UI in a web browser
259
+ webbrowser.open(smart_launch_url_with_params)
260
+
261
+ This program receives an HL7 v2.x message, converts it to FHIR resources (a Patient and an Observation), saves the resources to the FHIR server, and then renders the Exercise Assessment questionnaire using the saved resources. The program then saves the Exercise Assessment to the FHIR server and generates a SMART on FHIR launch URL with launch parameters. Finally, the program displays the launch URL and opens the SMART on FHIR UI in a web browser.
262
+
263
+ Note that in order to run this program, you'll need to have the `hl7apy`, `fhirpy`, `fhir.resources`, `fhirclient`, and `webbrowser` packages installed, and you'll need to update the `fhir_server_url`, `smart_launch_url`, `test_observation_loinc`, `exercise_questionnaire_id`, and `exercise_response_prefix` variables to match your environment.
264
+
265
+
266
+
267
+
268
+ """)
269
+