Build the full Python script that gets sent to E2B's /execute endpoint. The function's return dict is rendered as the cell's last expression wrapped in `IPython.display.JSON(...)`. The Jupyter kernel emits it as a `result` event with the dict in its `json` field, so we never scrape stdout for it. Optional pip deps are installed via `!pip install` at the top.
(user_code: &str, deps_field: &str, input: &Value)
| 231 | /// event with the dict in its `json` field, so we never scrape stdout for it. |
| 232 | /// Optional pip deps are installed via `!pip install` at the top. |
| 233 | fn build_e2b_python(user_code: &str, deps_field: &str, input: &Value) -> String { |
| 234 | let (star_imports, prelude, body) = build_user_block(user_code, input); |
| 235 | let pip_line = pip_install_line(deps_field); |
| 236 | |
| 237 | format!( |
| 238 | r#" |
| 239 | import json as __weft_json__ |
| 240 | from IPython.display import JSON as __weft_JSON__ |
| 241 | |
| 242 | {pip} |
| 243 | {star_imports} |
| 244 | |
| 245 | {prelude} |
| 246 | |
| 247 | def __weft_user_code__(): |
| 248 | {body} |
| 249 | |
| 250 | __weft_result__ = __weft_user_code__() |
| 251 | if __weft_result__ is None: |
| 252 | __weft_result__ = {{}} |
| 253 | if not isinstance(__weft_result__, dict): |
| 254 | raise ValueError("Code must return a dict with output port names as keys, got: " + str(type(__weft_result__))) |
| 255 | |
| 256 | __weft_JSON__(__weft_result__) |
| 257 | "#, |
| 258 | pip = pip_line, |
| 259 | star_imports = star_imports, |
| 260 | prelude = prelude, |
| 261 | body = body, |
| 262 | ) |
| 263 | } |
| 264 | |
| 265 | /// Build the Python script for local execution (no Jupyter, no IPython). |
| 266 | /// Returns a script that prints exactly one line `WEFT_RESULT_JSON=<json>` to |
no test coverage detected