Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +41 -11
app/main.py
CHANGED
|
@@ -1,17 +1,52 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from opentrons.simulate import simulate, format_runlog, get_protocol_api
|
| 4 |
-
import os
|
| 5 |
import io
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.get("/")
|
| 12 |
async def root():
|
| 13 |
-
return {"message": "
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# @app.get("/protocols")
|
| 17 |
# def read_protocols():
|
|
@@ -45,11 +80,6 @@ def get_file_names():
|
|
| 45 |
return file_names
|
| 46 |
|
| 47 |
|
| 48 |
-
class Protocol(BaseModel):
|
| 49 |
-
name: str
|
| 50 |
-
content: str
|
| 51 |
-
|
| 52 |
-
|
| 53 |
@app.post("/protocol")
|
| 54 |
def upload_protocol(protocol: Protocol):
|
| 55 |
file_path = 'storage/' + protocol.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
+
import os
|
| 3 |
+
import traceback
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI, HTTPException, status
|
| 6 |
+
from fastapi.concurrency import run_in_threadpool
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from opentrons.simulate import simulate, format_runlog
|
| 9 |
+
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
|
| 14 |
+
class Protocol(BaseModel):
|
| 15 |
+
name: str
|
| 16 |
+
content: str
|
| 17 |
+
|
| 18 |
@app.get("/")
|
| 19 |
async def root():
|
| 20 |
+
return {"message": "Opentrons simulation API is running."}
|
| 21 |
|
| 22 |
+
# no file store in memory, just simulate the protocol
|
| 23 |
+
@app.post("/simulate")
|
| 24 |
+
async def simulate_protocol(protocol: Protocol):
|
| 25 |
+
try:
|
| 26 |
+
protocol_file = io.StringIO(protocol.content)
|
| 27 |
+
run_log, _ = await run_in_threadpool(
|
| 28 |
+
simulate,
|
| 29 |
+
protocol_file=protocol_file,
|
| 30 |
+
file_name=protocol.name
|
| 31 |
+
)
|
| 32 |
+
# return execution log
|
| 33 |
+
return {
|
| 34 |
+
'protocol_name': protocol.name,
|
| 35 |
+
'run_status': 'success',
|
| 36 |
+
'run_log': format_runlog(run_log)
|
| 37 |
+
}
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error simulating protocol: {e}")
|
| 40 |
+
|
| 41 |
+
raise HTTPException(
|
| 42 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 43 |
+
detail={
|
| 44 |
+
"message": "Error simulating protocol",
|
| 45 |
+
"error_type": type(e).__name__,
|
| 46 |
+
"error_details": str(e),
|
| 47 |
+
"traceback": traceback.format_exc()
|
| 48 |
+
}
|
| 49 |
+
)
|
| 50 |
|
| 51 |
# @app.get("/protocols")
|
| 52 |
# def read_protocols():
|
|
|
|
| 80 |
return file_names
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
@app.post("/protocol")
|
| 84 |
def upload_protocol(protocol: Protocol):
|
| 85 |
file_path = 'storage/' + protocol.name
|