Further Examples

Workspace Setup

To setup a Workspace on Labstep:

workspace_setup.py

#!/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...

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.

delete_multiple.py

#!/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

download_file.py

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()

A basic python GUI

In this basic example, a GUI is created that can be used to authenticate a user on Labstep and create new experiments.

import labstep
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk

class NewExperimentForm:
  def __init__(self,root, user):
    self.user = user
    self.frame = Frame(root, width=100, height=50, pady=50,padx=50)
    self.frame.place(relx=0.5, rely=0.5, anchor=CENTER)
    Logo(self.frame)
    self.name = LabelledInput(self.frame, 'Name')
    self.submit_button = Button(self.frame, text="Create Experiment", command=self.action)
    self.submit_button.pack()

  def action(self):
    name = self.name.get()
    self.user.newExperiment(name)

class Logo:
  def __init__(self,root):
    load = Image.open("labstep_logo.png")
    render = ImageTk.PhotoImage(load)
    logo = Label(root, image=render)
    logo.image = render
    logo.pack()

class LabelledInput:
  def __init__(self,root,name):
    self.label = Label(root, text=name)
    self.label.pack()
    self.input = Entry(root,width=20)
    self.input.pack()

  def get(self):
    return self.input.get()


class LoginForm:
  def __init__(self,root,onSuccess):
    self.onSuccess = onSuccess
    # Define and place the frame for the login form
    self.frame = Frame(root, width=100, height=50, pady=10,padx=10)
    self.frame.place(relx=0.5, rely=0.5, anchor=CENTER)
    # Place the labstep logo
    Logo(self.frame)
    # Username label and input
    self.username = LabelledInput(self.frame,"Username")
    # Api key label and input
    self.apikey = LabelledInput(self.frame,"API key")
    # Submit button
    submit_button = Button(self.frame, text="Authenticate", command=self.onSubmit)
    submit_button.pack()

  # Function that runs when user hits enter
  def onSubmit(self):
    self.frame.place_forget() # hide login frame
    username = self.username.get() # get user input
    apikey = self.apikey.get() # get user input
    user = labstep.authenticate(username,apikey) # Login via labstep API
    messagebox.showinfo('Login Success', f'You have successfully authenticated as {user.first_name} {user.last_name}')
    self.onSuccess(user)


class App:
  def __init__(self):
    self.window = Tk()
    self.window.title("Labstep Python App")
    self.window.geometry('600x400')
    self.login()

  def main(self,user):
    NewExperimentForm(self.window,user)

  def login(self):
    LoginForm(self.window,self.main)

  def start(self):
    self.window.mainloop()


App().start()