Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from fhir.resources.observation import Observation
|
3 |
+
from datetime import datetime
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
|
7 |
+
# FHIR server endpoint
|
8 |
+
base_url = "https://<fhir_server_endpoint>/fhir/"
|
9 |
+
|
10 |
+
# Function to create an Observation resource
|
11 |
+
def create_observation_resource(activity, category, calories, duration, distance, heart_rate):
|
12 |
+
observation = Observation()
|
13 |
+
observation.status = "final"
|
14 |
+
observation.category = [{"coding": [{"system": "http://terminology.hl7.org/CodeSystem/observation-category",
|
15 |
+
"code": "activity"}]}]
|
16 |
+
observation.code = {"coding": [{"system": "http://loinc.org", "code": "55409-7", "display": "Exercise tracking panel"}]}
|
17 |
+
observation.effectiveDateTime = datetime.now().isoformat()
|
18 |
+
observation.component = []
|
19 |
+
if activity:
|
20 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "73985-4", "display": "Exercise activity"}]}, "valueString": activity})
|
21 |
+
if category:
|
22 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "73986-2", "display": "Exercise aerobic category"}]}, "valueString": category})
|
23 |
+
if calories:
|
24 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "55421-2", "display": "Calories burned Machine estimate"}]}, "valueQuantity": {"value": calories, "unit": "kcal"}})
|
25 |
+
if duration:
|
26 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "55411-3", "display": "Exercise duration"}]}, "valueQuantity": {"value": duration, "unit": "min"}})
|
27 |
+
if distance:
|
28 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "55412-1", "display": "Exercise distance unspecified time"}]}, "valueQuantity": {"value": distance, "unit": "[mi_us];km"}})
|
29 |
+
if heart_rate:
|
30 |
+
observation.component.append({"code": {"coding": [{"system": "http://loinc.org", "code": "55422-0", "display": "Heart rate Encounter maximum"}]}, "valueQuantity": {"value": heart_rate, "unit": "{beats}/min"}})
|
31 |
+
return observation
|
32 |
+
|
33 |
+
# Function to create an Observation resource on the FHIR server
|
34 |
+
def create_observation(observation):
|
35 |
+
headers = {"Content-Type": "application/fhir+json"}
|
36 |
+
response = requests.post(base_url + "Observation", data=json.dumps(observation.as_json()), headers=headers)
|
37 |
+
if response.status_code == 201:
|
38 |
+
st.success("Observation created successfully!")
|
39 |
+
else:
|
40 |
+
st.error("Error creating Observation.")
|
41 |
+
|
42 |
+
# Streamlit user interface
|
43 |
+
def app():
|
44 |
+
st.title("LOINC Exercise Tracking Panel")
|
45 |
+
activity = st.text_input("Exercise activity")
|
46 |
+
category = st.selectbox("Exercise aerobic category", ["", "Aerobic", "Anaerobic"])
|
47 |
+
calories = st.number_input("Calories burned (Machine estimate)", min_value=0, step=1)
|
48 |
+
duration = st.number_input("Exercise duration (minutes)", min_value=0, step=1)
|
49 |
+
distance = st.number_input("Exercise distance unspecified time", min_value=0, step=0.01)
|
50 |
+
heart_rate = st.number_input("Heart rate Encounter maximum", min_value=0, step=1)
|
51 |
+
|
52 |
+
if st.button("Record Exercise"):
|
53 |
+
observation = create_observation_resource(activity, category, calories, duration, distance, heart_rate)
|
54 |
+
create_observation(observation)
|
55 |
+
|