Jupyter Notebooks

Jupyter Notebooks Basics

Labstep Jupyter Notebooks have the following modules pre-installed:

  • pandas

  • scipy

  • matplotlib

  • openpyxl

  • ipywidgets

  • plotly

  • pymatgen

  • PyMassSpec

  • Labstep SDK

These modules can be imported and used in Jupyter Notebook scripts in Labstep without being installed.

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.

jn_table_to_df.py

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.

jn_csv_to_df.py

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.

jn_new_table.py

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.

jn_plot_to_data_field.py

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