Skip to main content

Python API for LabKey Server

pypi version

Lets you query, insert, and update data on a LabKey Server using Python.

Features

The following APIs can be used against a LabKey Server instance.

Query API - docs

  • delete_rows() - Delete records in a table.
  • execute_sql() - Execute SQL (LabKey SQL dialect) through the query module.
  • get_queries() - List the queries available in a schema.
  • import_rows() - Bulk insert or merge rows into a table from a file.
  • insert_rows() - Insert rows into a table.
  • save_rows() - Insert, update, and delete rows across multiple tables in one request.
  • select_rows() - Query and get results sets.
  • update_rows() - Update rows in a table.
  • move_rows() - Move rows to another container.
  • truncate_table() - Delete all rows from a table.

Domain API - sample code

  • create() - Create many types of domains (e.g. lists, datasets).
  • drop() - Delete a domain.
  • get() - Get a domain design.
  • infer_fields() - Infer fields for a domain design from a file.
  • save() - Save changes to a domain design.
  • conditional_format() - Create a conditional format on a field.

Experiment API - docs

  • load_batch() - Retrieve assay data (batch level).
  • save_batch() - Save assay data (batch level).
  • save_batches() - Save assay data for multiple batches in one request.
  • import_run() - Import a single assay run from data rows or a file.
  • lineage() - Query the experiment lineage graph. See docs.

Security API - sample code

  • Available for administrating and configuring user accounts and permissions.

Storage API - docs

  • Create, update, or delete a LabKey Freezer Manager storage item.

WebDav - docs

  • Convenience methods for creating "webdavclient3" clients and building webdav file paths.

Installation

To install, simply use pip:

$ pip install labkey

Note: For users who installed this package before it was published to PyPI (before v0.3.0) it is recommended you uninstall and reinstall the package rather than attempting to upgrade. This is due to a change in the package's versioning semantics.

Credentials

Set Up a netrc File

On a Mac, UNIX, or Linux system the netrc file should be named .netrc (dot netrc) and on Windows it should be named _netrc (underscore netrc). The file should be located in your home directory and the permissions on the file must be set so that you are the only user who can read it, i.e. it is unreadable to everyone else.

To create the netrc on a Windows machine, first create an environment variable called ’HOME’ that is set to your home directory (for example, C:/Users/johndoe) or any directory you want to use.

In that directory, create a text file with the prefix appropriate to your system, either an underscore or dot.

The following three lines must be included in the file. The lines must be separated by either white space (spaces, tabs, or newlines) or commas:

machine <remote-instance-of-labkey-server>
login <user-email>
password <user-password>

For example:

machine mymachine.labkey.org
login user@labkey.org
password mypassword

Note that the netrc file only deals with connections at the machine level and should not include a port or protocol designation, meaning both "mymachine.labkey.org:8888" and "https://mymachine.labkey.org" are incorrect.

Old credentials

As of v0.4.0 this API no longer supports using a .labkeycredentials.txt file, and now uses the .netrc files similar to the other labkey APIs. Additional .netrc setup instructions can be found at the link.

Examples

Sample code is available in the samples directory.

The following gets data from the Users table on your local machine:

from labkey.api_wrapper import APIWrapper

print("Create an APIWrapper")
labkey_server = 'localhost:8080'
# in this example, Tutorials is a project name and HIV study is a subfolder under it.
container_path = 'Tutorials/HIV Study'
contextPath = 'labkey'
schema = 'core'
table = 'Users'

# Note: If developing against localhost with https disabled, set use_ssl=False below
api = APIWrapper(labkey_server, container_path, contextPath)

# Makes an API request to https://www.example.com/labkey/tutorials/hiv%20study/query-getQuery.api
result = api.query.select_rows(schema, table)

if result is not None:
    print(result['rows'][0])
    print("select_rows: Number of rows returned: " + str(result['rowCount']))
else:
    print('select_rows: Failed to load results from ' + schema + '.' + table)

Supported Versions

Python 3.11+ is fully supported. LabKey Server v15.1 and later.

Contributing

This package is maintained by LabKey. If you have any questions or need support, please use the LabKey Server developer support forum.

Setup

We use the Just command runner to simplify setup and development with this package. You'll want to install just to work on this package. Installation instructions can be found here: https://just.systems/man/en/packages.html

To install the necessary dependencies for local development you can run the following command:

just install

Formatting your code

When contributing changes please use Black to format your code. To run Black you can run the following command:

black .

After black has run, it may have formatted some files, commit the changed files before opening a PR.

Testing

If you are looking to contribute please run the tests before issuing a PR.

To run only the unit tests, run the following command:

just test-unit

To run the integration tests, make sure you have a live server running, a netrc file, and run the following command:

just test-integration

To run all tests, run the following command:

just test

Maintainers

Package maintainer's can reference the Python Package Maintenance document (requires permission) for updating releases.

To build the package before releasing you will need to install the build dependencies. This can be done by running:

just install-build

To build the package you can run:

just build

You should now have a dist/ folder with two files:

  1. labkey-<version from __init__.py>.tar.gz - This is the source distribution
  2. labkey-<version from __init__.py>-py3-none-any.whl - This is the wheel

Release files for labkey 4.4.0

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

Source distribution (sdist)

Source distribution for labkey 4.4.0
File Size Uploaded
labkey-4.4.0.tar.gz 31.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for labkey 4.4.0
File Interpreter ABI Platform
labkey-4.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 70.6 kB

Release files / labkey-4.4.0.tar.gz

Download URL labkey-4.4.0.tar.gz
Size 31.2 kB
Tags Source
SHA-256 checksum
How to use checksums
14fd717ef7969275858a8866e1540eae1ebba954fc301b2dec4ade0b45f10f58
BLAKE2b-256 checksum
How to use checksums
2ef5ab68635592a82e0ac9626c6137c56421c0dc9329651e59718e2680db136c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.4 cpython/3.12.3 HTTPX/0.28.1

Release files / labkey-4.4.0-py3-none-any.whl

Download URL labkey-4.4.0-py3-none-any.whl
Size 39.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09f146c8380f076bcbea12182ee921b1caaa5d6ce0eb113c0e769ad3e2e2dacf
BLAKE2b-256 checksum
How to use checksums
12754386a9757c342595591c082566772bed7029768348901335f4621d1fbb26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.4 cpython/3.12.3 HTTPX/0.28.1

Release history Release notifications | RSS feed

This release

4.4.0 This release

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

1 release file

2.3.0

2 release files

2.2.0

1 release file

2.1.1

1 release file

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

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