pyxnat

1.6.4 · active · verified Thu Apr 16

pyxnat is an open source, BSD-licenced library providing a programmatic interface with XNAT, an extensible management system for imaging data and related information. It uses the RESTful Web services provided by XNAT to enable easier interaction through a simple and consistent API using Python. The library is actively maintained, with the latest version 1.6.4 released as a maintenance and compatibility update, typically seeing several minor releases per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an XNAT server using the `Interface` class and then retrieve a list of available projects. It uses environment variables for secure credential handling.

import os
from pyxnat import Interface

XNAT_HOST = os.environ.get('XNAT_HOST', 'https://central.xnat.org')
XNAT_USER = os.environ.get('XNAT_USER', '')
XNAT_PASSWORD = os.environ.get('XNAT_PASSWORD', '')

if not (XNAT_USER and XNAT_PASSWORD): 
    print("Please set XNAT_USER and XNAT_PASSWORD environment variables.")
    print("Or set XNAT_HOST if not using central.xnat.org.")
else:
    try:
        # Connect to the XNAT instance
        interface = Interface(server=XNAT_HOST, user=XNAT_USER, password=XNAT_PASSWORD)
        print(f"Successfully connected to {XNAT_HOST} as {XNAT_USER}")

        # List projects
        projects = interface.select.projects().get()
        print("\nAvailable projects:")
        for project_id in projects:
            print(f"- {project_id}")

        # Disconnect
        interface.disconnect()
        print("\nDisconnected from XNAT.")

    except Exception as e:
        print(f"Error connecting to XNAT or listing projects: {e}")

view raw JSON →