Protocols

Creating protocols

Create a protocol and edit the body

The body of protocols can be edited like the entries of experiments. The below shows how to add text and files to the protocol body. For further examples, see those listed under creating experiments.

edit_protocol.py

import os
import labstep

## Authenticate user
user = labstep.authenticate(USERNAME, APIKEY) # Replace with authenitcation credentials


## Set to the ID of the workspace to import into.
user.setWorkspace(WORKSPACE_ID) # Replace with workspace ID

## Create a protocol
protocol = user.newProtocol(name="API protocol")


## Add text to protocol body
body = {
    'type': 'doc',
    'content': [
        {
            'type': 'paragraph',
            'attrs': {
                'align': None
            }
        },
        {
            'type': 'paragraph',
            'attrs': {
                'align': None
            },
            'content': [
                {
                    'text': 'This is a new paragraph',
                    'type': 'text'
                }
            ]
        },
        {
            'type': 'paragraph',
            'attrs': {
                'align': None
            }
        }
    ]
}

protocol.edit(body=body)


## Adding a file / image to the body (images rendered automatically)

body = protocol.getBody()

file = experiment.addFile(os.path.abspath(__file__)) ## Replace with path to file

newContent = {
    "type": "paragraph",
    "attrs": {"align": None},
    "content": [
       {
        "type": "file", ## NOTE: this type of node MUST be embdedded in a paragraph node
        "attrs": {
            "id": file["id"],
            "fullWidth": False
            }
        }
    ]
}

body['content'].append(newContent)

protocol.edit(body=body)

Exporting protocols

The following example scripts will export experiments in PDF, HTML and JSON formats.

Export a specific protocol

The protocol ID can be found in the URL of the protocol in the web app.

export_protocol.py

import labstep
import labstep.config.export as exportConfig

exportConfig.includePDF = True

user = labstep.authenticate(apikey='<your API key>') ## Enter your API key.

protocol = user.getProtocol(XXXX) ## Enter protocol ID.

protocol.export('my/folder/path') ## Export all formats to a path called 'export'.

Export protocols by workspace

Export the protocols by their workspace. The ID of the workspace, collections and tags can be found in their URLs in the web app.

export__workspace_protocol.py

import labstep
import labstep.config.export as exportConfig

exportConfig.includePDF = True

user = labstep.authenticate(apikey='<your API key>') ## Enter your API key

workspace = user.getWorkspace(XXXX) ## Enter Workspace ID

protocols = workspace.getProtocols() ## Filter protocols using parameters such as tag_id, collection_id, created_at_from, created_at_to

for protocol in protocols:
    protocol.export('my/folder/path') ## Export all formats to a path called 'export'.

Export protocols by user

Export the protocols of a user across multiple workspaces.

export__workspace_protocol.py

import labstep
import labstep.config.export as exportConfig

exportConfig.includePDF = True

user = labstep.authenticate(apikey='<your API key') ## Enter your API key.

protocols = user.getProtocols() ## Filter protocols using parameters such as tag_id, collection_id, created_at_from, created_at_to.

for protocol in protocols:
    protocol.export('my/folder/path') ## Export all formats to a path called 'export'.