@getmegabrain/cli 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -13,9 +13,14 @@ cell are visible in the next.
|
|
|
13
13
|
import contextlib
|
|
14
14
|
import io
|
|
15
15
|
import json
|
|
16
|
+
import os
|
|
16
17
|
import sys
|
|
17
18
|
import traceback
|
|
18
19
|
|
|
20
|
+
# Render matplotlib to a non-interactive backend so plt.show() never blocks; we capture
|
|
21
|
+
# figures ourselves after each cell (see emit_figures). Set before any user import.
|
|
22
|
+
os.environ.setdefault("MPLBACKEND", "Agg")
|
|
23
|
+
|
|
19
24
|
# Persistent namespace shared across all cells in this session.
|
|
20
25
|
NAMESPACE: dict = {"__name__": "__mb_science__"}
|
|
21
26
|
|
|
@@ -25,6 +30,30 @@ def emit(frame: dict) -> None:
|
|
|
25
30
|
sys.stdout.flush()
|
|
26
31
|
|
|
27
32
|
|
|
33
|
+
def emit_figures(cell_id: str) -> int:
|
|
34
|
+
"""Emit any open matplotlib figures as base64 PNG image frames, then close them.
|
|
35
|
+
|
|
36
|
+
This is what makes plots appear inline in the transcript (a science-notebook staple).
|
|
37
|
+
No-op if matplotlib hasn't been used in this session."""
|
|
38
|
+
pyplot = sys.modules.get("matplotlib.pyplot")
|
|
39
|
+
if pyplot is None:
|
|
40
|
+
return 0
|
|
41
|
+
import base64
|
|
42
|
+
|
|
43
|
+
count = 0
|
|
44
|
+
try:
|
|
45
|
+
for num in pyplot.get_fignums():
|
|
46
|
+
buf = io.BytesIO()
|
|
47
|
+
pyplot.figure(num).savefig(buf, format="png", bbox_inches="tight", dpi=110)
|
|
48
|
+
data = base64.b64encode(buf.getvalue()).decode("ascii")
|
|
49
|
+
emit({"type": "image", "cellId": cell_id, "mime": "image/png", "data": data})
|
|
50
|
+
count += 1
|
|
51
|
+
pyplot.close("all")
|
|
52
|
+
except Exception: # noqa: BLE001 - figure capture is best-effort
|
|
53
|
+
pass
|
|
54
|
+
return count
|
|
55
|
+
|
|
56
|
+
|
|
28
57
|
def run_cell(cell_id: str, code: str) -> None:
|
|
29
58
|
emit({"type": "status", "cellId": cell_id, "state": "busy"})
|
|
30
59
|
stdout, stderr = io.StringIO(), io.StringIO()
|
|
@@ -41,6 +70,11 @@ def run_cell(cell_id: str, code: str) -> None:
|
|
|
41
70
|
emit({"type": "stream", "cellId": cell_id, "kind": "stdout", "data": out})
|
|
42
71
|
if err:
|
|
43
72
|
emit({"type": "stream", "cellId": cell_id, "kind": "stderr", "data": err})
|
|
73
|
+
figures = emit_figures(cell_id)
|
|
74
|
+
if figures:
|
|
75
|
+
# A note in stdout so the (text-only) model knows a figure was produced.
|
|
76
|
+
emit({"type": "stream", "cellId": cell_id, "kind": "stdout",
|
|
77
|
+
"data": f"[{figures} figure(s) displayed]\n"})
|
|
44
78
|
emit({"type": "status", "cellId": cell_id, "state": "idle"})
|
|
45
79
|
|
|
46
80
|
|
package/dist/science/pages.js
CHANGED
|
@@ -307,6 +307,8 @@ export function sessionPage(projectId, sessionId) {
|
|
|
307
307
|
white-space:pre-wrap; overflow-x:auto; border-top:1px solid var(--border); }
|
|
308
308
|
.step .out { color:var(--muted); background:var(--bg); }
|
|
309
309
|
.step .out.err { color:#c0392b; }
|
|
310
|
+
img.figure { max-width:calc(100% - 24px); border-radius:8px; margin:10px 12px; display:block;
|
|
311
|
+
background:#fff; border:1px solid var(--border); }
|
|
310
312
|
/* Composer */
|
|
311
313
|
.composer { flex-shrink:0; border-top:1px solid var(--border); padding:14px 20px; }
|
|
312
314
|
.composer-inner { max-width:760px; margin:0 auto; display:flex; gap:10px; align-items:flex-end;
|
|
@@ -64,6 +64,18 @@
|
|
|
64
64
|
if (stderr) out.classList.add('err');
|
|
65
65
|
scroll();
|
|
66
66
|
}
|
|
67
|
+
// Inline a rendered figure (matplotlib PNG) under the current step, or on its own if none.
|
|
68
|
+
function addFigure(mime, data) {
|
|
69
|
+
var img = el('img', 'figure');
|
|
70
|
+
img.src = 'data:' + mime + ';base64,' + data;
|
|
71
|
+
if (lastCard) lastCard.appendChild(img);
|
|
72
|
+
else {
|
|
73
|
+
var row = el('div', 'msg agent');
|
|
74
|
+
row.appendChild(img);
|
|
75
|
+
transcript.appendChild(row);
|
|
76
|
+
}
|
|
77
|
+
scroll();
|
|
78
|
+
}
|
|
67
79
|
|
|
68
80
|
function setRunning(on) {
|
|
69
81
|
sendBtn.disabled = on;
|
|
@@ -112,6 +124,7 @@
|
|
|
112
124
|
if (m.channel === 'kernel') {
|
|
113
125
|
var f = m.payload;
|
|
114
126
|
if (f.type === 'status') kstate.textContent = f.state === 'busy' ? 'running' : f.state;
|
|
127
|
+
else if (f.type === 'image') addFigure(f.mime, f.data);
|
|
115
128
|
return;
|
|
116
129
|
}
|
|
117
130
|
var p = m.payload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getmegabrain/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "MegaBrain CLI — sign in once, then code from your terminal via OpenCode wired to the MegaBrain Gateway, or run `megabrain science` for the local research workbench.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|