• Stars
    star
    588
  • Rank 76,022 (Top 2 %)
  • Language
    Python
  • License
    Other
  • Created about 10 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python SMART on FHIR client

SMART FHIR Client

This is fhirclient, a flexible Python client for FHIR servers supporting the SMART on FHIR protocol. The client is compatible with Python 2.7.10 and Python 3.

Client versioning is not identical to FHIR versioning. The master branch is usually on the latest version of the client as shown below, possibly on bugfix releases thereof. The develop branch should be on recent freezes, and the feature/latest-ci branch is periodically updated to the latest FHIR continuous integration builds.

Version FHIR Β 
4.0.0 4.0.0 (R4)
3.0.0 3.0.0 (STU-3)
x.x 1.8.0 (STU-3 Ballot, Jan 2017)
x.x 1.6.0 (STU-3 Ballot, Sep 2016)
1.0.3 1.0.2 (DSTU 2)
1.0 1.0.1 (DSTU 2)
0.5 0.5.0.5149 (DSTU 2 Ballot, May 2015)
0.0.4 0.0.82.2943 (DSTU 1)
0.0.3 0.0.82.2943 (DSTU 1)
0.0.2 0.0.82.2943 (DSTU 1)

Installation

pip install fhirclient

Documentation

Technical documentation is available at docs.smarthealthit.org/client-py/.

Client Use

To connect to a SMART on FHIR server (or any open FHIR server), you can use the FHIRClient class. It will initialize and handle a FHIRServer instance, your actual handle to the FHIR server you'd like to access.

Read Data from Server

To read a given patient from an open FHIR server, you can use:

from fhirclient import client
settings = {
    'app_id': 'my_web_app',
    'api_base': 'https://fhir-open-api-dstu2.smarthealthit.org'
}
smart = client.FHIRClient(settings=settings)

import fhirclient.models.patient as p
patient = p.Patient.read('hca-pat-1', smart.server)
patient.birthDate.isostring
# '1963-06-12'
smart.human_name(patient.name[0])
# 'Christy Ebert'

If this is a protected server, you will first have to send your user to the authorize endpoint to log in. Just call smart.authorize_url to obtain the correct URL. You can use smart.prepare(), which will return False if the server is protected and you need to authorize. The smart.ready property has the same purpose, it will however not retrieve the server's CapabilityStatement resource and hence is only useful as a quick check whether the server instance is ready.

smart = client.FHIRClient(settings=settings)
smart.ready
# prints `False`
smart.prepare()
# prints `True` after fetching CapabilityStatement
smart.ready
# prints `True`
smart.prepare()
# prints `True` immediately
smart.authorize_url
# is `None`

You can work with the FHIRServer class directly, without using FHIRClient, but this is not recommended:

smart = server.FHIRServer(None, 'https://fhir-open-api-dstu2.smarthealthit.org')
import fhirclient.models.patient as p
patient = p.Patient.read('hca-pat-1', smart)
patient.name[0].given
# ['Christy']
Search Records on Server

You can also search for resources matching a particular set of criteria:

smart = client.FHIRClient(settings=settings)
import fhirclient.models.procedure as p
search = p.Procedure.where(struct={'subject': 'hca-pat-1', 'status': 'completed'})
procedures = search.perform_resources(smart.server)
for procedure in procedures:
    procedure.as_json()
    # {'status': u'completed', 'code': {'text': u'Lumpectomy w/ SN', ...

# to include the resources referred to by the procedure via `subject` in the results
search = search.include('subject')

# to include the MedicationAdministration resources which refer to the procedure via `partOf`
import fhirclient.models.medicationadministration as m
search = search.include('partOf', m.MedicationAdministration, reverse=True)

# to get the raw Bundle instead of resources only, you can use:
bundle = search.perform(smart.server)

Data Model Use

The client contains data model classes, built using fhir-parser, that handle (de)serialization and allow to work with FHIR data in a Pythonic way. Starting with version 1.0.5, data model validity are enforced to a certain degree.

Initialize Data Model

