Running Code
Basic Execution
Section titled “Basic Execution”State persists between run() calls within the same sandbox:
cell.run("x = 42")result = cell.run("print(x * 2)")print(result.stdout) # "84"print(result.exit_code) # 0print(result.latency_ms) # 0.24Streaming Output
Section titled “Streaming Output”For long-running code, use callbacks to stream output in real-time:
result = cell.run( "for i in range(10): print(f'Processing {i}')", on_stdout=lambda line: print(f"[live] {line}"), on_stderr=lambda line: print(f"[err] {line}"),)const result = await cell.run(code, { onStdout: (line) => console.log(`[live] ${line}`), onStderr: (line) => console.error(`[err] ${line}`),});Background Commands
Section titled “Background Commands”Run commands asynchronously and poll for results:
handle = cell.command("pip install pandas && python train.py", background=True)
# Poll statuswhile handle.is_running: time.sleep(1)
print(handle.stdout)print(handle.exit_code)Execution Receipts
Section titled “Execution Receipts”Every execution returns a cryptographic receipt:
result = cell.run("print(2 + 2)")receipt = result.receipt
print(receipt.execution_id) # Unique IDprint(receipt.code_hash) # SHA-256 of input codeprint(receipt.result_hash) # SHA-256 of outputprint(receipt.timestamp) # Unix ms