Protocols
ProtocolContext ¶
A context for the state of a protocol.
The ProtocolContext class provides the objects, attributes, and methods that
allow you to configure and control the protocol.
Methods generally fall into one of two categories.
- They can change the state of the
ProtocolContextobject, such as adding pipettes, hardware modules, or labware to your protocol. - They can control the flow of a running protocol, such as pausing, displaying messages, or controlling built-in robot hardware like the ambient lighting.
Do not instantiate a ProtocolContext directly.
The run() function of your protocol does that for you.
See the Tutorial for more information.
Use get_protocol_api() to instantiate a ProtocolContext when
using Jupyter Notebook. See Advanced Control.
New in version 2.0
api_version
property
v2.0
¶
Return the API version specified for this protocol context.
This value is set when the protocol context is initialized.
- When the context is the argument of
run(), the"apiLevel"key of the metadata or requirements dictionary determinesapi_version. - When the context is instantiated with
opentrons.execute.get_protocol_apioropentrons.simulate.get_protocol_api, the value of itsversionargument determinesapi_version.
It may be lower than the maximum version supported by the
robot software, which is accessible via the
protocol_api.MAX_SUPPORTED_VERSION constant.
bundled_data
property
v2.0
¶
Accessor for data files bundled with this protocol, if any.
This is a dictionary mapping the filenames of bundled datafiles to their
contents. The filename keys are formatted with extensions but without paths. For
example, a file stored in the bundle as data/mydata/aspirations.csv will
have the key "aspirations.csv". The values are bytes objects
representing the contents of the files.
capture_image
v2.27
¶
capture_image(home_before=False, filename=None, resolution=None, zoom=None, contrast=None, brightness=None, saturation=None)
Capture an image using the camera. Captured images get saved as a result of the protocol run.
Parameters:
-
home_before(bool, default:False) –Boolean to home the pipette before capturing an image.
-
filename(str, default:None) –Filename to use when saving the captured image as a file.
-
resolution(Tuple[int, int], default:None) –Width/height tuple to determine the resolution to use when capturing an image.
-
zoom(float, default:None) –Optional zoom level, with minimum/default of 1x zoom and maximum of 2x zoom.
-
contrast(float, default:None) –Contrast level to be applied to an image, range is 0% to 100%.
-
brightness(float, default:None) –Brightness level to be applied to an image, range is 0% to 100%.
-
saturation(float, default:None) –Saturation level to be applied to an image, range is 0% to 100%.
commands
v2.0
¶
Return the run log.
This is a list of human-readable strings representing what's been done in the protocol so far. For example, "Aspirating 123 µL from well A1 of 96 well plate in slot 1."
The exact format of these entries is not guaranteed. The format here may differ from other places that show the run log, such as the Opentrons App or touchscreen.
comment
v2.0
¶
Add a user-readable message to the run log.
The message is visible anywhere you can view the run log, including the Opentrons App and the touchscreen on Flex.
Note
The value of the message is computed during protocol analysis,
so comment() can't communicate real-time information during the
actual protocol run.
create_timer
v2.27
¶
Create a timer task that runs in the background.
Parameters:
-
seconds(float) –The time to delay in seconds.
This timer will continue to run until it is complete and will not block subsequent commands.
deck
property
v2.0
¶
An interface to provide information about what's currently loaded on the deck. This object is useful for determining if a slot on the deck is free.
This object behaves like a dictionary whose keys are the
deck slot names. For instance, deck[1], deck["1"],
and deck["D1"] will all return the object loaded in the front-left slot.
The value for each key depends on what is loaded in the slot:
- A
Labwareif the slot contains a labware. - A module context if the slot contains a hardware module.
Noneif the slot doesn't contain anything.
A module that occupies multiple slots is set as the value for all of the
relevant slots. Currently, the only multiple-slot module is the Thermocycler.
When loaded, the ThermocyclerContext
object is the value for deck keys "A1" and "B1" on Flex, and 7, 8, 10,
and 11 on OT-2. In API version 2.13 and earlier, only slot 7 keyed to the
Thermocycler object, and slots 8, 10, and 11 keyed to None.
Rather than filtering the objects in the deck map yourself, you can also use
loaded_labwares to get
a dict of labwares and loaded_modules
to get a dict of modules.
For advanced control only, you can delete an element
of the deck dict. This only works for deck slots that contain labware objects.
For example, if slot 1 contains a labware, del protocol.deck["1"] will free the
slot so you can load another labware there.
Warning
Deleting labware from a deck slot does not pause the protocol. Subsequent
commands continue immediately. If you need to physically move the labware to
reflect the new deck state, add a
pause() or use
move_labware() instead.
Changed in version 2.14: Includes the Thermocycler in all of the slots it occupies.
Changed in version 2.15: del sets the corresponding labware's location to OFF_DECK.
define_liquid
v2.14
¶
Define a liquid within a protocol.
Parameters:
-
name(str) –A human-readable name for the liquid.
-
description(Optional[str], default:_Unset()) –An optional description of the liquid.
-
display_color(Optional[str], default:_Unset()) –An optional hex color code, with hash included, to represent the specified liquid. For example,
"#48B1FA". Standard three-value, four-value, six-value, and eight-value syntax are all acceptable.
Returns:
Changed in version 2.20: You can now omit the description and display_color arguments.
Formerly, when you didn't want to provide values, you had to supply
description=None and display_color=None explicitly.
define_liquid_class
v2.24
¶
Define a custom liquid class, either based on an existing liquid class, or create a completely new one.
Parameters:
-
name(str) –The name to give to the new liquid class. Cannot use the name of an Opentrons-verified liquid class.
-
properties(Dict[str, Dict[str, TransferPropertiesDict]]) –A dict of transfer properties for pipette and tip combinations to use for liquid class transfers. The nested dictionary must have top-level keys corresponding to pipette load names and second-level keys corresponding to compatible tip rack load names. Further nested key–value pairs should be as specified in
TransferPropertiesDict. See the liquid class type definitions. -
base_liquid_class(Optional[LiquidClass], default:None) –An existing liquid class object to base the newly defined liquid class on. The specified
transfer_propertieswill override any existing properties for the specified pipette and tip combinations. All other properties will remain the same as those in the base class. -
display_name(Optional[str], default:None) –An optional name for the liquid class. Defaults to the title-case
nameif a display name isn't provided.
Returns:
-
LiquidClass–A new LiquidClass object.
delay
v2.0
¶
Delay protocol execution for a specific amount of time.
Parameters:
-
seconds(float, default:0) –The time to delay in seconds.
-
minutes(float, default:0) –The time to delay in minutes.
If both seconds and minutes are specified, they will be added together.
fixed_trash
property
v2.0
¶
The trash fixed to slot 12 of an OT-2's deck.
In API version 2.15 and earlier, the fixed trash is a
Labware object with one well. Access it
like labware in your protocol. For example, protocol.fixed_trash["A1"].
In API version 2.15 only, Flex protocols have a fixed trash in slot A3.
In API version 2.16 and later, the fixed trash only exists in OT-2 protocols.
It is a TrashBin object, which doesn't
have any wells. Trying to access fixed_trash in a Flex protocol will raise
an error. See Trash Bin for details on using
the movable trash in Flex protocols.
Changed in version 2.16: Returns a TrashBin object.
get_liquid_class
v2.24
¶
Get an instance of an Opentrons-verified liquid class for use in a Flex protocol.
Parameters:
-
name(str) –Name of an Opentrons-verified liquid class. Must be one of:
"water": an Opentrons-verified liquid class based on deionized water."glycerol_50": an Opentrons-verified liquid class for viscous liquid. Based on 50% glycerol."ethanol_80": an Opentrons-verified liquid class for volatile liquid. Based on 80% ethanol.
-
version(Optional[int], default:None) –Version of the liquid class to retrieve. If left unspecified, defaults to the latest version for the protocol's API level.
Raises:
-
LiquidClassDefinitionDoesNotExist–If the specified liquid class does not exist.
Returns:
-
LiquidClass–A new LiquidClass object.
New in version 2.24.
is_simulating
v2.0
¶
Returns True if the protocol is running in simulation.
Returns False if the protocol is running on actual hardware.
You can evaluate the result of this method in an if statement to make your
protocol behave differently in different environments. For example, you could
refer to a data file on your computer when simulating and refer to a data file
stored on the robot when not simulating.
You can also use it to skip time-consuming aspects of your protocol. Most Python
Protocol API methods, like delay(), are designed to evaluate
instantaneously in simulation. But external methods, like those from the
time module, will run at normal speed if not skipped.
load_adapter
v2.15
¶
Load an adapter onto a location.
For adapters already defined by Opentrons, this is a convenient way to collapse the two stages of adapter initialization (creating the adapter and adding it to the protocol) into one.
This function returns the created and initialized adapter for use later in the protocol.
Parameters:
-
load_name(str) –A string to use for looking up a labware definition for the adapter. You can find the
load_namefor any standard adapter on the Opentrons Labware Library. -
location(Union[int, str, OffDeckType]) – -
namespace(Optional[str], default:None) –The namespace that the labware definition belongs to. If unspecified, the API will automatically search two namespaces:
"opentrons", to load standard Opentrons labware definitions."custom_beta", to load custom labware definitions created with the Custom Labware Creator.
You might need to specify an explicit
namespaceif you have a custom definition whoseload_nameis the same as an Opentrons standard definition, and you want to explicitly choose one or the other. -
version(Optional[int], default:None) –The version of the labware definition. You should normally leave this unspecified to let
load_adapter()choose a version automatically.
load_adapter_from_definition
v2.15
¶
Specify the presence of an adapter on the deck.
This function loads the adapter definition specified by adapter_def
to the location specified by location.
Parameters:
-
adapter_def('LabwareDefinition') –The adapter's labware definition.
-
location(Union[DeckLocation, OffDeckType]) –The slot into which to load the labware, such as
1,"1", or"D1". See Deck Slots.
load_instrument
v2.0
¶
load_instrument(instrument_name, mount=None, tip_racks=None, replace=False, liquid_presence_detection=None)
Load a specific instrument for use in the protocol.
When analyzing the protocol on the robot, instruments loaded with this method are compared against the instruments attached to the robot. You won't be able to start the protocol until the correct instruments are attached and calibrated.
Currently, this method only loads pipettes. You do not need to load the Flex Gripper to use it in protocols. See [automatic-manual-moves].
Parameters:
-
instrument_name(str) –The instrument to load. See API Load Names for the valid values.
-
mount(Mount or str or None, default:None) –The mount where the instrument should be attached. This can either be an instance of
Mountor one of the strings"left"or"right". When loading a Flex 96-Channel Pipette (instrument_name="flex_96channel_1000"), you can leave this unspecified, since it always occupies both mounts; if you do specify a value, it will be ignored. -
tip_racks(List[Labware], default:None) –A list of tip racks from which to pick tips when calling
pick_up_tip()without arguments. -
replace(bool, default:False) –If
True, replace the currently loaded instrument inmount, if any. This is intended for advanced control applications. You cannot replace an instrument in the middle of a protocol being run from the Opentrons App or touchscreen. -
liquid_presence_detection(bool, default:None) –If
True, enable automatic liquid presence detection for Flex 1-, 8-, or 96-channel pipettes.New in version 2.20.
load_labware
v2.0
¶
load_labware(load_name, location, label=None, namespace=None, version=None, adapter=None, lid=None, *, adapter_namespace=None, adapter_version=None, lid_namespace=None, lid_version=None)
Load a labware onto a location.
For Opentrons-verified labware, this is a convenient way to collapse the two stages of labware initialization (creating the labware and adding it to the protocol) into one.
This function returns the created and initialized labware for use later in the protocol.
Parameters:
-
load_name(str) –A string to use for looking up a labware definition. You can find the
load_namefor any Opentrons-verified labware on the Labware Library. -
location(Union[int, str, `OFF_DECK`]) – -
label(Optional[str], default:None) –An optional special name to give the labware. If specified, this is how the labware will appear in the run log, Labware Position Check, and elsewhere in the Opentrons App and on the touchscreen.
-
namespace(Optional[str], default:None) –The namespace that the labware definition belongs to. If unspecified, the API will automatically search two namespaces:
"opentrons", to load standard Opentrons labware definitions."custom_beta", to load custom labware definitions created with the Custom Labware Creator.
You might need to specify an explicit
namespaceif you have a custom definition whoseload_nameis the same as an Opentrons-verified definition, and you want to explicitly choose one or the other. -
version(Optional[int], default:None) –The version of the labware definition. You should normally leave this unspecified to let
load_labware()choose a version automatically. -
adapter(Optional[str], default:None) –The load name of an adapter to load the labware on top of. Accepts the same values as the
load_nameparameter ofload_adapter().New in version 2.15.
-
adapter_namespace(Optional[str], default:None) –The namespace of the adapter being loaded. Applies to
adapterthe same way thatnamespaceapplies toload_name.New in version 2.26:
adapter_namespacemay now be specified explicitly. Also, when you've specifiednamespacebut notadapter_namespace,adapter_namespacewill now independently follow the same search rules described innamespace. Formerly, it tooknamespace's exact value. -
adapter_version(Optional[int], default:None) –The version of the adapter being loaded. Applies to adapter the same way that version applies to load_name.
New in version 2.26:
adapter_versionmay now be specified explicitly. When unspecified, the API uses the newest version available for your protocol's API level. -
lid(Optional[str], default:None) –A lid to load on the top of the main labware. Accepts the same values as the
load_nameparameter ofload_lid_stack(). The lid will use the same namespace as the labware, and the API will choose the lid's version automatically.New in version 2.23
-
lid_namespace(Optional[str], default:None) –The namespace of the lid being loaded. Applies to
lidthe same way thatnamespaceapplies toload_name.New in version 2.26:
lid_namespacemay now be specified explicitly. Also, when you've specifiednamespacebut notlid_namespace,lid_namespacewill now independently follow the same search rules described innamespace. Formerly, it tooknamespace's exact value. -
lid_version(Optional[int], default:None) –The version of the adapter being loaded. Applies to
lidthe same way thatversionapplies toload_name.New in version 2.26:
lid_versionmay now be specified explicitly. When unspecified, the API uses the newest version available for your protocol's API level.
load_labware_by_name
v2.0
¶
Deprecated in version 2.0: Use load_labware() instead.
load_labware_from_definition
v2.0
¶
Specify the presence of a labware on the deck.
This function loads the labware definition specified by labware_def
to the location specified by location.
Parameters:
-
labware_def('LabwareDefinition') –The labware's definition.
-
location(Union[DeckLocation, OffDeckType]) –The slot into which to load the labware, such as
1,"1", or"D1". See Deck Slots. -
label(str, default:None) –An optional special name to give the labware. If specified, this is how the labware will appear in the run log, Labware Position Check, and elsewhere in the Opentrons App and on the touchscreen.
load_lid_stack
v2.23
¶
load_lid_stack(load_name, location, quantity, adapter=None, namespace=None, version=None, *, adapter_namespace=None, adapter_version=None)
Load a stack of Opentrons Tough Auto-Sealing Lids onto a valid deck location or adapter.
Parameters:
-
load_name(str) –A string to use for looking up a lid definition. You can find the
load_namefor any compatible lid on the Opentrons Labware Library. -
location(Union[DeckLocation, Labware]) –Either a deck slot, like
1,"1", or"D1", or a valid Opentrons Adapter. -
quantity(int) –The quantity of lids to be loaded in the stack.
-
adapter(Optional[str], default:None) –An adapter to load the lid stack on top of. Accepts the same values as the
load_nameparameter ofload_adapter(). The adapter will use the same namespace as the lid labware, and the API will choose the adapter's version automatically. -
namespace(str, default:None) –The namespace that the lid labware definition belongs to. If unspecified, the API will automatically search two namespaces:
"opentrons", to load standard Opentrons labware definitions."custom_beta", to load custom labware definitions created with the Custom Labware Creator.
You might need to specify an explicit
namespaceif you have a custom definition whoseload_nameis the same as an Opentrons-verified definition, and you want to explicitly choose one or the other. -
version(Optional[int], default:None) –The version of the labware definition. You should normally leave this unspecified to let
load_lid_stack()choose a version automatically. -
adapter_namespace(Optional[str], default:None) –The namespace of the adapter being loaded. Applies to
adapterthe same way thatnamespaceapplies toload_name.Changed in version 2.26:
adapter_namespacemay now be specified explicitly. When you've specifiednamespaceforload_namebut notadapter_namespace,adapter_namespacenow independently follows the same search rules described innamespace. Formerly, it took the exactnamespacevalue. -
adapter_version(Optional[int], default:None) –The version of the adapter being loaded. Applies to
adapterthe same way thatversionapplies toload_name.New in version 2.26:
adapter_versionmay now be specified explicitly.
Returns:
-
Labware–The initialized and loaded labware object representing the lid stack.
New in version 2.23.
load_module
v2.0
¶
Load a module onto the deck, given its name or model.
This is the function to call to use a module in your protocol, like
load_instrument()
is the method to call to use an instrument in your protocol. It returns the
created and initialized module context, which will be a different class
depending on the kind of module loaded.
After loading modules, you can access a map of deck positions to loaded modules
with loaded_modules.
Parameters:
-
module_name(str) –The name or model of the module. See Available Modules for possible values.
-
location(Optional[DeckLocation], default:None) –The location of the module.
This is usually the name or number of the slot on the deck where you will be placing the module, like
1,"1", or"D1". See deck slots.The Thermocycler is only valid in one deck location. You don't have to specify a location when loading it, but if you do, it must be
7,"7", or"B1". See Thermocycler Module.Changed in version 2.15: You can now specify a deck slot as a coordinate, like
"D1". -
configuration(Optional[str], default:None) –Configure a Thermocycler to be in the
semiposition. This parameter does not work. Do not use it.Removed in version 2.14: This parameter dangerously modified the protocol's geometry system, and it didn't function properly, so it was removed.
Returns:
-
ModuleTypes–The loaded and initialized module: a
AbsorbanceReaderContext,FlexStackerContext,HeaterShakerContext,MagneticBlockContext,MagneticModuleContext,TemperatureModuleContext, orThermocyclerContext, depending on what you requested withmodule_name.Changed in version 2.13: Added
HeaterShakerContextreturn value.Changed in version 2.15: Added
MagneticBlockContextreturn value.Changed in version 2.21: Added
AbsorbanceReaderContextreturn value.Changed in version 2.23: Added
FlexStackerModuleContextreturn value.
load_trash_bin
v2.16
¶
Load a trash bin on the deck of a Flex.
See Trash Bin for details.
If you try to load a trash bin on an OT-2, the API will raise an error.
Parameters:
-
location(DeckLocation) –The deck slot where the trash bin is. The location can be any unoccupied slot in column 1 or 3.
If you try to load a trash bin in column 2 or 4, the API will raise an error.
load_waste_chute
v2.16
¶
Load the waste chute on the deck of a Flex.
See Waste Chute for details, including the deck configuration variants of the waste chute.
The deck plate adapter for the waste chute can only go in slot D3. If you try to load another item in slot D3 after loading the waste chute, or vice versa, the API will raise an error.
loaded_instruments
property
v2.0
¶
Get the instruments that have been loaded into the protocol.
This is a map of mount name to instruments previously loaded with
load_instrument().
It does not reflect what instruments are actually installed on the robot.
For example, if the robot has instruments installed on both mounts but your
protocol has only loaded one of them with
load_instrument(),
the unused one will not be included in loaded_instruments.
Returns:
-
Dict[str, InstrumentContext]–A dict mapping mount name (
"left"or"right") to the instrument in that mount. If a mount has no loaded instrument, that key will be missing from the dict.
loaded_labwares
property
v2.0
¶
Get the labwares that have been loaded into the protocol context.
Slots with nothing in them will not be present in the return value.
Note
If a module is present on the deck but no labware has been loaded
into it with load_labware(),
there will be no entry for that slot in this value. That means you should not
use loaded_labwares to determine if a slot is available or not,
only to get a list of labwares. If you want a data structure of all
objects on the deck regardless of type, use
deck.
Returns:
-
Dict[int, Labware]–Dict mapping deck slot number to labware, sorted in order of the locations.
loaded_modules
property
v2.0
¶
Get the modules loaded into the protocol context.
This is a map of deck positions to modules loaded by previous calls to
load_module(). It does not reflect what modules are actually attached
to the robot. For example, if the robot has a Magnetic Module and a Temperature
Module attached, but the protocol has only loaded the Temperature Module with
load_module(), only the Temperature Module will be included in
loaded_modules.
Returns:
-
Dict[int, ModuleTypes]–Dict mapping slot name to module contexts. The elements may not be ordered by slot number.
max_speeds
property
v2.0
¶
Per-axis speed limits for moving instruments.
Changing values within this property sets the speed limit for each non-plunger axis of the robot. Note that this property only sets upper limits and can't exceed the physical speed limits of the movement system.
This property is a dict mapping string names of axes to float values
of maximum speeds in mm/s. To change a speed, set that axis's value. To
reset an axis's speed to default, delete the entry for that axis
or assign it to None.
Note
This property is not yet supported in API version 2.14 or higher.
move_labware
v2.15
¶
Move a loaded labware to a new location.
See Moving Labware for more details.
Parameters:
-
labware(Labware) –The labware to move. It should be a labware already loaded using
load_labware(). -
new_location(Union[DeckLocation, Labware, ModuleTypes, OffDeckType, WasteChute, TrashBin]) –Where to move the labware to. This is either:
- A deck slot like
1,"1", or"D1". See Deck Slots. - A hardware module that's already been loaded on the deck
with
load_module(). - A labware or adapter that's already been loaded on the deck
with
load_labware()orload_adapter(). - The special constant
OFF_DECK.
- A deck slot like
-
use_gripper(bool, default:False) –Whether to use the Flex Gripper for this movement.
- If
True, use the gripper to perform an automatic movement. This will raise an error in an OT-2 protocol. - If
False, pause protocol execution until the user performs the movement. Protocol execution remains paused until the user presses Confirm and resume.
- If
-
pick_up_offset(Optional[Mapping[str, float]], default:None) –Optional x, y, z vector offset for the gripper to use when picking up labware.
-
drop_offset(Optional[Mapping[str, float]], default:None) –Optional x, y, z vector offset for the gripper to use when dropping off labware.
Note
Before moving a labware to or from a hardware module, make sure that the labware's current and new locations are accessible, i.e., open the Thermocycler lid or open the Heater-Shaker's labware latch.
move_lid
v2.23
¶
Move a compatible lid from a valid source to a new location. Can return a lid stack if one is created.
Parameters:
-
source_location(Union[DeckLocation, Labware]) –The lid's starting location. This is either:
- A deck slot like
1,"1", or"D1". See deck slots. - A labware or adapter that's already been loaded on the deck
with
load_labware()orload_adapter(). - A lid stack that's already been loaded on the deck with
load_lid_stack().
- A deck slot like
-
new_location(Union[DeckLocation, Labware, OffDeckType, WasteChute, TrashBin]) –Where to move the lid to. This is either:
- A deck slot like
1,"1", or"D1". See deck slots. - A hardware module that's already been loaded on the deck
with
load_module(). - A labware or adapter that's already been loaded on the deck
with
load_labware()orload_adapter(). - The special constant
OFF_DECK.
- A deck slot like
-
use_gripper(bool, default:False) –Whether to use the Flex Gripper to move the lid.
- If
True, use the gripper to perform an automatic movement. This will raise an error in an OT-2 protocol. - If
False, pause protocol execution until the user performs the movement. Protocol execution remains paused until the user presses Confirm and resume.
- If
-
pick_up_offset(Optional[Mapping[str, float]], default:None) –Optional x, y, z vector offset to use when picking up a lid.
-
drop_offset(Optional[Mapping[str, float]], default:None) –Optional x, y, z vector offset to use when dropping off a lid.
Note
Before moving a lid to or from a labware in a hardware module, make sure that the labware's current and new locations are accessible, i.e., open the Thermocycler lid or open the Heater-Shaker's labware latch.
New in version 2.23.
params
property
v2.18
¶
The values of runtime parameters, as set during run setup.
Each attribute of this object corresponds to the variable_name of a parameter.
See Using Parameter Values for details.
Parameter values can only be set during run setup. If you try to alter the value
of any attribute of params, the API will raise an error.
pause
v2.0
¶
Pause execution of the protocol until it's resumed.
A human can resume the protocol in the Opentrons App or on the touchscreen.
Note
In Python Protocol API version 2.13 and earlier, the pause will only take effect on the next function call that involves moving the robot.
Parameters:
-
msg(str, default:None) –An optional message to show in the run log entry for the pause step.
resume
v2.0
¶
set_rail_lights
v2.5
¶
Controls the robot's ambient lighting (rail lights).
Parameters:
-
on(bool) –If
True, turn on the lights; otherwise, turn them off.