Skip to content

Commit

Permalink
feat(protocol-designer): add placeholder for step commands to generat…
Browse files Browse the repository at this point in the history
…ed Python (#17496)

# Overview

This adds the Python output from `step-generation` into the generated
Python file so that we can see our progress as we work. AUTH-1385

Right now, it just emits a placeholder that looks like:
```
def run(protocol: protocol_api.ProtocolContext):
    # PROTOCOL STEPS

    # Step 1:
    pass

    # Step 2:
    pass
```

## Test Plan and Hands on Testing

Updated unit test, and tested exporting a Python protocol by hand.

## Risk assessment

Low. Python export is hidden under a feature flag, so this should have
no observable impact to users.
  • Loading branch information
ddcc4 authored Feb 11, 2025
1 parent 024b4e9 commit 831c4ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion protocol-designer/src/file-data/__tests__/createFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ describe('createFile selector', () => {
fileMetadata,
OT2_ROBOT_TYPE,
entities,
v7Fixture.initialRobotState
v7Fixture.initialRobotState,
v7Fixture.robotStateTimeline
)
// This is just a quick smoke test to make sure createPythonFile() produces
// something that looks like a Python file. The individual sections of the
Expand All @@ -133,6 +134,11 @@ def run(protocol: protocol_api.ProtocolContext):
mockPythonName = protocol.load_labware("fixture_trash", "12")
mockPythonName = protocol.load_labware("fixture_tiprack_10_ul", "1")
mockPythonName = protocol.load_labware("fixture_96_plate", "7")
# PROTOCOL STEPS
# Step 1:
pass
`.trimStart()
)
})
Expand Down
11 changes: 9 additions & 2 deletions protocol-designer/src/file-data/selectors/fileCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,21 @@ export const createPythonFile: Selector<string> = createSelector(
getRobotType,
stepFormSelectors.getInvariantContext,
getInitialRobotState,
(fileMetadata, robotType, invariantContext, robotState) => {
getRobotStateTimeline,
(
fileMetadata,
robotType,
invariantContext,
robotState,
robotStateTimeline
) => {
return (
[
// Here are the sections of the Python file:
pythonImports(),
pythonMetadata(fileMetadata),
pythonRequirements(robotType),
pythonDefRun(invariantContext, robotState),
pythonDefRun(invariantContext, robotState, robotStateTimeline),
]
.filter(section => section) // skip any blank sections
.join('\n\n') + '\n'
Expand Down
18 changes: 16 additions & 2 deletions protocol-designer/src/file-data/selectors/pythonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
InvariantContext,
LabwareEntities,
ModuleEntities,
Timeline,
TimelineFrame,
} from '@opentrons/step-generation'
import type { RobotType } from '@opentrons/shared-data'
Expand Down Expand Up @@ -135,9 +136,22 @@ export function getLoadLabware(
return pythonLabware ? `# Load Labware:\n${pythonLabware}` : ''
}

export function stepCommands(robotStateTimeline: Timeline): string {
return (
'# PROTOCOL STEPS\n\n' +
robotStateTimeline.timeline
.map(
(timelineFrame, idx) =>
`# Step ${idx + 1}:\n${timelineFrame.python || 'pass'}`
)
.join('\n\n')
)
}

export function pythonDefRun(
invariantContext: InvariantContext,
robotState: TimelineFrame
robotState: TimelineFrame,
robotStateTimeline: Timeline
): string {
const { moduleEntities, labwareEntities } = invariantContext
const { modules, labware } = robotState
Expand All @@ -152,7 +166,7 @@ export function pythonDefRun(
// loadInstruments(),
// defineLiquids(),
// loadLiquids(),
// stepCommands(),
stepCommands(robotStateTimeline),
]
const functionBody =
sections
Expand Down

0 comments on commit 831c4ae

Please sign in to comment.