Skip to main content

FHIR classes for Python

Project description


FHIR Resources/Elements for Python. Code generated by py-fhir-codegen.


This python package provides a python version of the Resources and Elements specified in the FHIR specification. Additionally, the package includes first steps towards database persistance (using SQLAlchemy) and a client.

How to use Resources and DataTypes

Running the following code creates a Patient and assigns values to different attributes. Finally, the object is serialized to JSON representation and the result is printed.

# Import the relevant classes from fhir.model
from fhir.model import Patient, HumanName, Identifier, CodeableConcept, Coding, uri

# Create a new Patient object. Resource attributes can be passed in the constructor. Note
# that 'id' is the logical id of the Resource and has no relation to the Medical Record
# Number (MRN)
p = Patient(id='patient1')

# Give the patient an MRN
identifier = Identifier(
    type=CodeableConcept(coding=[Coding(system="", code="MR")]),

# Lists can be assigned to attributes.
# Alternatively p.identifier.append(identifier) could have been used.
p.identifier = [identifier]

# Native python values are automatically coerced to FHIR classes = True
# output: <class 'fhir.model._boolean.boolean'>

name = HumanName()
name.use = 'official'
name.given = ['Melle', 'Sjoerd'] = 'Sieswerda' = [name]

# Serialize to JSON and print the result. To serialize to XML use 'p.toXML()'.

This should yield the following output:

    "resourceType": "Patient",
    "id": "patient1",
    "identifier": [
            "type": {
                "coding": [
                        "system": "",
                        "code": "MR"
            "system": "urn:oid:",
            "value": "123456789"
    "active": true,
    "name": [
            "use": "official",
            "family": [
            "given": [

It is also possible to marshall an object from JSON (or XML) representation:

jsonstring = p.dumps('json')
p2 = Patient.loads(jsonstring, 'json') ==
# output: True

Using the client

import fhir.model
import fhir.client
from fhir.model import Patient, HumanName

URL = fhir.client.SERVERS['spark']
client = fhir.client.Client(URL)

# The following is equivalent to
# bundle = client.query('Patient')
bundle = fhir.model.Patient.query()

# Print the first result we received
for p in bundle:

# Create a new patient on the server
p = Patient() = True

name = HumanName(
) = [name]
print('The server assigned id "{}"'.format(

Using persistant storage

Cave: the current implementation should be considered a stub. Searching/querying, updating, etc. is not (yet) supported.

from fhir.persistance import FHIRStore
import fhir.model

# By default FHIRStore creates a sqlite database in the current directory.
# Still, be careful when using 'drop_all'!
store = FHIRStore(drop_all=True)

# Create a new Patient
p = fhir.model.Patient() = 'patient1' = True

name = fhir.model.HumanName(
    given=['Melle', 'Sjoerd'],
) = [name]

# Store the patient

# Retrieve the patient from the store and print the result.
# output: <fhir.model.patient.Patient object at 0x1096dd828>

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-fhir-0.1.1a5.tar.gz (81.0 kB view hashes)

Uploaded source

Built Distribution

py_fhir-0.1.1a5-py3-none-any.whl (161.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page