@@ -66,7 +66,6 @@ const pythonCtx = await sandbox.createCodeContext({
6666});
6767
6868console.log('Context ID:', pythonCtx.id);
69-
7069```
7170</TypeScriptExample >
7271
@@ -83,7 +82,6 @@ const jsCtx = await sandbox.createCodeContext({
8382 }
8483});
8584```
86-
8785</TypeScriptExample >
8886
8987** Create TypeScript context:**
@@ -147,7 +145,6 @@ x * 2
147145
148146console.log('Stdout:', result.logs.stdout); // ["Hello from Python!"]
149147console.log('Result:', result.results[0].text); // "84"
150-
151148```
152149</TypeScriptExample >
153150
196193
197194console.log(result.logs.stdout); // ["Average: 3"]
198195console.log(result.results[0].text); // "3"
199-
200196```
201197</TypeScriptExample >
202198
@@ -229,7 +225,6 @@ for i in range(5):
229225// [STDOUT] Processing item 1...
230226// ...
231227```
232-
233228</TypeScriptExample >
234229
235230** Error handling:**
@@ -250,7 +245,6 @@ console.error('Traceback:', result.error.traceback);
250245} catch (error) {
251246console.error('Failed to execute:', error.message);
252247}
253-
254248```
255249</TypeScriptExample >
256250
277271
278272console.log('Result:', result.results[0].text); // "78.53981633974483"
279273```
280-
281274</TypeScriptExample >
282275
283276** AI code execution example:**
@@ -306,7 +299,6 @@ timeout: 5000 // 5 second timeout
306299});
307300
308301console.log('Result:', result.results[0].text);
309-
310302```
311303</TypeScriptExample >
312304
@@ -337,7 +329,6 @@ console.log(` Language: ${ctx.language}`);
337329console.log(` Created: ${ctx.createdAt.toISOString()}`);
338330console.log(` Last used: ${ctx.lastUsed.toISOString()}`);
339331}
340-
341332```
342333</TypeScriptExample >
343334
@@ -368,7 +359,6 @@ await sandbox.runCode('print("Hello")', { context: ctx });
368359// Clean up when done
369360await sandbox.deleteCodeContext(ctx.id);
370361console.log('Context deleted');
371-
372362```
373363</TypeScriptExample >
374364
@@ -390,7 +380,6 @@ for (const ctx of contexts) {
390380 }
391381}
392382```
393-
394383</TypeScriptExample >
395384
396385## Rich Output Formats
@@ -401,8 +390,7 @@ The Code Interpreter supports multiple output formats for data visualization and
401390
402391Each execution returns ` Result ` objects with these properties:
403392
404- <TypeScriptExample >
405- ```
393+ ``` ts
406394interface Result {
407395 text? : string ; // Plain text representation
408396 html? : string ; // HTML tables, formatted output
@@ -418,9 +406,7 @@ interface Result {
418406
419407formats(): string []; // List available formats
420408}
421-
422409```
423- </TypeScriptExample >
424410
425411
426412### Working with Charts
@@ -452,7 +438,6 @@ if (result.results[0]?.png) {
452438 console.log('Chart generated');
453439}
454440```
455-
456441</TypeScriptExample >
457442
458443** Return image to client:**
@@ -502,7 +487,6 @@ if (result.results[0]?.html) {
502487 });
503488}
504489```
505-
506490</TypeScriptExample >
507491
508492### Working with JSON Data
@@ -513,23 +497,22 @@ const result = await sandbox.runCode(`
513497import json
514498
515499data = {
516- 'status': 'success',
517- 'items': [1, 2, 3, 4, 5],
518- 'summary': {
519- 'total': 15,
520- 'average': 3.0
521- }
500+ 'status': 'success',
501+ 'items': [1, 2, 3, 4, 5],
502+ 'summary': {
503+ 'total': 15,
504+ 'average': 3.0
505+ }
522506}
523507
524508json.dumps(data)
525509`, { language: 'python' });
526510
527511// Access JSON result
528512if (result.results[0]?.json) {
529- const jsonData = result.results[0].json;
530- return Response.json(jsonData);
513+ const jsonData = result.results[0].json;
514+ return Response.json(jsonData);
531515}
532-
533516```
534517</TypeScriptExample >
535518
@@ -576,7 +559,6 @@ if (viz.results[0]?.png) {
576559 // Use chart image...
577560}
578561```
579-
580562</TypeScriptExample >
581563
582564### Multi-language workflow
@@ -591,8 +573,8 @@ import json
591573
592574data = [1, 2, 3, 4, 5]
593575result = {
594- 'sum': sum(data),
595- 'avg': sum(data) / len(data)
576+ 'sum': sum(data),
577+ 'avg': sum(data) / len(data)
596578}
597579
598580# Write results to file
@@ -615,7 +597,6 @@ data;
615597`, { language: 'javascript' });
616598
617599console.log(jsResult.logs.stdout);
618-
619600```
620601</TypeScriptExample >
621602
@@ -662,7 +643,6 @@ if (analysis.results[0]) {
662643 console.log('Result:', analysis.results[0].text);
663644}
664645```
665-
666646</TypeScriptExample >
667647
668648### Real-time execution monitoring
@@ -684,11 +664,10 @@ print("Complete!")
684664 onStdout: (output) => {
685665 if (output.text.startsWith('Step')) {
686666 progress++;
687- console.log(`Progress: ${progress \ * 10}%`);
688- }
689- }
667+ console.log(`Progress: ${progress * 10}%`);
668+ }
669+ }
690670});
691-
692671```
693672</TypeScriptExample >
694673
@@ -731,7 +710,6 @@ try {
731710 console.error('Failed:', error.message);
732711}
733712```
734-
735713</TypeScriptExample >
736714
737715## Error Handling
@@ -740,16 +718,14 @@ try {
740718
741719The Code Interpreter returns structured error information:
742720
743- <TypeScriptExample >
744- ```
721+ ``` ts
745722interface ExecutionError {
746723 name: string ; // Error type (e.g., 'NameError', 'SyntaxError')
747724 value: string ; // Error message
748725 traceback: string []; // Full stack trace
749726 lineNumber? : number ; // Line number where error occurred
750727}
751728```
752- </TypeScriptExample >
753729
754730** Example error handling:**
755731
@@ -799,7 +775,6 @@ time.sleep(120) # Sleep for 2 minutes
799775 }
800776}
801777```
802-
803778</TypeScriptExample >
804779
805780### Context Not Ready
@@ -836,7 +811,6 @@ await sandbox.runCode('df.describe()', { context: ctx });
836811await sandbox.runCode('import pandas as pd', { language: 'python' });
837812await sandbox.runCode('df = pd.read_csv("data.csv")', { language: 'python' }); // Import lost!
838813```
839-
840814</TypeScriptExample >
841815
842816### 2. Set appropriate timeouts
@@ -889,13 +863,12 @@ const result = await sandbox.runCode(userCode, {
889863});
890864
891865if (result.error) {
892- return Response.json({
893- success: false,
894- error: result.error.value,
895- traceback: result.error.traceback
896- }, { status: 400 });
866+ return Response.json({
867+ success: false,
868+ error: result.error.value,
869+ traceback: result.error.traceback
870+ }, { status: 400 });
897871}
898-
899872```
900873</TypeScriptExample >
901874
@@ -918,7 +891,6 @@ await sandbox.runCode(longRunningCode, {
918891 }
919892});
920893```
921-
922894</TypeScriptExample >
923895
924896## Related Resources
0 commit comments