import fhirclient.models.patient as p
import fhirclient.models.humanname as hn
patient = p.Patient({'id': 'patient-1'})
patient.id
# prints `patient-1`

name = hn.HumanName()
name.given = ['Peter']
name.family = 'Parker'
patient.name = [name]
patient.as_json()
# prints patient's JSON representation, now with id and name

name.given = 'Peter'
patient.as_json()
# throws FHIRValidationError:
# {root}:
#   name:
#     given:
#       Expecting property "given" on <class 'fhirclient.models.humanname.HumanName'> to be list, but is <class 'str'>

Initialize from JSON file

import json
import fhirclient.models.patient as p
with open('path/to/patient.json', 'r') as h:
    pjs = json.load(h)
patient = p.Patient(pjs)
patient.name[0].given
# prints patient's given name array in the first `name` property

Flask App

Take a look at flask_app.py to see how you can use the client in a simple (Flask) app. This app starts a webserver, listening on localhost:8000, and prompts you to login to our sandbox server and select a patient. It then goes on to retrieve the selected patient's demographics and med prescriptions and lists them in a simple HTML page.

The Flask demo app has separate requirements. Clone the client-py repository, then best create a virtual environment and install the needed packages like so:

git clone https://github.com/smart-on-fhir/client-py.git
cd client-py
virtualenv -p python3 env
. env/bin/activate
pip install -r requirements_flask_app.txt
python flask_app.py

Building Distribution

pip install -r requirements.txt
python setup.py sdist
python setup.py bdist_wheel

Incrementing the lib version

bumpversion patch
bumpversion minor
bumpversion major

Docs Generation

Docs are generated with Doxygen and doxypypy. You can install doxypypy via pip: pip install doxypypy. Then you can just run Doxygen, configuration is stored in the Doxyfile.

Running Doxygen will put the generated documentation into docs, the HTML files into docs/html. Those files make up the content of the gh-pages branch. I usually perform a second checkout of the gh-pages branch and copy the html files over, with:

doxygen
rsync -a docs/html/ ../client-py-web/

PyPi Publishing (notes for SMART team)

