update: 添加nb支持

This commit is contained in:
stellahsr 2023-12-12 17:17:40 +08:00
parent 35c9d744a4
commit a4cef261e0
4 changed files with 32 additions and 1 deletions

View file

@ -5,6 +5,8 @@
import os
import json
import nbformat
from metagpt.const import DATA_PATH
def save_code_file(name: str, code_context: str, file_format: str = "py") -> None:
@ -32,6 +34,8 @@ def save_code_file(name: str, code_context: str, file_format: str = "py") -> Non
data = {"code": code_context}
with open(file_path, "w", encoding="utf-8") as fp:
json.dump(data, fp, indent=2)
elif file_format == "ipynb":
nbformat.write(code_context, file_path)
else:
raise ValueError("Unsupported file format. Please choose 'py' or 'json'.")