Workspace Setup
To setup a Workspace on Labstep:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import labstep
# Login
user = labstep.login('myaccount@labstep.com', 'mypassword')
# Create a new Workspace
workspace = user.newWorkspace(name='The Synthesis of Aspirin')
user.setWorkspace(workspace.id)
# Create an Experiment
my_experiment = user.newExperiment(name='Trial 1')
# Upload the reaction scheme
user.newFile('./aspirin_reaction_scheme.png')
# Create a Protocol
my_protocol = user.newProtocol('Aspirin Synthesis')
# Create Resource Category
chemicalCategory = user.newResourceCategory('Chemical')
chemicalCategory.addMetadata(fieldName='Molar mass')
chemicalCategory.addMetadata(fieldName='Density')
chemicalCategory.addMetadata(fieldName='Melting point')
# Add Resources
salicylic_acid = user.newResource('Salicylic Acid')
salicylic_acid.addComment('Here is the chemical structure',
'./salicylic_acid.png')
salicylic_acid.addMetadata(fieldName='Formula', value='C7H6O3')
salicylic_acid.addMetadata(fieldName='Hazards', value='Corrosive, irritant')
# etc...
Importing Resources
To import a list of resources into Labstep from a resource_import.csv file:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import labstep
import pandas
# Login
user = labstep.login('myaccount@labstep.com', 'mypassword')
# Set to an existing workspace
my_workspace = user.newWorkspace('Shared Inventory')
user.setWorkspace(my_workspace.id)
# Read input data
# headers = ['ID', 'Name', 'Location', 'Amount / uL']
data = pandas.read_csv('./resource_import.csv')
# For each entry:
for counter, name in enumerate(data['Name']):
# Create a new Resource for it
new_resource = user.newResource(name)
# Add a new Item and use the 'ID' as the Item name
new_item = new_resource.newItem(name=data['ID'][counter])
# Set the amount
new_item.edit(quantity_amount=data['Amount / uL'][counter],
quantity_unit='uL')
# Set the location
location = my_workspace.getResourceLocations(
search_query=data['Location'][counter])
if len(location) == 0:
location = user.newResourceLocation(data['Location'][counter])
else:
location = location[0]
new_item.edit(resource_location_id=location.id)
Deleting Multiple Entities
You can use labstepPy to easily delete multiple different Entities on Labstep (a list of Experiments, Protocols, or Tags, etc.), either from within a specific Workspace or by performing a global delete.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import labstep
# Login
user = labstep.login('myaccount@labstep.com', 'mypassword')
# Choose a Workspace
my_workspace = user.getWorkspaces(name='Structure of Protein A')[0]
user.setWorkspace(my_workspace.id)
# Delete a list of Tags for 'crystallisation'
# That are only in the Resource entity type
tags_to_delete = user.getTags(search_query='crystallisation', type='resource')
for i in range(len(tags_to_delete)):
print('TAGS TO DELETE =', tags_to_delete[i].name)
tags_to_delete[i].delete()
# Get the difference of two lists using set()
def diff(list1, list2):
return (list(set(list1) - set(list2)))
# Only keep experiments that investigate the protein structure by 'NMR'
# And store the IDs in a list
keep_experiments = user.getExperiments(search_query='NMR')
keep_exp_ids = []
for i in range(len(keep_experiments)):
print('EXPERIMENTS TO KEEP =', keep_experiments[i].name)
print('EXPERIMENT IDS TO KEEP =', keep_experiments[i].id)
keep_exp_ids.append(keep_experiments[i].id)
# Get all Experiment IDs
all_experiments = user.getExperiments()
all_exp_ids = []
for i in range(len(all_experiments)):
all_exp_ids.append(all_experiments[i].id)
# Find the IDs of Experiments to delete, and delete them
for i in diff(all_exp_ids, keep_exp_ids):
print('EXPERIMENT IDS TO DELETE =', i)
exp_to_delete = user.getExperiment(i)
exp_to_delete.delete()
Downloading Files
You can use labstepPy to download files uploaded directly to Labstep or attached to different Labstep entities
import labstep
user = labstep.login('myaccount@labstep.com', 'mypassword')
# A List of the authenticated user's files can be accessed
# via the getFiles method
my_files = user.getFiles()
my_file = my_files[0]
# Alternatively retrieve a list of files from a workspace
my_workspace = user.getWorkspaces()[0]
workspace_files = my_workspace.getFiles()
# Access the data via the getData method
rawData = my_file.getData()
# Or save directly as a new file
my_file.save()
# If you come across a file_id attached to another labstep entity
# you can retrieve the file using the User getFile method
my_file = user.getFile(123)
my_file.save()