Spaces:
Running
Running
File size: 58,146 Bytes
fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 7bd5726 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 1bb2113 fe3f5b0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 |
'''
Run generated code in a sandbox environment.
Gradio will interact with this module.
'''
from typing import Any, Generator, Literal, TypeAlias, TypedDict, Set
import uuid
import time
import gradio as gr
import base64
from e2b_code_interpreter import Sandbox as CodeSandbox
from gradio_sandboxcomponent import SandboxComponent
from sandbox.sandbox_state import ChatbotSandboxState
from sandbox.code_analyzer import SandboxEnvironment, extract_code_from_markdown, extract_installation_commands, extract_java_class_name, extract_js_imports, extract_python_imports, replace_placeholder_urls, validate_dependencies
from sandbox.prompts import (
DEFAULT_C_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_CPP_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_GOLANG_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_GRADIO_SANDBOX_INSTRUCTION, DEFAULT_HTML_SANDBOX_INSTRUCTION, DEFAULT_JAVA_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_JAVASCRIPT_RUNNER_INSTRUCTION, DEFAULT_MERMAID_SANDBOX_INSTRUCTION, DEFAULT_PYGAME_SANDBOX_INSTRUCTION, DEFAULT_PYTHON_RUNNER_INSTRUCTION, DEFAULT_REACT_SANDBOX_INSTRUCTION, DEFAULT_RUST_CODE_RUN_SANDBOX_INSTRUCTION, DEFAULT_STREAMLIT_SANDBOX_INSTRUCTION, DEFAULT_VUE_SANDBOX_INSTRUCTION, GENERAL_SANDBOX_INSTRUCTION
)
from sandbox.sandbox_telemetry import log_sandbox_telemetry_gradio_fn
from .constants import CODE_RUN_TIMEOUT_SECONDS, E2B_API_KEY, SANDBOX_TEMPLATE_ID, SANDBOX_NGINX_PORT
from .sandbox_manager import get_sandbox_app_url, create_sandbox, install_npm_dependencies, install_pip_dependencies, reuse_or_create_sandbox, run_background_command_with_timeout, run_command_in_sandbox
SUPPORTED_SANDBOX_ENVIRONMENTS: list[str] = [
env.value for env in SandboxEnvironment
]
WEB_UI_SANDBOX_ENVIRONMENTS = [
SandboxEnvironment.HTML,
SandboxEnvironment.REACT,
SandboxEnvironment.VUE,
SandboxEnvironment.GRADIO,
SandboxEnvironment.STREAMLIT,
# SandboxEnvironment.NICEGUI,
SandboxEnvironment.PYGAME,
SandboxEnvironment.MERMAID
]
'''
Sandbox environments that can be rendered in the web UI.
'''
VALID_GRADIO_CODE_LANGUAGES = [
'python', 'c', 'cpp', 'markdown', 'json', 'html', 'css', 'javascript', 'jinja2', 'typescript', 'yaml', 'dockerfile', 'shell', 'r', 'sql',
'sql-msSQL', 'sql-mySQL', 'sql-mariaDB', 'sql-sqlite', 'sql-cassandra', 'sql-plSQL', 'sql-hive', 'sql-pgSQL', 'sql-gql', 'sql-gpSQL', 'sql-sparkSQL',
'sql-esper'
]
'''
Languages that gradio code component can render.
'''
RUN_CODE_BUTTON_HTML = "<button style='background-color: #4CAF50; border: none; color: white; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px;'>Click to Run in Sandbox</button>"
'''
Button in the chat to run the code in the sandbox.
'''
DEFAULT_SANDBOX_INSTRUCTIONS: dict[SandboxEnvironment, str] = {
SandboxEnvironment.AUTO: GENERAL_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.PYTHON_RUNNER: DEFAULT_PYTHON_RUNNER_INSTRUCTION.strip(),
SandboxEnvironment.JAVASCRIPT_RUNNER: DEFAULT_JAVASCRIPT_RUNNER_INSTRUCTION.strip(),
SandboxEnvironment.HTML: DEFAULT_HTML_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.REACT: DEFAULT_REACT_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.VUE: DEFAULT_VUE_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.GRADIO: DEFAULT_GRADIO_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.STREAMLIT: DEFAULT_STREAMLIT_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.PYGAME: DEFAULT_PYGAME_SANDBOX_INSTRUCTION.strip(),
SandboxEnvironment.MERMAID: DEFAULT_MERMAID_SANDBOX_INSTRUCTION.strip(),
# Runners
SandboxEnvironment.C_RUNNER: DEFAULT_C_CODE_RUN_SANDBOX_INSTRUCTION,
SandboxEnvironment.CPP_RUNNER: DEFAULT_CPP_CODE_RUN_SANDBOX_INSTRUCTION,
SandboxEnvironment.JAVA_RUNNER: DEFAULT_JAVA_CODE_RUN_SANDBOX_INSTRUCTION,
SandboxEnvironment.GOLANG_RUNNER: DEFAULT_GOLANG_CODE_RUN_SANDBOX_INSTRUCTION,
SandboxEnvironment.RUST_RUNNER: DEFAULT_RUST_CODE_RUN_SANDBOX_INSTRUCTION,
}
SandboxGradioSandboxComponents: TypeAlias = tuple[
gr.Markdown | Any, # sandbox_output_md
SandboxComponent | Any, # sandbox_ui
gr.Code | Any, # sandbox_code
Any
]
'''
Gradio components for the sandbox.
'''
class CodeRunResult(TypedDict):
'''
The result of running the code in the sandbox.
'''
sandbox_id: str
'''
The sandbox id to run the code.
'''
sandbox_url: str
'''
The sandbox url to access the rendered results.
'''
is_run_success: bool
'''
Whether the code run is successful.
'''
stderr: str
'''
The stderr output from the sandbox.
'''
def create_chatbot_sandbox_state(btn_list_length: int = 5) -> ChatbotSandboxState:
'''
Create a new sandbox state for a chatbot.
'''
return {
'enable_sandbox': True, # Always enabled
'enabled_round': 0,
'sandbox_run_round': 0,
'edit_round': 0,
'sandbox_environment': SandboxEnvironment.AUTO,
'auto_selected_sandbox_environment': None,
'sandbox_instruction': DEFAULT_SANDBOX_INSTRUCTIONS[SandboxEnvironment.AUTO],
'code_to_execute': "",
'code_language': None,
'install_command': "",
'btn_list_length': btn_list_length,
'sandbox_id': None,
'chat_session_id': None,
'conv_id': None,
"sandbox_output": None,
"sandbox_error": None,
}
def set_sandbox_state_ids(
sandbox_state: ChatbotSandboxState,
conv_id: str,
chat_session_id: str,
) -> ChatbotSandboxState:
'''
Set the conv_id and chat_session_id in the sandbox state.
'''
sandbox_state['conv_id'] = conv_id
sandbox_state['chat_session_id'] = chat_session_id
return sandbox_state
def reset_sandbox_state(state: ChatbotSandboxState) -> ChatbotSandboxState:
'''
Reset the sandbox state.
Used when the chatbot session is reset.
'''
# reset rounds
state['enabled_round'] = 0
state['sandbox_run_round'] = 0
state['edit_round'] = 0
# state['sandbox_environment'] = SandboxEnvironment.AUTO
state['auto_selected_sandbox_environment'] = None
state['sandbox_instruction'] = DEFAULT_SANDBOX_INSTRUCTIONS[SandboxEnvironment.AUTO]
state['code_to_execute'] = ""
state['code_language'] = None
state['install_command'] = ""
state['sandbox_error'] = None
state['sandbox_output'] = None
# reset ids
state['sandbox_id'] = None
state['conv_id'] = None
state['chat_session_id'] = None
return state
def update_sandbox_config_multi(
enable_sandbox: bool,
sandbox_environment: SandboxEnvironment,
*states: ChatbotSandboxState
) -> list[ChatbotSandboxState]:
'''
Fn to update sandbox config.
'''
return [
update_sandbox_config(enable_sandbox, sandbox_environment, state)
for state
in states
]
def update_sandbox_state_system_prompt(sandbox_state: ChatbotSandboxState, system_prompt: str):
if sandbox_state['enabled_round'] == 0:
sandbox_state['sandbox_instruction'] = system_prompt
return sandbox_state
def update_sandbox_config(
enable_sandbox: bool,
sandbox_environment: SandboxEnvironment,
state: ChatbotSandboxState
) -> ChatbotSandboxState:
'''
Fn to update sandbox config for single model.
'''
state["enable_sandbox"] = enable_sandbox
state["sandbox_environment"] = sandbox_environment
state['sandbox_instruction'] = DEFAULT_SANDBOX_INSTRUCTIONS.get(sandbox_environment, None)
return state
def update_visibility(visible):
return [gr.update(visible=visible)] *14
def update_visibility_for_single_model(visible: bool, component_cnt: int):
return [gr.update(visible=visible)] * component_cnt
def mermaid_to_html(mermaid_code: str, theme: str = 'default') -> str:
"""
Convert Mermaid diagram code to a minimal HTML document.
Args:
mermaid_code: The Mermaid diagram syntax
theme: Theme name ('default', 'dark', 'forest', 'neutral', etc.)
Returns:
str: Complete HTML document with embedded Mermaid diagram
"""
html_template = f'''<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>
<script>
mermaid.initialize({{
startOnLoad: true,
theme: '{theme}',
securityLevel: 'loose',
fontFamily: 'Arial, sans-serif'
}});
</script>
</head>
<body>
<div class="mermaid">
{mermaid_code}
</div>
</body>
</html>'''
return html_template
def javascript_to_html(javascript_code: str) -> str:
"""
Convert JavaScript code to a minimal HTML document that executes the code.
Args:
javascript_code: The JavaScript code to embed
Returns:
str: Complete HTML document with embedded JavaScript code
"""
html_template = f'''<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript Code Execution</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f5f5f5;
}}
#output {{
background-color: white;
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
margin-top: 20px;
white-space: pre-wrap;
font-family: monospace;
}}
.error {{
color: red;
}}
.log {{
color: black;
}}
</style>
</head>
<body>
<h1>JavaScript Code Execution</h1>
<div id="output"></div>
<script>
// Override console methods to capture output
const outputDiv = document.getElementById('output');
const originalConsole = {{}};
['log', 'error', 'warn', 'info'].forEach(function(method) {{
originalConsole[method] = console[method];
console[method] = function(...args) {{
const message = args.map(function(arg) {{
return typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg);
}}).join(' ');
const span = document.createElement('span');
span.className = method === 'error' ? 'error' : 'log';
span.textContent = '[' + method.toUpperCase() + '] ' + message + '\\n';
outputDiv.appendChild(span);
// Also call original console method
originalConsole[method].apply(console, args);
}};
}});
// Capture uncaught errors
window.addEventListener('error', function(e) {{
const span = document.createElement('span');
span.className = 'error';
span.textContent = '[ERROR] ' + e.message + ' at line ' + e.lineno + '\\n';
outputDiv.appendChild(span);
}});
try {{
// Execute the user's JavaScript code
{javascript_code}
}} catch (error) {{
console.error('Execution error:', error.message);
}}
</script>
</body>
</html>'''
return html_template
def render_result(result):
if result.png:
if isinstance(result.png, str):
img_str = result.png
else:
img_str = base64.b64encode(result.png).decode()
return f""
elif result.jpeg:
if isinstance(result.jpeg, str):
img_str = result.jpeg
else:
img_str = base64.b64encode(result.jpeg).decode()
return f""
elif result.svg:
if isinstance(result.svg, str):
svg_data = result.svg
else:
svg_data = result.svg.decode()
svg_base64 = base64.b64encode(svg_data.encode()).decode()
return f""
elif result.html:
return result.html
elif result.markdown:
return f"```markdown\n{result.markdown}\n```"
elif result.latex:
return f"```latex\n{result.latex}\n```"
elif result.json:
return f"```json\n{result.json}\n```"
elif result.javascript:
return result.javascript # Return raw JavaScript
else:
return str(result)
def run_code_interpreter(code: str, code_language: str | None, install_command: str) -> tuple[str, str]:
"""
Executes the provided code within a sandboxed environment and returns the output.
Args:
code (str): The code to be executed.
"""
sandbox = CodeSandbox()
stderrs = []
# Run install command if provided
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
execution = sandbox.run_code(
code=code,
language=code_language
)
# collect stdout, stderr from sandbox
stdout = "\n".join(execution.logs.stdout)
stderr = "\n".join(execution.logs.stderr)
if execution.error:
stderr += f"\n{execution.error.name}: {execution.error.value}"
output = ""
if stdout:
output += f"```markdown\n{stdout}\n```\n\n"
stderrs.append(stderr)
results = []
for result in execution.results:
if result.html or result.javascript:
# TODO: fix this
continue
# with open('html_code.html', 'w') as f:
# f.write(result.html)
# url, _ = run_html_sandbox(result.html, ([], extract_js_imports(result.html)))
else:
rendered_result = render_result(result)
results.append(rendered_result)
if results:
output += "\n### Results:\n" + "\n".join(results)
stderrs = '\n'.join(stderrs)
return output, "" if output else stderrs
def run_html_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> tuple[str, str, str]:
"""
Executes the provided code within a sandboxed environment and returns the output.
Supports both React and Vue.js rendering in HTML files.
Args:
code (str): The code to be executed.
install_command (str): Bash command to install dependencies
Returns:
tuple: (sandbox_url, sandbox_id, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
project_root = "~/html_app"
sandbox.files.make_dir(project_root)
# Run install command if provided
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
)
if not is_success:
print(f"Install command failed: {stderr}")
return "", sandbox.sandbox_id, '\n'.join(stderr)
# replace placeholder URLs with SVG data URLs
code = replace_placeholder_urls(code)
file_path = f"{project_root}/index.html"
sandbox.files.write(file_path, code, "user", 60)
sandbox_url = get_sandbox_app_url(sandbox, 'html')
return (sandbox_url, sandbox.sandbox_id, '')
def run_react_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> CodeRunResult:
"""
Executes the provided code within a sandboxed environment and returns the output.
Args:
code (str): The code to be executed.
install_command (str): Bash command to install dependencies
Returns:
url for remote sandbox
"""
project_root = "~/react_app"
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
stderrs: list[str] = [] # to collect errors
# replace placeholder URLs with SVG data URLs
code = replace_placeholder_urls(code)
# set up the sandbox directory structure first
print("Setting up sandbox directory structure...")
file_path = "~/react_app/src/App.tsx"
sandbox.files.write(file_path, code, "user", 60)
print("Code files written successfully.")
# Run install command AFTER setting up the project structure
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
working_directory=project_root, # Run in the correct directory
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
# Don't return early - continue with build attempt
stderrs.append(f"Install command failed: {' '.join(stderr)}")
# Attempt to build the React app
is_run_success, _, build_stderrs = run_command_in_sandbox(
sandbox=sandbox,
command="npm run build --loglevel=error -- --mode development --logLevel error",
working_directory=project_root,
)
stderrs.extend(build_stderrs)
# Always try to get the sandbox URL, even if build failed
sandbox_url = get_sandbox_app_url(sandbox, 'react')
# If build failed but we have a sandbox, still return the URL
if not is_run_success and sandbox_url:
is_run_success = True # Consider it successful if we have a working sandbox
return {
'sandbox_id': sandbox.sandbox_id,
'sandbox_url': sandbox_url,
'is_run_success': is_run_success,
'stderr': '\n'.join(stderrs),
}
def run_vue_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> CodeRunResult:
"""
Executes the provided Vue code within a sandboxed environment and returns the output.
Args:
code (str): The Vue code to be executed.
install_command (str): Bash command to install dependencies
Returns:
url for remote sandbox
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
project_root = "~/vue_app"
stderrs: list[str] = [] # to collect errors
# replace placeholder URLs with SVG data URLs
code = replace_placeholder_urls(code)
# Set up the sandbox directory structure first
file_path = "~/vue_app/src/App.vue"
sandbox.files.write(file_path, code, "user", 60)
# Run install command AFTER setting up the project structure
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
working_directory=project_root, # Run in the correct directory
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
# Don't return early - continue with build attempt
stderrs.append(f"Install command failed: {' '.join(stderr)}")
# Attempt to build the Vue app
is_run_success, _, build_stderrs = run_command_in_sandbox(
sandbox=sandbox,
command="npm run build --loglevel=error -- --mode development --logLevel error",
working_directory=project_root,
)
stderrs.extend(build_stderrs)
# Always try to get the sandbox URL, even if build failed
sandbox_url = get_sandbox_app_url(sandbox, 'vue')
# If build failed but we have a sandbox, still return the URL
if not is_run_success and sandbox_url:
print(f"β οΈ Build failed but sandbox is available at: {sandbox_url}")
is_run_success = True # Consider it successful if we have a working sandbox
return {
'sandbox_id': sandbox.sandbox_id,
'sandbox_url': sandbox_url,
'is_run_success': is_run_success,
'stderr': '\n'.join(stderrs),
}
def run_pygame_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> CodeRunResult:
"""
Executes the provided code within a sandboxed environment and returns the output.
Args:
code (str): The code to be executed.
install_command (str): Bash command to install dependencies
Returns:
url for remote sandbox
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
project_root = "~/pygame_app"
file_path = f"{project_root}/main.py"
stderrs = []
# Set up the sandbox directory structure first
sandbox.files.write(file_path, code, "user", 60)
# Run install command AFTER setting up the project structure
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
working_directory=project_root, # Run in the correct directory
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
# Don't return early - continue with build attempt
stderrs.append(f"Install command failed: {' '.join(stderr)}")
# Attempt to build the pygame code
is_run_success, _, build_stderrs = run_command_in_sandbox(
sandbox=sandbox,
command="pygbag --build ~/pygame_app",
)
stderrs.extend(build_stderrs)
# Always try to get the sandbox URL, even if build failed
sandbox_url = get_sandbox_app_url(sandbox, 'pygame')
# If build failed but we have a sandbox, still return the URL
if not is_run_success and sandbox_url:
print(f"β οΈ Build failed but sandbox is available at: {sandbox_url}")
is_run_success = True # Consider it successful if we have a working sandbox
return {
'sandbox_id': sandbox.sandbox_id,
'sandbox_url': sandbox_url,
'is_run_success': is_run_success,
'stderr': '\n'.join(stderrs),
}
def run_gradio_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> tuple[str, str, str]:
"""
Executes the provided code within a sandboxed environment and returns the output.
Args:
code (str): The code to be executed.
install_command (str): Bash command to install dependencies
Returns:
url for remote sandbox and sandbox id
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/gradio_app/main.py"
sandbox.files.write(file_path, code, "user", 60)
stderrs = []
# Run install command if provided
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
return "", sandbox.sandbox_id, '\n'.join(stderr)
stderr = run_background_command_with_timeout(
sandbox,
f"python {file_path}",
timeout=10,
)
stderrs.append(stderr)
sandbox_url = 'https://' + sandbox.get_host(7860)
return (sandbox_url, sandbox.sandbox_id, '\n'.join(stderrs))
def run_streamlit_sandbox(code: str, install_command: str, existing_sandbox_id: str | None = None) -> tuple[str, str, str]:
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
stderrs = []
sandbox.files.make_dir('mystreamlit')
file_path = "~/mystreamlit/app.py"
sandbox.files.write(file_path, code, "user", 60)
# Run install command if provided
if install_command.strip():
is_success, stdout, stderr = run_command_in_sandbox(
sandbox=sandbox,
command=install_command,
timeout=60 * 3,
)
if stderr:
stderrs.extend(stderr)
if not is_success:
print(f"Install command failed: {stderr}")
return "", sandbox.sandbox_id, '\n'.join(stderr)
stderr = run_background_command_with_timeout(
sandbox,
r"sudo kill -9 $(ss -lptn 'sport = :8501' | grep -oP '(?<=pid=)\d+'); streamlit run ~/mystreamlit/app.py --server.port 8501 --server.headless true",
timeout=8,
)
stderrs.append(stderr)
host = sandbox.get_host(port=8501)
url = f"https://{host}"
return (url, sandbox.sandbox_id, '\n'.join(stderrs))
def run_c_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
"""
Executes the provided C code within a sandboxed environment and returns the output.
Args:
code (str): The C code to be executed.
Returns:
tuple: (stdout, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.c"
sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox(
sandbox=sandbox,
command=f"gcc {file_path} -o ~/main && ./main",
timeout=CODE_RUN_TIMEOUT_SECONDS,
)
# collect stdout, stderr from sandbox
stdout = "\n".join(stdouts)
stderr = "\n".join(stderrs)
return stdout, stderr
def run_cpp_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
"""
Executes the provided C++ code within a sandboxed environment and returns the output.
Args:
code (str): The C++ code to be executed.
Returns:
tuple: (stdout, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.cpp"
sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox(
sandbox=sandbox,
command=f"g++ {file_path} -o ~/main && ./main",
timeout=CODE_RUN_TIMEOUT_SECONDS,
)
# collect stdout, stderr from sandbox
stdout = "\n".join(stdouts)
stderr = "\n".join(stderrs)
return stdout, stderr
def run_java_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
"""
Executes the provided Java code within a sandboxed environment and returns the output.
Args:
code (str): The Java code to be executed.
Returns:
tuple: (stdout, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
class_name = extract_java_class_name(code)
file_path = f"~/{class_name}.java"
sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox(
sandbox=sandbox,
command=f"javac {file_path} && java {class_name}",
timeout=CODE_RUN_TIMEOUT_SECONDS,
)
# collect stdout, stderr from sandbox
stdout = "\n".join(stdouts)
stderr = "\n".join(stderrs)
return stdout, stderr
def run_golang_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
"""
Executes the provided Go code within a sandboxed environment and returns the output.
Args:
code (str): The Go code to be executed
Returns:
tuple: (stdout, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.go"
sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox(
sandbox=sandbox,
command=f"go run {file_path}",
timeout=CODE_RUN_TIMEOUT_SECONDS,
)
# collect stdout, stderr from sandbox
stdout = "\n".join(stdouts)
stderr = "\n".join(stderrs)
return stdout, stderr
# def run_csharp_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
# """
# Executes the provided C# code within a sandboxed environment and returns the output.
# Args:
# code (str): The C# code to be executed
# Returns:
# tuple: (stdout, stderr)
# """
# sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
# file_path = "~/main.cs"
# sandbox.files.write(file_path, code, "user", 60)
# is_success, stdouts, stderrs = run_command_in_sandbox(
# sandbox=sandbox,
# command=f"mcs {file_path} && mono main.exe",
# timeout=CODE_RUN_TIMEOUT_SECONDS,
# )
# # collect stdout, stderr from sandbox
# stdout = "\n".join(stdouts)
# stderr = "\n".join(stderrs)
# return stdout, stderr
def run_rust_code(code: str, existing_sandbox_id: str | None = None) -> tuple[str, str]:
"""
Executes the provided Rust code within a sandboxed environment and returns the output.
Args:
code (str): The Rust code to be executed
Returns:
tuple: (stdout, stderr)
"""
sandbox = reuse_or_create_sandbox(sandbox_id=existing_sandbox_id)
file_path = "~/main.rs"
sandbox.files.write(file_path, code, "user", 60)
is_success, stdouts, stderrs = run_command_in_sandbox(
sandbox=sandbox,
command=f"rustc {file_path} && ./main",
timeout=CODE_RUN_TIMEOUT_SECONDS,
)
# collect stdout, stderr from sandbox
stdout = "\n".join(stdouts)
stderr = "\n".join(stderrs)
return stdout, stderr
def on_edit_code(
state,
sandbox_state: ChatbotSandboxState,
sandbox_output_md: gr.Markdown,
sandbox_ui: SandboxComponent,
sandbox_code: str,
sandbox_dependency: gr.Dataframe,
) -> Generator[tuple[Any, Any, Any, Any], None, None]:
'''
Gradio Handler when code is edited manually by users.
'''
if sandbox_state['enable_sandbox'] is False:
yield None, None, None, None
return
if len(sandbox_code.strip()) == 0 or sandbox_code == sandbox_state['code_to_execute']:
yield gr.skip(), gr.skip(), gr.skip(), gr.skip()
return
sandbox_state['code_to_execute'] = sandbox_code
# Create empty dependencies dataframe for UI compatibility
dependencies = [["python", "", ""], ["npm", "", ""]]
# Keep existing install command
# No need to update install_command here as it's set from the original message
yield (
gr.skip(), # sandbox_output_md
gr.skip(), # sandbox_ui
gr.skip(), # sandbox_code
gr.update(value=dependencies), # sandbox_dependency
)
yield from on_run_code(
state,
sandbox_state,
sandbox_output_md,
sandbox_ui,
sandbox_code,
sandbox_dependency,
)
def on_edit_dependency(
state,
sandbox_state: ChatbotSandboxState,
sandbox_dependency: gr.Dataframe,
sandbox_output_md: gr.Markdown,
sandbox_ui: SandboxComponent,
sandbox_code: str,
) -> Generator[tuple[Any, Any, Any, Any], None, None]:
"""
Gradio Handler when dependencies are edited manually by users.
Handles version specifications and dependency removal.
"""
if sandbox_state["enable_sandbox"] is False:
yield None, None, None, None
return
# Validate dependencies format
is_valid, error_msg = validate_dependencies(sandbox_dependency)
if not is_valid:
yield (
gr.Markdown(f"Invalid dependencies: {error_msg}"),
gr.skip(),
gr.skip(),
sandbox_dependency, # Return original dataframe
)
return
# Convert dataframe format to separate python and npm lists
python_deps = []
npm_deps = []
for dep in sandbox_dependency:
dep_type, pkg_name, version = dep
pkg_name = pkg_name.strip()
version = version.strip()
# Skip empty rows
if not pkg_name:
continue
if dep_type.lower() == "python":
# Handle Python package with version
if version and version.lower() != "latest":
if not any(op in version for op in ["==", ">=", "<=", "~=", ">", "<"]):
python_deps.append(f"{pkg_name}=={version}")
else:
python_deps.append(f"{pkg_name}{version}")
else:
python_deps.append(pkg_name)
elif dep_type.lower() == "npm":
# Handle NPM package with version
if version and version.lower() != "latest":
if not version.startswith("@"):
version = "@" + version
npm_deps.append(f"{pkg_name}{version}")
else:
npm_deps.append(pkg_name)
# Update sandbox state with new install command
# For now, we'll keep the existing install_command as dependency editing is simplified
# In a full implementation, you might want to convert the dependency edits to install commands
# increase edit round
sandbox_state['edit_round'] += 1
# First yield: Update UI with success message
yield (
gr.Markdown("Dependencies updated successfully"),
gr.skip(), # sandbox_ui
gr.skip(), # sandbox_code
sandbox_dependency, # Return the same dataframe
)
# Second yield: Run code with new dependencies
yield from on_run_code(
state,
sandbox_state,
sandbox_output_md,
sandbox_ui,
sandbox_code,
sandbox_dependency,
)
def on_click_code_message_run(
state,
sandbox_state: ChatbotSandboxState,
sandbox_output_md: gr.Markdown,
sandbox_ui: SandboxComponent,
sandbox_code: str,
sandbox_dependency: gr.Dataframe,
evt: gr.SelectData
) -> Generator[SandboxGradioSandboxComponents, None, None]:
'''
Gradio Handler when run code button in message is clicked. Update Sandbox components.
'''
print("on_click_code_message_run")
if sandbox_state['enable_sandbox'] is False:
yield None, None, None, None
return
if not evt.value.endswith(RUN_CODE_BUTTON_HTML):
yield gr.skip(), gr.skip(), gr.skip(), gr.skip()
return
message = evt.value.replace(RUN_CODE_BUTTON_HTML, "").strip()
extract_result = extract_code_from_markdown(
message=message,
enable_auto_env=sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO
)
if extract_result is None:
yield gr.skip(), gr.skip(), gr.skip(), gr.skip()
return
code, code_language, env_selection, install_command = extract_result
# As sandbox is reused, no need to skip
# if sandbox_state['code_to_execute'] == code and sandbox_state['code_language'] == code_language:
# # skip if no changes
# yield gr.skip(), gr.skip(), gr.skip(), gr.skip()
# return
if code_language == 'tsx':
code_language = 'typescript'
code_language = code_language.lower()
gradio_code_language = code_language.lower() if code_language and code_language.lower(
# ensure gradio supports the code language
) in VALID_GRADIO_CODE_LANGUAGES else None
# Create empty dependencies dataframe for UI compatibility
dependencies = [["python", "", ""], ["npm", "", ""]]
sandbox_state['code_to_execute'] = code
sandbox_state['code_language'] = code_language
sandbox_state['install_command'] = install_command
if sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO:
sandbox_state['auto_selected_sandbox_environment'] = env_selection
# reset edit round
sandbox_state['edit_round'] = 0
yield (
gr.skip(), # sandbox_output_md
gr.skip(), # sandbox_ui
gr.update(value=code, language=gradio_code_language), # sandbox_code
gr.update(value=dependencies) # sandbox_dependency
)
yield from on_run_code(
state,
sandbox_state,
sandbox_output_md,
sandbox_ui,
sandbox_code,
sandbox_dependency,
)
def on_run_code(
state,
sandbox_state: ChatbotSandboxState,
sandbox_output_md: gr.Markdown,
sandbox_ui: SandboxComponent,
sandbox_code: str,
sandbox_dependency: gr.Dataframe,
) -> Generator[tuple[Any, Any, Any, Any], None, None]:
'''
gradio fn when run code button is clicked. Update Sandbox components.
'''
print("on_run_code")
if sandbox_state['enable_sandbox'] is False:
yield None, None, None, None
return
# validate e2b api key
if not E2B_API_KEY:
raise ValueError("E2B_API_KEY is not set in env vars.")
# hide and change value of the current sandbox UI to force refresh the sandbox
# otherwise the sandbox might not change if the url is same
yield (
gr.skip(),
SandboxComponent(
value=('', False, []),
label="Example",
visible=False,
),
gr.skip(),
gr.skip(),
)
code, code_language = sandbox_state['code_to_execute'], sandbox_state['code_language']
if code is None or code_language is None:
yield None, None, None, None
return
gradio_code_language = code_language.lower() if code_language and code_language.lower(
# ensure gradio supports the code language
) in VALID_GRADIO_CODE_LANGUAGES else None
# Get install command from sandbox state
install_command = sandbox_state.get('install_command', '')
# Create empty dependencies dataframe for UI compatibility
dependencies = [["python", "", ""], ["npm", "", ""]]
# Initialize output with loading message
markdown_output_text = "### Sandbox Execution Log\n\n"
yield (
gr.Markdown(
value=markdown_output_text + "π Initializing sandbox environment...", visible=True
),
SandboxComponent(visible=False),
gr.Code(value=code, language=gradio_code_language, visible=True),
gr.update(value=dependencies, visible=True), # Update with unified dependencies
)
# Use auto_selected_sandbox_environment only when in AUTO mode, otherwise use sandbox_environment
sandbox_env = (
sandbox_state['auto_selected_sandbox_environment']
if sandbox_state['sandbox_environment'] == SandboxEnvironment.AUTO
else sandbox_state['sandbox_environment']
)
def update_markdown_output(message: str, clear_output: bool = False):
nonlocal markdown_output_text
if clear_output:
markdown_output_text = ""
markdown_output_text += f"\n{message}"
return (
gr.Markdown(value=markdown_output_text, visible=True, sanitize_html=False),
gr.skip(),
gr.skip(),
gr.skip() # Always include dependencies update
)
sandbox_id: str | None = sandbox_state["sandbox_id"] # the sandbox id
sandbox_output: str = "" # stdout from sandbox
sandbox_error: str = "" # stderr from sandbox
print(f"sandbox_env: {sandbox_env}")
match sandbox_env:
case SandboxEnvironment.HTML:
yield update_markdown_output("π Setting up HTML sandbox...")
sandbox_url, sandbox_id, sandbox_error = run_html_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
if sandbox_error:
yield update_markdown_output("β HTML sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
HTML sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(sandbox_url, True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.REACT:
yield update_markdown_output("π Setting up React sandbox...")
code_run_result = run_react_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr']
if code_run_result['is_run_success'] is False and sandbox_error:
yield update_markdown_output("β React sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
React sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(code_run_result['sandbox_url'], True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.VUE:
yield update_markdown_output("π Setting up Vue sandbox...")
code_run_result = run_vue_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr']
if code_run_result['is_run_success'] is False and code_run_result['stderr']:
yield update_markdown_output("β Vue sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{code_run_result['stderr']}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Vue sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(code_run_result['sandbox_url'], True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.PYGAME:
yield update_markdown_output("π Setting up PyGame sandbox...")
code_run_result = run_pygame_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
sandbox_id, sandbox_error = code_run_result['sandbox_id'], code_run_result['stderr']
if code_run_result['is_run_success'] is False and code_run_result['stderr']:
yield update_markdown_output("β PyGame sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{code_run_result['stderr']}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
PyGame sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(code_run_result['sandbox_url'], True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.GRADIO:
yield update_markdown_output("π Setting up Gradio sandbox...")
sandbox_url, sandbox_id, sandbox_error = run_gradio_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
if sandbox_error:
yield update_markdown_output("β Gradio sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Gradio sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(sandbox_url, True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.STREAMLIT:
yield update_markdown_output("π Setting up Streamlit sandbox...")
sandbox_url, sandbox_id, sandbox_error = run_streamlit_sandbox(
code=code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
if sandbox_error:
yield update_markdown_output("β Streamlit sandbox failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Streamlit sandbox is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(sandbox_url, True, []),
label="Example",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.MERMAID:
yield update_markdown_output("π Setting up Mermaid visualization...")
# Convert Mermaid to HTML at execution time
html_code = mermaid_to_html(code, theme='light')
sandbox_url, sandbox_id, sandbox_error = run_html_sandbox(
code=html_code,
install_command=install_command,
existing_sandbox_id=sandbox_state['sandbox_id'],
)
if sandbox_error:
yield update_markdown_output("β Mermaid visualization failed to render!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Mermaid visualization is ready!", clear_output=True)
yield (
gr.Markdown(value=markdown_output_text, visible=True),
SandboxComponent(
value=(sandbox_url, True, []),
label="Mermaid Diagram",
visible=True,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.PYTHON_RUNNER:
yield update_markdown_output("π Running Python Runner...", clear_output=True)
sandbox_output, sandbox_error = run_code_interpreter(
code=code, code_language='python', install_command=install_command
)
if sandbox_error:
yield update_markdown_output("β Python Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + sandbox_output,
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.JAVASCRIPT_RUNNER:
yield update_markdown_output("π Running JavaScript Runner...", clear_output=True)
# Convert JavaScript code to HTML
html_code = javascript_to_html(code)
# Run the HTML in sandbox
sandbox_url, sandbox_id, sandbox_error = run_html_sandbox(
code=html_code, install_command=install_command, sandbox_id=sandbox_state.get('sandbox_id')
)
# Update sandbox state with the sandbox_id
sandbox_state['sandbox_id'] = sandbox_id
if sandbox_error:
yield update_markdown_output("β JavaScript Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text,
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=(sandbox_url, True, []),
label="JavaScript Sandbox",
visible=True,
key=f"js_sandbox_{int(time.time() * 1000)}",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.C_RUNNER:
yield update_markdown_output("π Running C Runner...", clear_output=True)
sandbox_output, sandbox_error = run_c_code(
code=code, existing_sandbox_id=sandbox_state['sandbox_id']
)
if sandbox_error:
yield update_markdown_output("β C Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```",
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.CPP_RUNNER:
yield update_markdown_output("π Running C++ Runner...", clear_output=True)
sandbox_output, sandbox_error = run_cpp_code(
code=code, existing_sandbox_id=sandbox_state['sandbox_id']
)
if sandbox_error:
yield update_markdown_output("β C++ Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```",
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.JAVA_RUNNER:
yield update_markdown_output("π Running Java Runner...", clear_output=True)
sandbox_output, sandbox_error = run_java_code(
code=code, existing_sandbox_id=sandbox_state['sandbox_id']
)
if sandbox_error:
yield update_markdown_output("β Java Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```",
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case SandboxEnvironment.GOLANG_RUNNER:
yield update_markdown_output("π Running Go Runner...", clear_output=True)
sandbox_output, sandbox_error = run_golang_code(
code=code, existing_sandbox_id=sandbox_state['sandbox_id']
)
if sandbox_error:
yield update_markdown_output("β Go Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```",
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
# case SandboxEnvironment.CSHARP_RUNNER:
# yield update_markdown_output("π Running C# Runner...", clear_output=True)
# output, stderr = run_csharp_code(
# code=code, existing_sandbox_id=sandbox_state['sandbox_id']
# )
# yield update_markdown_output("β
Code execution is ready!", clear_output=True)
# if output:
# yield update_markdown_output(f"### Stdout:\n```markdown\n{output}\n```\n\n")
# if stderr:
# yield update_markdown_output(f"### Stderr:\n```markdown\n{stderr}\n```\n\n")
case SandboxEnvironment.RUST_RUNNER:
yield update_markdown_output("π Running Rust Runner...", clear_output=True)
sandbox_output, sandbox_error = run_rust_code(
code=code, existing_sandbox_id=sandbox_state['sandbox_id']
)
if sandbox_error:
yield update_markdown_output("β Rust Runner failed to run!", clear_output=True)
yield update_markdown_output(f"<details open><summary><strong>π¨ Stderr</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n")
else:
yield update_markdown_output("β
Code execution is ready!", clear_output=True)
yield (
gr.Markdown(
value=markdown_output_text + "\n\n" + f"```markdown\n{sandbox_output}\n```",
sanitize_html=False,
visible=True,
),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
case _:
yield (
gr.Markdown(value=code, visible=True),
SandboxComponent(
value=("", False, []),
label="Example",
visible=False,
key="newsandbox",
),
gr.skip(),
gr.skip(),
)
sandbox_state['sandbox_run_round'] += 1
sandbox_state["sandbox_output"] = sandbox_output # record sandbox output if exists
sandbox_state["sandbox_error"] = sandbox_error # record sandbox error if exists
# generate a random sandbox id if not exists as some code runners might not return sandbox id
sandbox_state['sandbox_id'] = sandbox_id if sandbox_id else str(uuid.uuid4())
log_sandbox_telemetry_gradio_fn(
sandbox_state=sandbox_state,
sandbox_ui_value=None,
)
print("on_run_code done") |