Skip to navigation

Creating a new Notebook Entry containing Results

Create a notebook entry, add a results table to it, and fill the table with assay results, using the V3 SDK.

Overview

In this guide, we’ll go through the steps necessary to create a new notebook Entry that contains assay results, using the V3 Python SDK. If you’d like to add results to an existing Entry, please refer to Uploading Results to Benchling through the API.

With the V3 API this takes four steps:

  1. Create the entry.
  2. Add a results table for your result schema to the entry.
  3. Find the table’s document part ID.
  4. Create the results with that table ID, then integrate them into the table.

V3’s entry.create does not take an entry template or initial tables, so it cannot create an entry from a template that already holds a filled-in results table. That still needs V2, covered in Creating the entry from a template (V2).

The V3 entry and result endpoints are beta. Set early_access="true" on the client, as in the examples below, or send the EARLY-ACCESS: true header on raw HTTP requests. See the V3 Stability Guidelines.

Every example below uses this client. Pass your tenant’s root URL; the SDK adds /api/v3 itself.

from benchling_v3_sdk import Benchling
benchling = Benchling(
base_url="https://yourcompany.benchling.com",
api_key="<your-api-key>",
early_access="true",
)

This guide uses the qPCR result schema (assaysch_FL4k8H51), sample entities, and project (src_wMsnz0ru) from Uploading Results to Benchling through the API. Substitute your own schema, field names, and IDs.

Create the Entry

To create the entry, provide:

  • a folder ID: where the entry should be created within Benchling
  • a name: the name for the new entry that will appear within Benchling
entry = benchling.entry.create(
folder_id="lib_XXXXXX",
name="Create Entry w Results",
)

entry.create also takes parts, for sections, text, headers, tables, and attachments, and schema_id and schema_fields for an entry schema.

Add a Results Table

Add a results table for the result schema with entry.update, inserting a results_table part with a parts_mutation. The insert names the entry’s current version, and position="end_of_document" appends the table at the end of the entry.

from benchling_v3_sdk import DocumentContentInput_ResultsTable, UpdateDocumentPartsInput_Insert
benchling.entry.update(
entry.id,
parts_mutation=UpdateDocumentPartsInput_Insert(
version=entry.version_metadata.version_id,
position="end_of_document",
parts=[
DocumentContentInput_ResultsTable(
name="qPCR Results",
result_schema_id="assaysch_FL4k8H51",
)
],
),
)

If the entry changed since you read its version, the request fails with HTTP 409. Read the entry again and retry.

Find the Table’s Document Part ID

List the entry’s parts and pick out the results table. Parts are returned in the order they appear in the entry:

results_tables = [
part
for part in benchling.entry.list_parts(entry_id=entry.id)
if part.typename == "DocumentPartResultsTable"
]
table_id = results_tables[0].id # looks like docpart_aBCdEfgh

Storing and Re-using IDs

There’s no need to look up the result schema ID or the destination folder ID every time you make a new Entry. Instead, we’d recommend storing these as fields in App Configuration. For more information, see Building your first App with App Configuration.

Create the Results

Create the results with the table’s ID as document_part_results_table_id. A batch create makes up to 25 results in one all-or-nothing request:

from benchling_v3_sdk import CreateResultInput, FieldValueInput
rows = [
("bfi_ELstHqON", 11.115),
("bfi_Xq2PmTzA", 12.034),
]
response = benchling.result.batch_create(
items=[
CreateResultInput(
schema_id="assaysch_FL4k8H51",
project_id="src_wMsnz0ru",
document_part_results_table_id=table_id,
schema_fields={
"sample_id": FieldValueInput(value=sample_id),
"ct": FieldValueInput(value=ct),
},
)
for sample_id, ct in rows
]
)
print([created.id for created in response.items])

For more than 25 results, use a bulk create with documentPartResultsTableId in each line of the bulk file, as shown in Create results in bulk.

Results created this way are not appended to the table straight away: they are marked as pending until someone integrates them with the plug icon in the table’s toolbar. Pending results can’t be integrated if doing so would take the table past the Notebook table row maximum. See Integrating the Results into the Table.

Creating the entry from a template (V2)

V3 cannot create an entry from an entry template or fill a table at creation, so this flow uses the V2 SDK (benchling-sdk). It creates an entry from an Entry Template that contains a results table, and fills the table from a CSV file uploaded as a blob. Here’s what our empty Entry Template looks like:

A Benchling entry template containing a results table with Plate Well and Value columns and no rows
An Entry Template with an empty results table

Fetch the Entry Template

Let’s start by fetching the Entry Template that we want to use to create our entry. To do this, we’ll use the ID of the Entry Template, which you can find via Benchling (Feature settings > Template collections) and using the ‘Copy API ID’ button:

The template collections settings page with the Copy API ID option open for an entry template
Copying the API ID of an entry template

Now we have the Entry Template ID, we can retrieve the template using the SDK.

import os
from benchling_sdk.benchling import Benchling
from benchling_sdk.auth.api_key_auth import ApiKeyAuth
benchling = Benchling(url = "https://TENANT_NAME.benchling.com", auth_method=ApiKeyAuth(os.environ['BENCHLING_API_KEY']))
template = benchling.entries.get_entry_template_by_id(entry_template_id="tmpl_XXXXXX")
print(template.to_dict())

To create results within our entry, we’ll need to use:

  • the ID of the table in the template - this will be used to associate our results with the correct table in the Entry
  • the IDs of the columns - these will be used in our CSV in place of traditional column headers (Plate Well and Value in our example)
The API response for an entry template, highlighting the table id and the id of each column
The template table ID and its column IDs

Create CSV File and Upload as Blob

Now we have the column identifiers, we’re ready to create our CSV file. The first column represents our Plate Well positions, and the second column the result values we’d like to associate with these wells.

Plate Results.csv
fb8590a2-9c12-42ab-b96d-54f596142715,b105c438-6639-469e-ae28-4ad162daf47e
96WP001:A1,1.2345
96WP001:A2,2.3456

We can now upload this file as a blob to Benchling:

from pathlib import Path
blob = benchling.blobs.create_from_file(
file_path=Path("Plate Results.csv"),
name = "Plate Results.csv",
mime_type="text/csv",
)
csv_blob = blob.id

Create the Entry

We now have all the pieces we need to create the new Entry in Benchling. We’ll define our table as an InitialTable, which associates our blob with the ID of the table in the template. To construct the Entry, we’ll create an instance of EntryCreate and also provide:

  • a folder ID: where the entry should be created within Benchling
  • a name: the name for the new entry that will appear within Benchling
  • the Entry Template ID: we’ll re-use the value from the first step
from benchling_sdk.models import EntryCreate, InitialTable
table = InitialTable(
csv_data = blob.id,
template_table_id = "strtbl_XXXXXX",
)
entry = EntryCreate(
folder_id = "lib_XXXXXX",
name = "Create Entry w Results",
entry_template_id = template.id,
initial_tables = [table],
)
created_entry = benchling.entries.create_entry(entry=entry)

The entry will be created within Benchling, and contain the results we added to the CSV in the correct place:

A Benchling notebook entry created from a template, with the results table populated with two plate well rows
The new entry in Benchling, with results populated in the table