-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_marimo.py
More file actions
77 lines (59 loc) · 2.34 KB
/
Copy pathexport_marimo.py
File metadata and controls
77 lines (59 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Export marimo notebooks to scripts in parallel."""
import subprocess
import shutil
import sys
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
def export_notebooks(project_dir: Path):
"""Export all marimo notebooks in the given project directory."""
scripts_dir = project_dir / "scripts"
if scripts_dir.exists():
shutil.rmtree(scripts_dir)
scripts_dir.mkdir(exist_ok=True)
# Find notebooks in the project directory
notebooks = [
f.name
for f in project_dir.glob("*.py")
if f.name not in ["main.py", "export_marimo.py"]
]
if not notebooks:
print(f"⚠️ No notebooks found in {project_dir}")
return
def export_notebook(notebook: str) -> tuple[str, bool, str]:
# 使用相對於專案目錄的路徑
output_path = f"./scripts/{notebook}"
cmd = ["uv", "run", "marimo", "export", "script", notebook, "-o", output_path]
# 關鍵:在專案目錄中執行,使用該專案的 uv 環境
result = subprocess.run(
cmd,
capture_output=True,
text=True,
cwd=project_dir, # ⭐ 指定工作目錄
)
success = result.returncode == 0
message = result.stderr if not success else ""
return notebook, success, message
print(f"🚀 Exporting {len(notebooks)} notebooks from {project_dir.name}...")
with ThreadPoolExecutor(max_workers=3) as executor:
futures = {executor.submit(export_notebook, nb): nb for nb in notebooks}
for future in as_completed(futures):
notebook, success, error_msg = future.result()
if success:
print(f"✅ {notebook} -> {project_dir.name}/scripts/{notebook}")
else:
print(f"❌ Failed to export {notebook}")
if error_msg:
print(f" {error_msg}")
print(f"✨ Done with {project_dir.name}!\n")
if __name__ == "__main__":
if len(sys.argv) > 1:
# 指定專案目錄
for project in sys.argv[1:]:
project_path = Path(project)
if project_path.exists():
export_notebooks(project_path)
else:
print(f"❌ Project not found: {project}")
else:
# 當前目錄
export_notebooks(Path("."))