All Collections
Tools & Integrations
Labstep API
Using the Labstep API with Jupyter Notebooks
Using the Labstep API with Jupyter Notebooks

How to combine our API with our Jupyter Notebooks integration for workflow automation.

Charlotte Ward avatar
Written by Charlotte Ward
Updated over a week ago

Jupyter Notebooks provide a Python programming environment within your Labstep experiments and protocols. Combined with the Labstep API, they enable the great potential for automating your workflows and data analysis within the Labstep user interface (UI). To get started with Jupyter Notebooks, click here. For more information on our Labstep API, click here.


Log in to Labstep Workspace:

import labstep

## Login to Labstep Workspace
user = labstep.authenticate()

Labstep team tip: It is unnecessary to enter log-in credentials with the .authenticate() method as these are taken from the active Labstep web session.

Get the current Labstep document:

import labstep

## Login to Labstep Workspace
user = labstep.authenticate()

## Get the id of the document (experiment or protocol) in which the Jupyter Notebook resides
document = labstep.jupyter.getParent()

Labstep team tip: Getting the ID of the current document (Labstep experiment or protocol) makes it possible to reference the document when calling functions in the Jupyter Notebook.

Read tables within a document and convert to a DataFrame:

This script allows you to take the data from within a Labstep table within your document and convert it to a Pandas DataFrame for further processing or analysis.

To execute this script in your Labstep experiment:

  1. Create an interactive table within your experiment called "Experiment data table".

  2. Enter some example data into the table.

  3. Open a new instance of Jupyter Notebooks in your experiment.

  4. Copy and paste the script below into the Jupyter Notebook editor, then run the script.

Labstep team tip: You will see the DataFrame of the data in your table printed in the Jupyter Notebook output.

import labstep

## Login to Labstep Workspace
user = labstep.authenticate()

## Get the id of the document (experiment or protocol) in which the Jupyter Notebook resides
document = labstep.jupyter.getParent()

## Get the tables within the experiment
tables = document.getTables()

## Select the table of interest and convert to a DataFrame.
df = tables.get('Experiment data table').getDataFrame()

## Print the DataFrame.
print(df)

Get data from a data field and convert to a DataFrame

This script allows you to take the data from within a .csv file that you have added to your Labstep document and convert it to a Pandas DataFrame for further processing or analysis.

To execute this script in your Labstep experiment:

  1. Create a data field in your Labstep experiment called "Example data".

  2. Upload a .csv file containing some example data to the data field.

  3. Open a new instance of Jupyter Notebooks in your experiment.

  4. Copy and paste the script below into the Jupyter Notebook editor, then run the script.

Labstep team tip: You will see the DataFrame of the data in your .csv file printed in the Jupyter Notebook output.

import labstep
import pandas as pd

## Login to Labstep Workspace
user = labstep.authenticate()

## Get the id of the document (experiment or protocol) in which the Jupyter Notebook resides
document = labstep.jupyter.getParent()

## Get the data fields within the experiment
dataFields = document.getDataFields()

## Select the data field of interest and get the file
example_data_file = dataFields.get('Example data').getValue()

## Save the file locally in the notebook
example_data_file.save()

## Read the file in as a DataFrame
df = pd.read_csv(example_data_file.name)

## print the DataFrame
print(df)

Write a new data table for an experiment:

This script allows you to take the data from within a .csv file that you have added to your Labstep experiment and write it to a table in your experiment for further interrogation or analysis.

To execute this script in your Labstep experiment:

  1. Create a data field in your Labstep experiment called "Example data".

  2. Upload a .csv file containing some example data to the data field.
    โ€‹

  3. Create an interactive called 'Output Table' to enter data into.

  4. Open a new instance of Jupyter Notebooks in your experiment.

  5. Copy and paste the script below into the Jupyter Notebook editor.

  6. Save your script and exit the Jupyter Notebook editor.

  7. Run your script from within the Labstep experiment.

Labstep team tip: You will see the table of the data from the .csv file appear in your Output Table. You may need to refresh your page.

import labstep
import pandas as pd
from labstep.service.helpers import dataFrameToDataTable


## Login to Labstep Workspace
user = labstep.authenticate()

## Get the id of the document (experiment or protocol) in which the Jupyter Notebook resides
document = labstep.jupyter.getParent()

## Get the datafields within the experiment
dataFields = document.getDataFields()

## Select the datafield of interest and get the data
example_data_file = dataFields.get('Example data').getValue()

## Save the file locally in the notebook
example_data_file.save()

## Convert the data to a dataframe
df = pd.read_csv(example_data_file.name)

## Make a Labstep data table from the DataFrame
data_table = dataFrameToDataTable(df)

## Get the table from the document
table = document.getTables().get('Output Table')

## Add data to Output Table
table.edit(data=data_table)

Save an image of a plot to a Labstep data field:

This script allows you to plot data in a graph which is then saved into your Labstep document (experiment or protocol) as a data field.

To execute this script in your Labstep experiment:

  1. Create a data field in your Labstep experiment called "Plot".

  2. Open a new instance of Jupyter Notebooks in your experiment.

  3. Copy and paste the script below into the Jupyter Notebook editor.

  4. Save your script and exit the Jupyter Notebook editor.

  5. Run your script from within the Labstep experiment.

Labstep team tip: You will see the plot of your data appear within the 'Plot' data field in your experiment. You may need to refresh your page.

import labstep
import matplotlib.pyplot as plt

## Login to Labstep Workspace
user = labstep.authenticate()

## Get the id of the document (experiment or protocol) in which the Jupyter Notebook resides
document = labstep.jupyter.getParent()

## Plot some data
plt.plot([1, 2, 3, 4])

## Save the data
plt.savefig('plot.png')

## Get the data field to save the plot to
plotData = document.getDataFields().get('Plot')

## Set the value of the data field to be the generated plot.
plotData.setValue(user.newFile('plot.png'))


Still need help?

Contact us here or start a conversation with a member of our team using our in-app chat.

Did this answer your question?