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:
Create an interactive table within your experiment called "Experiment data table".
Enter some example data into the table.
Open a new instance of Jupyter Notebooks in your experiment.
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:
Create a data field in your Labstep experiment called "Example data".
Upload a .csv file containing some example data to the data field.
Open a new instance of Jupyter Notebooks in your experiment.
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:
Create a data field in your Labstep experiment called "Example data".
Upload a .csv file containing some example data to the data field.
โCreate an interactive called 'Output Table' to enter data into.
Open a new instance of Jupyter Notebooks in your experiment.
Copy and paste the script below into the Jupyter Notebook editor.
Save your script and exit the Jupyter Notebook editor.
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:
Create a data field in your Labstep experiment called "Plot".
Open a new instance of Jupyter Notebooks in your experiment.
Copy and paste the script below into the Jupyter Notebook editor.
Save your script and exit the Jupyter Notebook editor.
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.