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)
| 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. |
| 270 | fn 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#" |
| 275 | import json as __weft_json__ |
| 276 | import sys as __weft_sys__ |
| 277 | |
| 278 | {star_imports} |
| 279 | |
| 280 | {prelude} |
| 281 | |
| 282 | def __weft_user_code__(): |
| 283 | {body} |
| 284 | |
| 285 | __weft_result__ = __weft_user_code__() |
| 286 | if __weft_result__ is None: |
| 287 | __weft_result__ = {{}} |
| 288 | if 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, |
no test coverage detected