Overview

LabKey's Python APIs allow you to query, insert and update data on a LabKey Server from Python. They provide functionality similar to the following LabKey Javascript APIs:

  • LABKEY.Query.selectRows()
  • LABKEY.Query.executeSql()
  • LABKEY.Query.insertRows()
  • LABKEY.Query.updateRows()
  • LABKEY.Query.deleteRows()
In addition, it allows you to programmatically update wikis and post messages to the LabKey Server.

Supported Functions

  • labkey.query.selectRows()
  • labkey.query.executeSql()
  • labkey.query.insertRows()
  • labkey.query.updateRows()
  • labkey.query.deleteRows()
  • labkey.wiki.updateWiki()
  • labkey.messageboard.postMessage

Free, Hosted Test Server:

The sample login information provided above for the hosted.labkey.com server provides you with read access only. This level of access is sufficient for only selectRows and executeSql. To test the other APIs on this server, please contact us. LabKey Software will happily provide you with a private, free project on this server to use for testing.

Further details: http://www.labkey.com/hosted/labkey-hosted

selectRows()

selectRows() can be used to query data from LabKey Server

The following are the minimum required params:

myresults = labkey.query.selectRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam')

The following are optional parameters:

viewName => 'view1',
filterArray => [
['ParticipantID', 'eq', 249318596],
['Pulse', 'gt', '0']
],
maxRows = 10, #the max number of rows returned
sort = 'ColumnA,ColumnB', #sort order used for this query
offset = 100, #the offset used when running the query
columns = 'ColumnA,ColumnB', #A comma-delimited list of column names to include in the results.
containerFilter = 'currentAndSubfolders',
debug = True #will result in a more verbose output

Test code:

import labkey
myresults = labkey.query.selectRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam',
maxRows = 2,
filterArray = [
['ParticipantID', 'eq', 249318596],
['Pulse', 'gt', '0']],
debug = True)

myresults = labkey.query.selectRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
debug = True)


executeSql()

executeSql() can be used to execute arbitrary SQL

The following are the minimum required params:

myresults = labkey.query.executeSql(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
sql = 'SELECT * FROM "Physical Exam"')

The following are optional:

maxRows = 10,	#the max number of rows returned
sort = 'ColumnA,ColumnB', #sort order used for this query
offset = 100, #the offset used when running the query
containerFilter = 'currentAndSubfolders',
debug = True #will result in a more verbose output

Test Code:

import labkey
myresults = labkey.query.executeSql(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
sql = 'SELECT "Physical Exam".ParticipantId, "Physical Exam".Pulse FROM "Physical Exam" WHERE "Physical Exam".ParticipantId.ParticipantId='249318596'',
maxRows = 4,
debug = True)

Reminder:

In Python, if there are ' or characters in string arguments, the characters must be escaped as ' and
-- see the example above for '


insertRows()

insertRows() can be used to insert records into a LabKey table

The following are the minimum required params:

myresults = labkey.query.insertRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam',
rows =[{'DiastolicBloodPressure': 90,
'Language': 'English',
'ParticipantId': '249325718', #The combo of ParticipantId + date must be unique
'Pregnancy': '0',
'Pulse': 77,
'Respirations': 13,
'Signature': 0,
'SystolicBloodPressure': 137,
'Temp_C': 38,
'Weight_kg': 111,
'date': '21 May 2008 00:00:00'}])

The following are optional:

debug = True #will result in a more verbose output

Test Code:

import labkey
myresults = labkey.query.insertRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam',
rows = [{'DiastolicBloodPressure': 90,
'Language': 'English',
'ParticipantId': '249325728', #The combo of ParticipantId + date must be unique
'Pregnancy': '0',
'Pulse': 77,
'Respirations': 13,
'Signature': 0,
'SystolicBloodPressure': 137,
'Temp_C': 38,
'Weight_kg': 111,
'date': '21 May 2008 00:00:00'}],
debug = True)


myresults = labkey.query.insertRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
rows = [{'ContactPerson': 'Elizabeth',
'InstrumentID': '7', # The key for each inserted row must be unique
'Name': 'HAL'}],
debug = True)


updateRows()

updateRows() can be used to insert records into a LabKey table

The following are the minimum required params:

myresults = labkey.query.updateRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
rows = [{'InstrumentID': '10', #This is the key - it's required
'Name': 'HAL'}]) #This is the update you wish to execute

The following are optional:

debug = True #will result in a more verbose output

Test Code:

import labkey
myresults = labkey.query.updateRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam',
rows = [{'ParticipantId': '249325717',
'SystolicBloodPressure': 1390,
'lsid': 'urn:lsid:labkey.com:Study.Data-173:5004.2.0080427E7.249325717'}],
debug = True)

myresults = labkey.query.updateRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
rows = [{'ContactPerson': 'Elizabeth',
'InstrumentID': '10', #This is the key
'Name': 'HAL'}], #This is the update
debug = True)


deleteRows()

deleteRows() can be used to delete records into a LabKey table

The following are the minimum required params:

myresults = labkey.query.deleteRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
rows = [{'InstrumentID': '10'}]) #The key for each row is required.

The following are optional:

debug = True #will result in a more verbose output

Test Code:

import labkey
myresults = labkey.query.deleteRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'study',
queryName = 'Physical Exam',
rows = [{'lsid': 'urn:lsid:labkey.com:Study.Data-290:5004.2.0080427E7.249325717'}],
debug = True)

myresults = labkey.query.deleteRows(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
schemaName = 'lists',
queryName = 'Lab Machines',
rows = [{'InstrumentID': '10'}], #Make sure this exists
debug = True)


postMessage()

postMessage() can be used to post a message to a message board on the LabKey Server

The following are the minimum required params:

myresults = labkeyApi.postMessage(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
messageTitle = 'Message Title',
messageBody = 'This is the content of my message ....',
renderAs = 'HTML')

The following are optional:

debug = True #will result in a more verbose output

The function will return the integer 0 for success and the integer 1 if the message post fails

Test Code:

[NOT DONE]


updateWiki()

updateWiki() can be used to update an existing wiki page

The following are the minimum required params:

myresults = labkeyApi.updateWiki(
baseUrl = 'https://hosted.labkey.com',
containerPath = 'PythonProject',
wikiName = 'MyWiki',
messageBody = 'New Content for my wiki')

The following are optional:

debug = True #will result in a more verbose output

This API does not support the ability to change the Render Type for the wiki to be updated.

This API returns a dictionary containing the response from the server. The 'success' key in the dictionary will be true when the wiki was successfully updated. It will be false in the case of a failure. In the case of a failure, the 'error' key contains the error message returned by the server.

Test Code:

[NOT DONE]


previousnext
 
expand allcollapse all