MCPcopy Create free account
hub / github.com/WeaveMindAI/weft / build_e2b_python

Function build_e2b_python

catalog/code/:exec/python/backend.rs:233–263  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
233fn 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#"
239import json as __weft_json__
240from IPython.display import JSON as __weft_JSON__
241
242{pip}
243{star_imports}
244
245{prelude}
246
247def __weft_user_code__():
248{body}
249
250__weft_result__ = __weft_user_code__()
251if __weft_result__ is None:
252 __weft_result__ = {{}}
253if 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

Callers 1

executeMethod · 0.85

Calls 2

build_user_blockFunction · 0.85
pip_install_lineFunction · 0.85

Tested by

no test coverage detected