feat(foreign): own view sessions and clear outputs
foreign.session returns a handle from ordinary kernel/cell tables; view consumes only the 会话 binding. Sessions live in host foreign_doc. Without 会话, view cells run stateless one-shot. Add /api/session/clear and a Clear outputs button for every view page; clearing keeps persistent kernel state.
This commit is contained in:
1 parent
01468ad976
commit
fb28eee62f
21 files changed
+379
-75
No files matched your search
@@ -1,7 +1,8 @@
|
||||
//! Document session: cells, kernels, execution journal, state store.
|
||||
//!
|
||||
//! The notebook layer reads the conventional `内核表`/`会话表` bindings and the
|
||||
//! bound code fences. It does not change the Markdown parser.
|
||||
//! Stateful cells live here, owned by the foreign runtime. The view reads a
|
||||
//! `会话 = > foreign.session ...` handle; no table names are reserved, and a
|
||||
//! document without that handle gets stateless one-shot cells.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -78,6 +79,8 @@ pub struct JournalRecord {
|
||||
|
||||
pub struct DocumentSession {
|
||||
pub path: PathBuf,
|
||||
root: PathBuf,
|
||||
pub persistent: bool,
|
||||
pub cells: Vec<Cell>,
|
||||
pub kernels: Vec<KernelDecl>,
|
||||
pub order: Vec<String>,
|
||||
@@ -125,10 +128,20 @@ fn table_rows(v: &Value) -> Result<Vec<HashMap<String, Value>>, String> {
|
||||
Ok(out)
|
||||
}
|
||||
Value::Map(cols) => {
|
||||
// A single-row table may evaluate to a map of scalar fields rather
|
||||
// than a column map. Treat that map as one record.
|
||||
if !cols.iter().all(|(_, v)| matches!(v, Value::List(_))) {
|
||||
let row = cols
|
||||
.iter()
|
||||
.filter(|(k, _)| k != "@")
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
return Ok(vec![row]);
|
||||
}
|
||||
let mut width = 0usize;
|
||||
for (name, value) in cols {
|
||||
for (_, value) in cols {
|
||||
let Value::List(xs) = value else {
|
||||
return Err(format!("view table column `{name}` is not a list"));
|
||||
unreachable!();
|
||||
};
|
||||
width = width.max(xs.len());
|
||||
}
|
||||
@@ -150,16 +163,34 @@ fn table_rows(v: &Value) -> Result<Vec<HashMap<String, Value>>, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn table_from_bindings(
|
||||
fn plan_tables(
|
||||
bindings: &HashMap<String, Value>,
|
||||
names: &[&str],
|
||||
) -> Result<Option<Vec<HashMap<String, Value>>>, String> {
|
||||
for name in names {
|
||||
if let Some(value) = bindings.get(*name) {
|
||||
return table_rows(value).map(Some);
|
||||
}
|
||||
) -> Result<(Option<Vec<HashMap<String, Value>>>, Option<Vec<HashMap<String, Value>>>), String> {
|
||||
let Some(plan) = bindings.get("会话") else {
|
||||
return Ok((None, None));
|
||||
};
|
||||
let Value::Map(fields) = plan else {
|
||||
return Err("`会话` must be a foreign.session handle".into());
|
||||
};
|
||||
let field = |name: &str| {
|
||||
fields
|
||||
.iter()
|
||||
.find(|(k, _)| k == name)
|
||||
.map(|(_, v)| v.clone())
|
||||
};
|
||||
let kind = field("kind").map(|v| value_text(&v)).unwrap_or_default();
|
||||
if kind != "foreign-session" {
|
||||
return Err("`会话` must be a foreign.session handle".into());
|
||||
}
|
||||
Ok(None)
|
||||
let kernels = match field("kernels") {
|
||||
Some(Value::None) | None => None,
|
||||
Some(v) => Some(table_rows(&v)?),
|
||||
};
|
||||
let cells = match field("cells") {
|
||||
Some(Value::None) | None => None,
|
||||
Some(v) => Some(table_rows(&v)?),
|
||||
};
|
||||
Ok((kernels, cells))
|
||||
}
|
||||
|
||||
fn col_value<'a>(row: &'a HashMap<String, Value>, names: &[&str]) -> Option<&'a Value> {
|
||||
@@ -201,10 +232,7 @@ fn capture_bindings(
|
||||
host.entry_metadata = module.metadata.clone();
|
||||
let mut interp = crate::interp::Interpreter::with_capture(Some(path), false).with_host(host);
|
||||
interp
|
||||
.capture_declarations(
|
||||
module,
|
||||
&["内核表", "kernels", "Kernels", "会话表", "session", "Session"],
|
||||
)
|
||||
.capture_declarations(module, &["会话"])
|
||||
.map_err(|e| format!("view plan evaluation: {e}"))
|
||||
}
|
||||
|
||||
@@ -281,21 +309,23 @@ impl DocumentSession {
|
||||
collect_cells(&entry.body, &mut cells, &mut seen)?;
|
||||
let declarations = capture_bindings(path, root, &module, &source)?;
|
||||
|
||||
let persistent = declarations.contains_key("会话");
|
||||
let (plan_kernels, plan_cells) = plan_tables(&declarations)?;
|
||||
let mut kernels: Vec<KernelDecl> = Vec::new();
|
||||
let mut kernel_map: HashMap<String, KernelDecl> = HashMap::new();
|
||||
if let Some(rows) = table_from_bindings(&declarations, &["内核表", "kernels", "Kernels"])? {
|
||||
if let Some(rows) = plan_kernels {
|
||||
if rows.is_empty() {
|
||||
return Err("内核表: at least one kernel row is required".into());
|
||||
return Err("foreign.session: at least one kernel row is required".into());
|
||||
}
|
||||
for row in rows {
|
||||
let name = col_text(&row, &["名称", "Name", "name"])
|
||||
.ok_or_else(|| "内核表: missing 名称/Name".to_string())?;
|
||||
.ok_or_else(|| "foreign.session kernels: missing 名称/Name".to_string())?;
|
||||
let lang = col_text(&row, &["语言", "Language", "language", "lang"])
|
||||
.ok_or_else(|| format!("内核表[{name}]: missing 语言/Language"))?;
|
||||
.ok_or_else(|| format!("foreign.session[{name}]: missing 语言/Language"))?;
|
||||
let lang = registry::canonical(&lang);
|
||||
let cmd = col_text(&row, &["命令", "Command", "command", "cmd"]);
|
||||
if kernel_map.contains_key(&name) {
|
||||
return Err(format!("内核表: kernel `{name}` is declared twice"));
|
||||
return Err(format!("foreign.session: kernel `{name}` is declared twice"));
|
||||
}
|
||||
let decl = KernelDecl {
|
||||
name: name.clone(),
|
||||
@@ -309,10 +339,10 @@ impl DocumentSession {
|
||||
}
|
||||
|
||||
let mut order: Vec<String> = Vec::new();
|
||||
let session_rows = table_from_bindings(&declarations, &["会话表", "session", "Session"])?;
|
||||
let session_rows = plan_cells;
|
||||
if let Some(rows) = session_rows {
|
||||
if rows.is_empty() {
|
||||
return Err("会话表: at least one cell row is required".into());
|
||||
return Err("foreign.session cells: at least one cell row is required".into());
|
||||
}
|
||||
let by_name: HashMap<String, usize> = cells
|
||||
.iter()
|
||||
@@ -322,19 +352,19 @@ impl DocumentSession {
|
||||
let mut listed = HashSet::new();
|
||||
for row in rows {
|
||||
let cell_name = col_text(&row, &["单元", "Cell", "cell", "单元名"])
|
||||
.ok_or_else(|| "会话表: missing 单元/Cell".to_string())?;
|
||||
.ok_or_else(|| "foreign.session cells: missing 单元/Cell".to_string())?;
|
||||
let kernel_name = col_text(&row, &["内核", "Kernel", "kernel"])
|
||||
.ok_or_else(|| format!("会话表[{cell_name}]: missing 内核/Kernel"))?;
|
||||
.ok_or_else(|| format!("foreign.session[{cell_name}]: missing 内核/Kernel"))?;
|
||||
let Some(&ci) = by_name.get(&cell_name) else {
|
||||
return Err(format!("会话表: unknown cell `{cell_name}`"));
|
||||
return Err(format!("foreign.session: unknown cell `{cell_name}`"));
|
||||
};
|
||||
if !listed.insert(cell_name.clone()) {
|
||||
return Err(format!("会话表: cell `{cell_name}` is listed twice"));
|
||||
return Err(format!("foreign.session: cell `{cell_name}` is listed twice"));
|
||||
}
|
||||
let kernel = resolve_kernel(&mut kernels, &mut kernel_map, &kernel_name)?;
|
||||
if registry::canonical(&cells[ci].lang) != registry::canonical(&kernel.lang) {
|
||||
return Err(format!(
|
||||
"会话表: cell `{cell_name}` ({}) cannot use kernel `{}` ({})",
|
||||
"foreign.session: cell `{cell_name}` ({}) cannot use kernel `{}` ({})",
|
||||
cells[ci].lang, kernel.name, kernel.lang
|
||||
));
|
||||
}
|
||||
@@ -347,7 +377,7 @@ impl DocumentSession {
|
||||
cell.status = CellStatus::Unlisted;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if persistent {
|
||||
for cell in &mut cells {
|
||||
let kernel = ensure_default_kernel(
|
||||
&mut kernels,
|
||||
@@ -358,6 +388,12 @@ impl DocumentSession {
|
||||
cell.listed = true;
|
||||
order.push(cell.name.clone());
|
||||
}
|
||||
} else {
|
||||
for cell in &mut cells {
|
||||
cell.kernel = "one-shot".into();
|
||||
cell.listed = true;
|
||||
order.push(cell.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let mut host = HostContext::default();
|
||||
@@ -369,6 +405,8 @@ impl DocumentSession {
|
||||
|
||||
Ok(Self {
|
||||
path: path.to_path_buf(),
|
||||
root: root.to_path_buf(),
|
||||
persistent,
|
||||
cells,
|
||||
kernels,
|
||||
order,
|
||||
@@ -527,7 +565,34 @@ impl DocumentSession {
|
||||
.cell_index(cell_name)
|
||||
.ok_or_else(|| format!("unknown cell `{cell_name}`"))?;
|
||||
if !self.cells[ci].listed && kernel_override.is_none() {
|
||||
return Err(format!("cell `{cell_name}` is not in 会话表"));
|
||||
return Err(format!("cell `{cell_name}` is not listed in the foreign session"));
|
||||
}
|
||||
if !self.persistent {
|
||||
self.cells[ci].kernel = "one-shot".into();
|
||||
self.cells[ci].status = CellStatus::Running;
|
||||
let lang = self.cells[ci].lang.clone();
|
||||
let source = self.cells[ci].source.clone();
|
||||
let result = crate::host::foreign::invoke_for_view(
|
||||
&self.root,
|
||||
&lang,
|
||||
&source,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
return match result {
|
||||
Ok(record) => {
|
||||
self.record_result(cell_name, "one-shot", &record)?;
|
||||
Ok(record)
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(i) = self.cell_index(cell_name) {
|
||||
self.cells[i].status = CellStatus::Error;
|
||||
self.cells[i].last_error = e.clone();
|
||||
}
|
||||
Err(e)
|
||||
}
|
||||
};
|
||||
}
|
||||
let kernel = match kernel_override {
|
||||
Some(k) => {
|
||||
@@ -766,6 +831,7 @@ impl DocumentSession {
|
||||
Value::Map(vec![
|
||||
("title".into(), Value::Text(self.title())),
|
||||
("path".into(), Value::Text(self.path.display().to_string())),
|
||||
("persistent".into(), Value::Bool(self.persistent)),
|
||||
("cells".into(), Value::List(cells)),
|
||||
("kernels".into(), Value::List(kernels)),
|
||||
("order".into(), Value::List(
|
||||
@@ -794,6 +860,22 @@ impl DocumentSession {
|
||||
])
|
||||
}
|
||||
|
||||
/// Clear displayed outputs and per-cell records; kernels and live state survive.
|
||||
pub fn clear_outputs(&mut self) -> Value {
|
||||
for cell in &mut self.cells {
|
||||
cell.runs = 0;
|
||||
cell.last_ms = 0;
|
||||
cell.last_ok = false;
|
||||
cell.last_error.clear();
|
||||
if cell.listed {
|
||||
cell.status = CellStatus::Never;
|
||||
}
|
||||
}
|
||||
self.journal.clear();
|
||||
self.cursor = 0;
|
||||
self.summary()
|
||||
}
|
||||
|
||||
pub fn journal_json(&self) -> Value {
|
||||
Value::List(
|
||||
self.journal
|
||||
@@ -845,7 +927,7 @@ fn resolve_kernel(
|
||||
return Ok(decl);
|
||||
}
|
||||
Err(format!(
|
||||
"会话表: kernel `{name}` is not declared in 内核表"
|
||||
"foreign.session: kernel `{name}` is not declared in kernels"
|
||||
))
|
||||
}
|
||||
|
||||
@@ -904,22 +986,29 @@ mod tests {
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
let source = r#"# main
|
||||
let source = r#"---
|
||||
title: evaluated plan test
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`lang` = python
|
||||
|
||||
`内核表` =
|
||||
`分析内核` =
|
||||
|
||||
| @ | 名称 | 语言 |
|
||||
|---|------|------|
|
||||
| 1 | 分析 | `lang` |
|
||||
|
||||
`会话表` =
|
||||
`分析单元` =
|
||||
|
||||
| @ | 单元 | 内核 |
|
||||
|---|------|------|
|
||||
| 1 | 单元 | 分析 |
|
||||
|
||||
`会话` = > foreign.session kernels=`分析内核` cells=`分析单元`
|
||||
|
||||
`单元` =
|
||||
```python
|
||||
print(1)
|
||||
@@ -982,7 +1071,12 @@ print(1)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
let source = r#"# main
|
||||
let source = r#"---
|
||||
title: shared state test
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`内核表` =
|
||||
|
||||
@@ -999,6 +1093,8 @@ print(1)
|
||||
| 读取 | 分析 | read |
|
||||
| 隔离 | 绘图 | isolated |
|
||||
|
||||
`会话` = > foreign.session kernels=`内核表` cells=`会话表`
|
||||
|
||||
`设置` =
|
||||
```node
|
||||
let shared = 41;
|
||||
@@ -1039,6 +1135,119 @@ console.log(typeof shared === "undefined" ? "isolated" : "leaked");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn magic_table_names_without_session_are_ignored() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join(format!(
|
||||
"target/view-stateless-{}-{}.mq.md",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
let source = r#"# main
|
||||
|
||||
`内核表` =
|
||||
|
||||
| 名称 | 语言 |
|
||||
|------|------|
|
||||
| 分析 | node |
|
||||
|
||||
`会话表` =
|
||||
|
||||
| 单元 | 内核 |
|
||||
|------|------|
|
||||
| a | 分析 |
|
||||
|
||||
`a` =
|
||||
```node
|
||||
console.log("stateless");
|
||||
```
|
||||
"#;
|
||||
std::fs::write(&path, source).unwrap();
|
||||
let mut s = DocumentSession::open(&path, &root).unwrap();
|
||||
assert!(!s.persistent);
|
||||
assert!(s.kernels.is_empty(), "magic tables must not create kernels");
|
||||
assert_eq!(s.order, vec!["a".to_string()]);
|
||||
assert_eq!(s.cells[0].kernel, "one-shot");
|
||||
let r = s.run_cell("a", None).unwrap();
|
||||
assert_eq!(
|
||||
field(&r, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())),
|
||||
Some("stateless\n".to_string())
|
||||
);
|
||||
assert_eq!(s.cells[0].status, CellStatus::Clean);
|
||||
s.clear_outputs();
|
||||
assert_eq!(s.cells[0].status, CellStatus::Never);
|
||||
assert!(s.journal.is_empty());
|
||||
s.close();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_outputs_keeps_persistent_kernel_state() {
|
||||
if !node_available() {
|
||||
return;
|
||||
}
|
||||
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let path = root.join(format!(
|
||||
"target/view-clear-{}-{}.mq.md",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
let source = r#"---
|
||||
title: clear outputs
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`内核` =
|
||||
|
||||
| 名称 | 语言 |
|
||||
|------|------|
|
||||
| 分析 | node |
|
||||
|
||||
`单元` =
|
||||
|
||||
| 单元 | 内核 |
|
||||
|------|------|
|
||||
| a | 分析 |
|
||||
|
||||
`会话` = > foreign.session kernels=`内核` cells=`单元`
|
||||
|
||||
`a` =
|
||||
```node
|
||||
globalThis.kept = (globalThis.kept || 0) + 1;
|
||||
console.log(globalThis.kept);
|
||||
```
|
||||
"#;
|
||||
std::fs::write(&path, source).unwrap();
|
||||
let mut s = DocumentSession::open(&path, &root).unwrap();
|
||||
let r = s.run_cell("a", None).unwrap();
|
||||
assert_eq!(
|
||||
field(&r, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())),
|
||||
Some("1\n".to_string())
|
||||
);
|
||||
s.clear_outputs();
|
||||
assert_eq!(s.cells[0].status, CellStatus::Never);
|
||||
assert!(s.journal.is_empty());
|
||||
let r = s.run_cell("a", None).unwrap();
|
||||
assert_eq!(
|
||||
field(&r, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())),
|
||||
Some("2\n".to_string()),
|
||||
"clear outputs must not restart the kernel"
|
||||
);
|
||||
s.close();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_marks_changed_cell_dirty() {
|
||||
if !node_available() {
|
||||
@@ -15,6 +15,8 @@ pub mod event_bus;
|
||||
#[cfg(feature = "exec-host")]
|
||||
pub mod foreign;
|
||||
#[cfg(feature = "exec-host")]
|
||||
pub mod foreign_doc;
|
||||
#[cfg(feature = "exec-host")]
|
||||
pub mod foreign_session;
|
||||
mod fs;
|
||||
mod hash;
|
||||
|
||||
@@ -34,8 +34,6 @@ pub mod lex;
|
||||
pub mod linalg_ops;
|
||||
pub mod load;
|
||||
pub mod mlsp;
|
||||
#[cfg(feature = "exec-host")]
|
||||
pub mod notebook;
|
||||
pub mod parse;
|
||||
pub mod repair;
|
||||
pub mod value;
|
||||
|
||||
+36
-2
@@ -3466,6 +3466,7 @@ body.notebook-page .stdin-form button {
|
||||
<button type="button" id="nb-step">Step</button>
|
||||
<button type="button" id="nb-state">State</button>
|
||||
<button type="button" id="nb-journal">Journal</button>
|
||||
<button type="button" id="nb-clear">Clear outputs</button>
|
||||
<button type="button" id="nb-reload">Reload</button>
|
||||
<button type="button" id="nb-debug">Debug</button>
|
||||
<button type="button" id="nb-theme" title="切换浅色/深色主题">浅色</button>
|
||||
@@ -3501,7 +3502,7 @@ body.notebook-page .stdin-form button {
|
||||
if (!window.MARQDO_VIEW_LIVE) return;
|
||||
var path = window.MARQDO_FILE_PATH || "";
|
||||
if (!path) return;
|
||||
var state = { open: false, kernels: [], cells: {} };
|
||||
var state = { open: false, persistent: false, kernels: [], cells: {} };
|
||||
function post(url, body) {
|
||||
return fetch(url, {
|
||||
method: "POST",
|
||||
@@ -3552,6 +3553,7 @@ body.notebook-page .stdin-form button {
|
||||
}
|
||||
function renderSummary(s) {
|
||||
if (!s) return;
|
||||
state.persistent = !!s.persistent;
|
||||
state.kernels = s.kernels || [];
|
||||
state.cells = {};
|
||||
(s.cells || []).forEach(function (c) { state.cells[c.name] = c; });
|
||||
@@ -3559,6 +3561,11 @@ body.notebook-page .stdin-form button {
|
||||
var el = card(c.name);
|
||||
if (!el) return;
|
||||
badge(el, c.status, c.status === "clean");
|
||||
if (!state.persistent) {
|
||||
var old = el.querySelector(".nb-kernel");
|
||||
if (old) old.remove();
|
||||
return;
|
||||
}
|
||||
var sel = el.querySelector(".nb-kernel");
|
||||
if (!sel) {
|
||||
sel = document.createElement("select");
|
||||
@@ -3608,6 +3615,30 @@ body.notebook-page .stdin-form button {
|
||||
status("run all done");
|
||||
});
|
||||
}
|
||||
function clearDom() {
|
||||
document.querySelectorAll(".code-out").forEach(function (el) {
|
||||
el.hidden = true;
|
||||
el.textContent = "";
|
||||
el.classList.remove("err");
|
||||
});
|
||||
document.querySelectorAll(".nb-badge").forEach(function (el) {
|
||||
el.textContent = "never";
|
||||
el.className = "nb-badge";
|
||||
});
|
||||
var exec = document.getElementById("exec-main");
|
||||
if (exec) { exec.hidden = true; exec.textContent = ""; }
|
||||
var stateOut = document.getElementById("nb-state-out");
|
||||
if (stateOut) { stateOut.hidden = true; stateOut.textContent = ""; }
|
||||
var pullOut = document.getElementById("nb-pull-out");
|
||||
if (pullOut) pullOut.textContent = "";
|
||||
}
|
||||
function clearOutputs() {
|
||||
clearDom();
|
||||
post("api/session/clear", {}).then(function (r) {
|
||||
if (r.ok && r.session) renderSummary(r.session);
|
||||
status(r.ok ? "outputs cleared" : (r.error || "clear failed"));
|
||||
}).catch(function (e) { status(String(e)); });
|
||||
}
|
||||
function bind(id, fn) { var b = document.getElementById(id); if (b) b.addEventListener("click", fn); }
|
||||
function init() {
|
||||
var themeBtn = document.getElementById("nb-theme");
|
||||
@@ -3687,6 +3718,7 @@ body.notebook-page .stdin-form button {
|
||||
bind("nb-debug", function () {
|
||||
window.open("debug?path=" + encodeURIComponent(path), "_blank");
|
||||
});
|
||||
bind("nb-clear", clearOutputs);
|
||||
bind("nb-reload", function () {
|
||||
post("api/session/reload", {}).then(function (r) {
|
||||
if (!r.ok) { status(r.error || "reload failed"); return; }
|
||||
@@ -3879,7 +3911,7 @@ window.MARQDO_AUTO_STREAM = {auto_stream};
|
||||
{stdin_panel}
|
||||
{stream_panel}
|
||||
{plan_card}
|
||||
<div class="out {status}">{out}</div>
|
||||
<div class="out {status}" id="exec-main">{out}</div>
|
||||
{plots}
|
||||
</section>
|
||||
<section class="block source-block">
|
||||
@@ -4124,6 +4156,8 @@ mod tests {
|
||||
assert!(ui.contains("post(\"api/session/open\""));
|
||||
assert!(!ui.contains("In ["), "view cells must not mimic Jupyter In[] prompts");
|
||||
assert!(!ui.contains("Out ["), "view cells must not mimic Jupyter Out[] prompts");
|
||||
assert!(ui.contains("id=\"nb-clear\""), "view must expose Clear outputs");
|
||||
assert!(ui.contains("api/session/clear"), "Clear outputs must call the session API");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -189,6 +189,11 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
|
||||
notebook_api::api_close(&root.root, body)
|
||||
})
|
||||
}
|
||||
"/api/session/clear" => {
|
||||
return api_debug_post(request, |body| {
|
||||
notebook_api::api_clear(&root.root, body)
|
||||
})
|
||||
}
|
||||
"/api/session/cell/run" => {
|
||||
return api_debug_post(request, |body| {
|
||||
notebook_api::api_cell_run(&root.root, body)
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
use crate::host::json::{json_to_value, value_to_json};
|
||||
use crate::notebook::DocumentSession;
|
||||
use crate::host::foreign_doc::DocumentSession;
|
||||
use crate::value::Value;
|
||||
|
||||
fn sessions() -> &'static Mutex<HashMap<String, Arc<Mutex<DocumentSession>>>> {
|
||||
@@ -151,6 +151,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub fn api_clear(root: &Path, body: &str) -> serde_json::Value {
|
||||
with_session(root, body, "cleared", |s| Ok(s.clear_outputs()))
|
||||
}
|
||||
|
||||
pub fn api_cell_run(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let cell = v.get("cell").and_then(|x| x.as_str()).unwrap_or("");
|
||||
@@ -281,6 +285,10 @@ mod tests {
|
||||
Some(false),
|
||||
"{failed:?}"
|
||||
);
|
||||
let clear = api_clear(&root, r#"{"path":"shared.mq.md"}"#);
|
||||
assert_eq!(clear.get("ok").and_then(|v| v.as_bool()), Some(true), "{clear:?}");
|
||||
let cleared = clear.get("session").and_then(|s| s.get("cells")).and_then(|c| c.as_array()).cloned().unwrap_or_default();
|
||||
assert!(cleared.iter().all(|c| c.get("runs").and_then(|v| v.as_u64()) == Some(0)));
|
||||
let push = api_state_push(
|
||||
&root,
|
||||
r#"{"path":"shared.mq.md","kernel":"分析","name":"z","value":9}"#,
|
||||
|
||||
Reference in new issue
Block a user