Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

py-fhir

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

Introduction

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="http://hl7.org/fhir/v2/0203", code="MR")]),
    system='urn:oid:1.2.36.146.595.217.0.1',
    value='123456789'
)

# 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
p.active = True
print(type(p.active))
# output: <class 'fhir.model._boolean.boolean'>

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

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

This should yield the following output:

{
    "resourceType": "Patient",
    "id": "patient1",
    "identifier": [
        {
            "type": {
                "coding": [
                    {
                        "system": "http://hl7.org/fhir/v2/0203",
                        "code": "MR"
                    }
                ]
            },
            "system": "urn:oid:1.2.36.146.595.217.0.1",
            "value": "123456789"
        }
    ],
    "active": true,
    "name": [
        {
            "use": "official",
            "family": [
                "Sieswerda"
            ],
            "given": [
                "Melle",
                "Sjoerd"
            ]
        }
    ]
}

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

jsonstring = p.dumps('json')
p2 = Patient.loads(jsonstring, 'json')
p2.id == p.id
# 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)
client.set_properties(fhir.model.Resource)

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

# Print the first result we received
for p in bundle:
    print(p.dumps('json'))
    break

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

name = HumanName(
	use='official',
	given=['Melle'],
	family='Testpatient'
)
p.name = [name]

p.save()
print('The server assigned id "{}"'.format(p.id))

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()
p.id = 'patient1'
p.active = True

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

# Store the patient
store.post(p)

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

Metadata

Release files for py-fhir 0.1.1a5

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

Source distribution (sdist)

Source distribution for py-fhir 0.1.1a5
File Size Uploaded
py-fhir-0.1.1a5.tar.gz 81.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-fhir 0.1.1a5
File Interpreter ABI Platform
py_fhir-0.1.1a5-py3-none-any.whl Python 3 none any Details

Total release size: 242.2 kB

Release files / py-fhir-0.1.1a5.tar.gz

Download URL py-fhir-0.1.1a5.tar.gz
Size 81.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a5d419ad85712c48dede7e5f08be9543d8caa29260487c6057698e80e21a2b16
BLAKE2b-256 checksum
How to use checksums
a141085656029bba8b951969946c43a82bb07319b5b2f362e97eb98f851b08d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release files / py_fhir-0.1.1a5-py3-none-any.whl

Download URL py_fhir-0.1.1a5-py3-none-any.whl
Size 161.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e47986a8ae61b59bbc05b235c92ac95fc7d256bd95845dfab1c4263e8458b18
BLAKE2b-256 checksum
How to use checksums
ea68b9849f026ca5b99adfd3f270badffb9d42f97ead213dec0e16902ebaae51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.1.1a5 This release

2 release files

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