Skip to main content

Note: Forked changes:

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'>
Validation
    def testDictWithBadValue(self):
        """Should not be able to create ResearchStudy dict with unknown value."""
        with self.assertRaises(FHIRValidationError) as err:
            ResearchStudy({'status': 'foo-bar'})
        expected_msg = 'Expecting property with required binding_strength "status" to be member of http://hl7.org/fhir/research-study-status is "foo-bar"'
        assert expected_msg in str(err.exception), self.testDictWithBadValue.__doc__
Simplified Projection

as_simplified_json() is available on all classes that inherit from FHIRAbstractBase. It will remove much of the FHIR scaffolding. Simplifies:

  • extensions
  • single_item_lists
  • codings

Returns -> tuple. A dict object with much of the FHIR scaffolding removed; A corresponding lite schema dict.


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

rm -r dist/ ; python3  setup.py sdist bdist_wheel
twine upload dist/*

Incrementing the lib version

bumpversion patch
bumpversion minor
bumpversion major

Note: if unable to use bumpversion , manually set fhirclient/client.py

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

Credits

“fhirclient” is written and maintained by the SMART Platforms Team / Boston Children's Hospital.

Contributors

The following wonderful people contributed directly or indirectly to this project:

Please add yourself here alphabetically when you submit your first pull request.

Metadata

Release files for fhirclientr4e 4.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fhirclientr4e 4.0.8
File Size Uploaded Provenance Actions
fhirclientr4e-4.0.8.tar.gz 671.1 kB Without provenance Details for fhirclientr4e-4.0.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fhirclientr4e 4.0.8
File Interpreter ABI Platform Actions
fhirclientr4e-4.0.8-py2.py3-none-any.whl Python 2, Python 3 none any Details for fhirclientr4e-4.0.8-py2.py3-none-any.whl

Total release size: 1.7 MB

Release files / fhirclientr4e-4.0.8.tar.gz

Download URL fhirclientr4e-4.0.8.tar.gz
Size 671.1 kB
Tags Source
SHA-256 checksum
How to use checksums
931875c34753f46d97373631bc255dd93e8c1fd461f7be89220a3cab17bcf266
BLAKE2b-256 checksum
How to use checksums
273c511fcb504ce7074916f7873866070c4d6ffddac49ecdf59969497a1702d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release files / fhirclientr4e-4.0.8-py2.py3-none-any.whl

Download URL fhirclientr4e-4.0.8-py2.py3-none-any.whl
Size 1.0 MB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4a642318287e805025ec98b552f7bdb76dffc9b33089de905de6337b61d239b4
BLAKE2b-256 checksum
How to use checksums
305ddc094a9c21072a6c5a0a073e605227abc36e8a608c62b71e8e16dd75d0d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Security for fhirclientr4e 4.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page