Using setuptools (Note: Alternatively, you can use twine https://pypi.python.org/pypi/twine/):

Make sure that you have the PyPi account credentials in your account

copy server.smarthealthit.org:/home/fhir/.pypirc to ~/.pypirc

Test the build

python setup.py sdist
python setup.py bdist_wheel

Upload the packages to PyPi

python setup.py sdist upload -r pypi
python setup.py bdist_wheel upload -r pypi

More Repositories

1

health-cards

Health Cards Framework: implementation guide and supporting material
TypeScript
259
star
2

Swift-FHIR

These are Swift classes for data models of FHIR elements and resources
Swift
159
star
3

fhir-parser

A Python FHIR specification parser and class generator
Python
149
star
4

Swift-SMART

Swift SMART on FHIR framework for iOS and OS X
Swift
125
star
5

smart-dev-sandbox

Docker based sandbox for smart apps
Smarty
116
star
6

api-server

Open-source FHIR Server to support patient- and clinician-facing apps
Groovy
104
star
7

patient-browser

JavaScript
83
star
8

smart-on-fhir.github.io

SMART on FHIR Docs
Jupyter Notebook
77
star
9

fhir-bulk-data-docs

Documentation and issue tracking for the emerging FHIR bulk data implementation guide
Jupyter Notebook
74
star
10

health-cards-dev-tools

Developer tools for validating SMART Health Cards
TypeScript
71
star
11

growth-chart-app

JavaScript
66
star
12

sample-apps-stu3

Collection of simple sample apps
JavaScript
46
star
13

fred

FRED - FHIR Resource Editor
JavaScript
44
star
14

health-cards-tests

Demo for health wallet with Verifiable Credentials and Decentralized Identifiers
TypeScript
43
star
15

bulk-data-server

JavaScript
42
star
16

fhir-server-dashboard

Presents a human-readable representation of the data in a FHIR server.
JavaScript
41
star
17

fhir-viewer

In-browser viewer for FHIR resources
JavaScript
39
star
18

installer

Shell
39
star
19

hapi

HAPI FHIR Server With Sample Patients
Dockerfile
37
star
20

SoF-Demo

Simple SMART on FHIR / Argonaut iOS sample app that fetches a couple of resources
Swift
36
star
21

smart-launcher

Launcher for SMART apps
JavaScript
33
star
22

smart-scheduling-links

Clinical Appointment Slot Discovery
TypeScript
28
star
23

sample-apps

HTML
25
star
24

cardiac-risk-app

JavaScript
25
star
25

generated-sample-data

Contains all the bundles that are loaded into the FHIR server
24
star
26

bulk-data-tools

HTML
23
star
27

smart-examples

Collection of public apps
JavaScript
14
star
28

client-node

SMART client for NodeJS
HTML
13
star
29

bulk-import

12
star
30

bulk-data-implementations

listing of bulk FHIR client implementations
12
star
31

bulk-data-client

JavaScript
11
star
32

tag-uploader

Adds tags to FHIR bundles and resources and uploads them to specified servers
JavaScript
9
star
33

sample-patients-stu3

Python
9
star
34

client-ts

FHIR Client Library
HTML
9
star
35

bp-centiles-app

JavaScript
8
star
36

cumulus-etl

Extract FHIR data, Transform with NLP and DEID tools, and then Load FHIR data into a SQL Database for analysis
Python
8
star
37

diabetes-monograph-app

JavaScript
7
star
38

fhir-starter

HTML
7
star
39

smart-launcher-v2

SMART Launcher
TypeScript
6
star
40

smart-local-sandbox

Docker based sandbox for smart apps
JavaScript
6
star
41

health-cards-designs

5
star
42

smart-hapi-stack

Experimental HAPI stack to support SMART on FHIR
Java
5
star
43

sample-bulk-fhir-datasets

Sample bulk export results of various sizes, for testing tools and workflows
Shell
5
star
44

bdt

Bulk Data Test Suite and Test Runner
TypeScript
5
star
45

sample-patients-prom

Provisional Quarterly Patient Reported Outcome Measures (PROMs) in England - April 2015 to March 2016, November 2016 release
JavaScript
5
star
46

smart-stub

Node / Express server to stub SMART Auth
JavaScript
4
star
47

mpr-monitor-app

JavaScript
4
star
48

dstu2-examples

Copy of official FHIR examples with an index page
HTML
4
star
49

synthea

Static build of Synthea with http interface
JavaScript
4
star
50

hca-fhir-importer

Import synthetic breast cancer sample data, courtesy of HCA/Bill Gregg
Python
4
star
51

fhir-demo-app

ApacheConf
3
star
52

smart-health-card-decoder

Sample code for a SMART Health Card validator
JavaScript
3
star
53

smart-health-links

Static documentation site for SMART Health Links
JavaScript
3
star
54

ehi-app

EHI Export API Client Reference Implementation
TypeScript
3
star
55

ehi-server

An EHI export server reference implementation of Argonaut's EHI Export API IG
TypeScript
3
star
56

bulk-import-client

CLI Client app for Bulk Data Import
JavaScript
2
star
57

confidential-client-example

JavaScript
2
star
58

fiery-sublime

Sublime Text 3 Plugin for FHIR data models
Python
2
star
59

disease-monograph-app

JavaScript
2
star
60

fhir-crawler

Utility to download resources from a FHIR server
TypeScript
1
star
61

fhir-server-tasks

Maintenance web-hooks for FHIR servers
JavaScript
1
star
62

timeshift

Shift dates in FHIR datasets
JavaScript
1
star
63

client-js-examples

Contains examples of how to use the client-js library
HTML
1
star
64

registry

Registry of FHIR Profiles for SMART Platforms
HTML
1
star
65

bulk-import-consumer

Bulk Data Import Server (Data Consumer)
JavaScript
1
star
66

reachnet

JavaScript
1
star
67

cumulus-library-hypertension

Hypertension case definition to support CDC chronic disease management and CMS measures
Python
1
star