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

Function build_local_python

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

Build the Python script for local execution (no Jupyter, no IPython). Returns a script that prints exactly one line `WEFT_RESULT_JSON= ` to stdout, surrounded by whatever the user code printed. The host parses that marker line out. Dependencies are intentionally ignored: in local mode the user runs against their own Python environment and installs their own libs.

(user_code: &str, input: &Value)

Source from the content-addressed store, hash-verified

268/// marker line out. Dependencies are intentionally ignored: in local mode the
269/// user runs against their own Python environment and installs their own libs.
270fn build_local_python(user_code: &str, input: &Value) -> String {
271 let (star_imports, prelude, body) = build_user_block(user_code, input);
272
273 format!(
274 r#"
275import json as __weft_json__
276import sys as __weft_sys__
277
278{star_imports}
279
280{prelude}
281
282def __weft_user_code__():
283{body}
284
285__weft_result__ = __weft_user_code__()
286if __weft_result__ is None:
287 __weft_result__ = {{}}
288if not isinstance(__weft_result__, dict):
289 raise ValueError("Code must return a dict with output port names as keys, got: " + str(type(__weft_result__)))
290
291# Leading \n is required so the host parser can anchor on `\n<marker>=` even
292# when the user's last print() had end="" (no trailing newline). json.dumps
293# default output is single-line, so the whole marker fits on one line.
294__weft_sys__.stdout.write("\nWEFT_RESULT_JSON=" + __weft_json__.dumps(__weft_result__) + "\n")
295__weft_sys__.stdout.flush()
296"#,
297 star_imports = star_imports,
298 prelude = prelude,
299 body = body,
300 )
301}
302
303/// Build a `!pip install` line for any deps the user listed. Empty if none.
304/// We don't whitelist or validate package names here: E2B is the trust boundary,

Callers 1

executeMethod · 0.85

Calls 1

build_user_blockFunction · 0.85

Tested by

no test coverage detected