marimo-md-export 0.2.0__tar.gz → 0.2.1__tar.gz
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.
- marimo_md_export-0.2.1/PKG-INFO +95 -0
- marimo_md_export-0.2.1/README.md +69 -0
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/pyproject.toml +4 -1
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/cli.py +9 -8
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/export.py +53 -9
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/inject.py +22 -20
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/models.py +1 -1
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/parse_html.py +24 -17
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/parse_md.py +2 -2
- marimo_md_export-0.2.0/PKG-INFO +0 -60
- marimo_md_export-0.2.0/README.md +0 -37
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/LICENSE +0 -0
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/__init__.py +0 -0
- {marimo_md_export-0.2.0 → marimo_md_export-0.2.1}/src/marimo_md_export/__main__.py +0 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: marimo-md-export
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A minimal solution for exporting marimo notebooks to markdown *with* rendered HTML outputs, designed for mkdocs/zensical sites.
|
|
5
|
+
Keywords: marimo,markdown,documentation
|
|
6
|
+
Author: jmarshrossney
|
|
7
|
+
Author-email: jmarshrossney <17361029+jmarshrossney@users.noreply.github.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Documentation
|
|
16
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
17
|
+
Requires-Dist: beautifulsoup4>=4.14.3
|
|
18
|
+
Requires-Dist: lxml>=6.1.0
|
|
19
|
+
Requires-Dist: marimo>=0.23.3
|
|
20
|
+
Requires-Dist: typer>=0.25.0
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Project-URL: Homepage, https://jmarshrossney.github.io/marimo-md-export
|
|
23
|
+
Project-URL: Repository, https://github.com/jmarshrossney/marimo-md-export
|
|
24
|
+
Project-URL: Documentation, https://jmarshrossney.github.io/marimo-md-export
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# marimo-md-export
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/marimo-md-export/)
|
|
30
|
+
[](https://pypi.org/project/marimo-md-export/)
|
|
31
|
+
[](https://github.com/jmarshrossney/marimo-md-export/blob/main/LICENSE)
|
|
32
|
+
[](https://github.com/jmarshrossney/marimo-md-export/actions/workflows/ci.yml)
|
|
33
|
+
[](https://jmarshrossney.github.io/marimo-md-export)
|
|
34
|
+
|
|
35
|
+
A `uv` tool that wraps `marimo export`, extracts rendered outputs from the HTML export,
|
|
36
|
+
and injects them into the markdown export. The result is a self-contained markdown
|
|
37
|
+
document with embedded figures (as base64 `<img>` tags) and tables (as GFM or HTML).
|
|
38
|
+
|
|
39
|
+
**[Full documentation](https://jmarshrossney.github.io/marimo-md-export)**
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
Mark cells in your marimo `.py` notebook with `# @output: <label>`:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
# @output: my_figure
|
|
47
|
+
fig, ax = plt.subplots()
|
|
48
|
+
ax.plot(x, np.sin(x))
|
|
49
|
+
fig
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Then run:
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
uvx marimo-md-export notebook.py output.md
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
If you want the tool to be available globally you can "install" it with
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
uv tool install marimo-md-export
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Integrating with documentation sites
|
|
65
|
+
|
|
66
|
+
`marimo-md-export` is designed to produce markdown pages for static site generators like
|
|
67
|
+
[mkdocs](https://www.mkdocs.org/) (Python) or
|
|
68
|
+
[zensical](https://zensical.org/) (Rust). Both work identically for this purpose.
|
|
69
|
+
|
|
70
|
+
**1. Add as a docs dependency:**
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
uv add --group docs marimo-md-export
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**2. Add a build step** that converts your notebook(s) before building the site.
|
|
77
|
+
|
|
78
|
+
For example, this project uses the following [just](https://github.com/casey/just) command to build the docs:
|
|
79
|
+
|
|
80
|
+
```just
|
|
81
|
+
docs:
|
|
82
|
+
marimo-md-export examples/notebook.py docs/example.md \
|
|
83
|
+
--html-output docs/example-notebook.html
|
|
84
|
+
zensical build
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
This runs `marimo-md-export` to produce a self-contained markdown page (with any
|
|
88
|
+
`# @output:` cells injected as figures/tables), then builds the site.
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
uv sync # sync packages
|
|
94
|
+
just # run lint, typecheck, test, build docs
|
|
95
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# marimo-md-export
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/marimo-md-export/)
|
|
4
|
+
[](https://pypi.org/project/marimo-md-export/)
|
|
5
|
+
[](https://github.com/jmarshrossney/marimo-md-export/blob/main/LICENSE)
|
|
6
|
+
[](https://github.com/jmarshrossney/marimo-md-export/actions/workflows/ci.yml)
|
|
7
|
+
[](https://jmarshrossney.github.io/marimo-md-export)
|
|
8
|
+
|
|
9
|
+
A `uv` tool that wraps `marimo export`, extracts rendered outputs from the HTML export,
|
|
10
|
+
and injects them into the markdown export. The result is a self-contained markdown
|
|
11
|
+
document with embedded figures (as base64 `<img>` tags) and tables (as GFM or HTML).
|
|
12
|
+
|
|
13
|
+
**[Full documentation](https://jmarshrossney.github.io/marimo-md-export)**
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Mark cells in your marimo `.py` notebook with `# @output: <label>`:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
# @output: my_figure
|
|
21
|
+
fig, ax = plt.subplots()
|
|
22
|
+
ax.plot(x, np.sin(x))
|
|
23
|
+
fig
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Then run:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
uvx marimo-md-export notebook.py output.md
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
If you want the tool to be available globally you can "install" it with
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
uv tool install marimo-md-export
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Integrating with documentation sites
|
|
39
|
+
|
|
40
|
+
`marimo-md-export` is designed to produce markdown pages for static site generators like
|
|
41
|
+
[mkdocs](https://www.mkdocs.org/) (Python) or
|
|
42
|
+
[zensical](https://zensical.org/) (Rust). Both work identically for this purpose.
|
|
43
|
+
|
|
44
|
+
**1. Add as a docs dependency:**
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
uv add --group docs marimo-md-export
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**2. Add a build step** that converts your notebook(s) before building the site.
|
|
51
|
+
|
|
52
|
+
For example, this project uses the following [just](https://github.com/casey/just) command to build the docs:
|
|
53
|
+
|
|
54
|
+
```just
|
|
55
|
+
docs:
|
|
56
|
+
marimo-md-export examples/notebook.py docs/example.md \
|
|
57
|
+
--html-output docs/example-notebook.html
|
|
58
|
+
zensical build
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
This runs `marimo-md-export` to produce a self-contained markdown page (with any
|
|
62
|
+
`# @output:` cells injected as figures/tables), then builds the site.
|
|
63
|
+
|
|
64
|
+
## Development
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
uv sync # sync packages
|
|
68
|
+
just # run lint, typecheck, test, build docs
|
|
69
|
+
```
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "marimo-md-export"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.1"
|
|
8
8
|
description = "A minimal solution for exporting marimo notebooks to markdown *with* rendered HTML outputs, designed for mkdocs/zensical sites."
|
|
9
9
|
authors = [
|
|
10
10
|
{ name = "jmarshrossney", email = "17361029+jmarshrossney@users.noreply.github.com" }
|
|
@@ -23,6 +23,9 @@ keywords = ["marimo", "markdown", "documentation"]
|
|
|
23
23
|
classifiers = [
|
|
24
24
|
"Development Status :: 3 - Alpha",
|
|
25
25
|
"Intended Audience :: Developers",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
"Programming Language :: Python :: 3.14",
|
|
26
29
|
"Topic :: Documentation",
|
|
27
30
|
"Topic :: Text Processing :: Markup :: Markdown",
|
|
28
31
|
]
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
2
|
|
|
3
3
|
import typer
|
|
4
|
+
from rich.console import Console
|
|
4
5
|
|
|
5
6
|
from .export import export_html, export_md, strip_header_from_frontmatter
|
|
6
7
|
from .inject import inject_outputs
|
|
7
8
|
from .parse_html import extract_outputs
|
|
8
9
|
from .parse_md import collect_marked_cells
|
|
9
10
|
|
|
11
|
+
_err_console = Console(stderr=True)
|
|
12
|
+
|
|
10
13
|
app = typer.Typer(
|
|
11
14
|
help="Export a marimo (.py) notebook to markdown (.md) with rendered outputs embedded inline.",
|
|
12
15
|
context_settings={"help_option_names": ["-h", "--help"]},
|
|
@@ -62,7 +65,7 @@ def main(
|
|
|
62
65
|
in an isolated uv environment.
|
|
63
66
|
"""
|
|
64
67
|
extra = marimo_args.split() if marimo_args.strip() else []
|
|
65
|
-
timeout_val = timeout or None
|
|
68
|
+
timeout_val: int | None = timeout or None
|
|
66
69
|
|
|
67
70
|
if verbose:
|
|
68
71
|
typer.echo(f"Exporting markdown: {notebook}")
|
|
@@ -74,10 +77,10 @@ def main(
|
|
|
74
77
|
|
|
75
78
|
marked = collect_marked_cells(md)
|
|
76
79
|
if not marked:
|
|
77
|
-
|
|
80
|
+
_err_console.print(
|
|
78
81
|
f"WARNING: no @output markers found in {notebook}. "
|
|
79
82
|
"Did you forget to add '# @output: <label>' comments?",
|
|
80
|
-
|
|
83
|
+
style="bold yellow",
|
|
81
84
|
)
|
|
82
85
|
raise typer.Exit(2)
|
|
83
86
|
|
|
@@ -103,12 +106,10 @@ def main(
|
|
|
103
106
|
target_hashes = {c.source_hash for c in marked}
|
|
104
107
|
outputs = extract_outputs(html, target_hashes)
|
|
105
108
|
|
|
106
|
-
|
|
107
|
-
for cell in marked:
|
|
108
|
-
if cell.source_hash in outputs:
|
|
109
|
-
outputs[cell.source_hash].label = cell.label
|
|
109
|
+
result, warnings = inject_outputs(md, marked, outputs)
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
for warning in warnings:
|
|
112
|
+
_err_console.print(f"WARNING: {warning}", style="bold yellow")
|
|
112
113
|
|
|
113
114
|
result = strip_header_from_frontmatter(result)
|
|
114
115
|
|
|
@@ -1,16 +1,50 @@
|
|
|
1
1
|
import os
|
|
2
|
-
import re
|
|
3
2
|
import subprocess
|
|
4
3
|
import sys
|
|
5
4
|
import tempfile
|
|
6
5
|
import threading
|
|
7
6
|
from pathlib import Path
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
|
|
9
|
+
def _strip_header_key(frontmatter: str) -> str | None:
|
|
10
|
+
"""Remove the 'header:' key (with its multiline value) from a YAML frontmatter block.
|
|
11
|
+
|
|
12
|
+
Returns None if the header key was not found. Handles YAML block scalars
|
|
13
|
+
(|-, |, >-, >) and plain multiline values (indented continuation lines).
|
|
14
|
+
"""
|
|
15
|
+
lines = frontmatter.split("\n")
|
|
16
|
+
result_lines: list[str] = []
|
|
17
|
+
i = 0
|
|
18
|
+
found = False
|
|
19
|
+
while i < len(lines):
|
|
20
|
+
line = lines[i]
|
|
21
|
+
stripped = line.lstrip()
|
|
22
|
+
if not stripped.startswith("header:"):
|
|
23
|
+
result_lines.append(line)
|
|
24
|
+
i += 1
|
|
25
|
+
continue
|
|
26
|
+
|
|
27
|
+
found = True
|
|
28
|
+
value_part = line[len("header:") :].strip()
|
|
29
|
+
# Detect block scalar indicators: |, |-, >, >-
|
|
30
|
+
is_block = value_part in ("|", "|-", ">", ">-")
|
|
31
|
+
if is_block:
|
|
32
|
+
# Skip indented continuation lines
|
|
33
|
+
i += 1
|
|
34
|
+
while i < len(lines) and lines[i].startswith((" ", "\t")):
|
|
35
|
+
i += 1
|
|
36
|
+
else:
|
|
37
|
+
# Plain value (possibly none after the colon)
|
|
38
|
+
i += 1
|
|
39
|
+
|
|
40
|
+
if not found:
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
return "\n".join(result_lines)
|
|
10
44
|
|
|
11
45
|
|
|
12
46
|
def _run_with_visible_output(
|
|
13
|
-
cmd: list[str], *, env: dict[str, str], timeout:
|
|
47
|
+
cmd: list[str], *, env: dict[str, str], timeout: int | None
|
|
14
48
|
) -> subprocess.CompletedProcess[str]:
|
|
15
49
|
"""Run *cmd* with stdout inherited (visible) and stderr captured."""
|
|
16
50
|
stderr_lines: list[str] = []
|
|
@@ -42,7 +76,7 @@ def export_html(
|
|
|
42
76
|
notebook: Path,
|
|
43
77
|
extra_args: list[str] | None = None,
|
|
44
78
|
sandbox: bool = False,
|
|
45
|
-
timeout:
|
|
79
|
+
timeout: int | None = 120,
|
|
46
80
|
) -> bytes:
|
|
47
81
|
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as f:
|
|
48
82
|
tmp = Path(f.name)
|
|
@@ -82,7 +116,7 @@ def export_html(
|
|
|
82
116
|
def export_md(
|
|
83
117
|
notebook: Path,
|
|
84
118
|
sandbox: bool = False,
|
|
85
|
-
timeout:
|
|
119
|
+
timeout: int | None = 120,
|
|
86
120
|
) -> str:
|
|
87
121
|
with tempfile.NamedTemporaryFile(suffix=".md", delete=False) as f:
|
|
88
122
|
tmp = Path(f.name)
|
|
@@ -108,7 +142,14 @@ def export_md(
|
|
|
108
142
|
|
|
109
143
|
|
|
110
144
|
def strip_header_from_frontmatter(md: str) -> str:
|
|
111
|
-
"""Remove the 'header:' key from YAML frontmatter if present.
|
|
145
|
+
"""Remove the 'header:' key from YAML frontmatter if present.
|
|
146
|
+
|
|
147
|
+
Parses the frontmatter line-by-line, removes the header key (including
|
|
148
|
+
any multiline block-scalar or indented continuation lines), then
|
|
149
|
+
reconstructs the frontmatter. If only a title key remains, it is
|
|
150
|
+
preserved cleanly. If the frontmatter is empty after removal, it is
|
|
151
|
+
dropped entirely.
|
|
152
|
+
"""
|
|
112
153
|
if not md.startswith("---"):
|
|
113
154
|
return md
|
|
114
155
|
|
|
@@ -123,11 +164,14 @@ def strip_header_from_frontmatter(md: str) -> str:
|
|
|
123
164
|
frontmatter = md[:after_close]
|
|
124
165
|
rest = md[after_close:]
|
|
125
166
|
|
|
126
|
-
stripped =
|
|
127
|
-
if stripped
|
|
167
|
+
stripped = _strip_header_key(frontmatter)
|
|
168
|
+
if stripped is None:
|
|
128
169
|
return md
|
|
129
170
|
|
|
130
|
-
|
|
171
|
+
inner_start = len("---\n")
|
|
172
|
+
inner_end = stripped.rfind("\n---")
|
|
173
|
+
inner = stripped[inner_start:inner_end]
|
|
174
|
+
|
|
131
175
|
if not inner.strip():
|
|
132
176
|
return rest.lstrip("\n")
|
|
133
177
|
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
import warnings
|
|
3
|
-
|
|
4
1
|
from bs4 import BeautifulSoup
|
|
5
2
|
|
|
6
3
|
from .models import ExtractedOutput, MarkedCell
|
|
7
|
-
from .parse_md import collect_marked_cells
|
|
8
4
|
|
|
9
5
|
|
|
10
6
|
def _table_to_gfm(html: str) -> str | None:
|
|
@@ -28,10 +24,17 @@ def _table_to_gfm(html: str) -> str | None:
|
|
|
28
24
|
|
|
29
25
|
header = rows[0]
|
|
30
26
|
body = rows[1:]
|
|
31
|
-
|
|
27
|
+
|
|
28
|
+
# Reject tables with inconsistent row widths — fall back to HTML
|
|
29
|
+
ncols = len(header)
|
|
30
|
+
if any(len(row) != ncols for row in rows):
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
sep = ["---"] * ncols
|
|
32
34
|
|
|
33
35
|
def _row(cells: list[str]) -> str:
|
|
34
|
-
|
|
36
|
+
escaped = [c.replace("|", "\\|") for c in cells]
|
|
37
|
+
return "| " + " | ".join(escaped) + " |"
|
|
35
38
|
|
|
36
39
|
lines = [_row(header), _row(sep), *(_row(r) for r in body)]
|
|
37
40
|
return "\n".join(lines)
|
|
@@ -55,27 +58,26 @@ def _format_output(output: ExtractedOutput) -> str:
|
|
|
55
58
|
|
|
56
59
|
def inject_outputs(
|
|
57
60
|
md: str,
|
|
61
|
+
marked_cells: list[MarkedCell],
|
|
58
62
|
outputs: dict[str, ExtractedOutput],
|
|
59
|
-
) -> str:
|
|
63
|
+
) -> tuple[str, list[str]]:
|
|
60
64
|
"""Inject rendered outputs into the markdown after each marked code block.
|
|
61
65
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
marked_cells: list[MarkedCell] = collect_marked_cells(md)
|
|
66
|
+
marked_cells should come from collect_marked_cells(md). outputs maps
|
|
67
|
+
source_hash → ExtractedOutput. Labels are set from the corresponding
|
|
68
|
+
MarkedCell during injection.
|
|
66
69
|
|
|
70
|
+
Returns (result_markdown, warnings) where warnings is a list of
|
|
71
|
+
human-readable messages for outputs that had no matching rendered data.
|
|
72
|
+
"""
|
|
67
73
|
result = md
|
|
74
|
+
warnings: list[str] = []
|
|
68
75
|
for cell in marked_cells:
|
|
69
76
|
matched = outputs.get(cell.source_hash)
|
|
70
77
|
if matched is None:
|
|
71
|
-
warnings.
|
|
72
|
-
f"
|
|
73
|
-
f"(hash {cell.source_hash[:8]})"
|
|
74
|
-
stacklevel=2,
|
|
75
|
-
)
|
|
76
|
-
print(
|
|
77
|
-
f"WARNING: no output found for @output:{cell.label}",
|
|
78
|
-
file=sys.stderr,
|
|
78
|
+
warnings.append(
|
|
79
|
+
f"no output found for @output:{cell.label} "
|
|
80
|
+
f"(hash {cell.source_hash[:8]})"
|
|
79
81
|
)
|
|
80
82
|
continue
|
|
81
83
|
|
|
@@ -85,4 +87,4 @@ def inject_outputs(
|
|
|
85
87
|
cell.block_text, cell.block_text + "\n\n" + formatted, 1
|
|
86
88
|
)
|
|
87
89
|
|
|
88
|
-
return result
|
|
90
|
+
return result, warnings
|
|
@@ -16,7 +16,7 @@ class MarkedCell:
|
|
|
16
16
|
class ExtractedOutput:
|
|
17
17
|
"""The rendered output scraped from the HTML export for one cell."""
|
|
18
18
|
|
|
19
|
-
label: str
|
|
20
19
|
raw_html: str
|
|
21
20
|
output_type: str # "figure" | "table" | "text" | "unknown"
|
|
21
|
+
label: str = ""
|
|
22
22
|
console_html: str = field(default="")
|
|
@@ -28,27 +28,30 @@ from bs4 import BeautifulSoup
|
|
|
28
28
|
from .models import ExtractedOutput
|
|
29
29
|
|
|
30
30
|
_SESSION_CELLS_RE = re.compile(
|
|
31
|
-
r'"session"\s*:\s*\{"cells"\s*:\s
|
|
31
|
+
r'"session"\s*:\s*\{"cells"\s*:\s*\[',
|
|
32
32
|
)
|
|
33
33
|
|
|
34
|
+
_decoder = json.JSONDecoder()
|
|
35
|
+
|
|
34
36
|
|
|
35
37
|
def _extract_session_cells_raw(html: bytes) -> str:
|
|
36
|
-
"""Return the raw JSON string for session.cells from the HTML.
|
|
38
|
+
"""Return the raw JSON string for session.cells from the HTML.
|
|
39
|
+
|
|
40
|
+
Uses json.JSONDecoder.raw_decode to correctly handle brackets inside
|
|
41
|
+
JSON string values.
|
|
42
|
+
"""
|
|
37
43
|
text = html.decode("utf-8", errors="replace")
|
|
38
44
|
m = _SESSION_CELLS_RE.search(text)
|
|
39
45
|
if m is None:
|
|
40
46
|
return "[]"
|
|
41
47
|
|
|
42
|
-
start = m.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if depth == 0:
|
|
50
|
-
return text[start : i + 1]
|
|
51
|
-
return "[]"
|
|
48
|
+
start = m.end() - 1 # position of the opening '['
|
|
49
|
+
try:
|
|
50
|
+
_, end = _decoder.raw_decode(text, idx=start)
|
|
51
|
+
except json.JSONDecodeError:
|
|
52
|
+
return "[]"
|
|
53
|
+
|
|
54
|
+
return text[start:end]
|
|
52
55
|
|
|
53
56
|
|
|
54
57
|
def _table_html_from_marimo_table(marimo_table_html: str) -> str:
|
|
@@ -71,10 +74,12 @@ def _table_html_from_marimo_table(marimo_table_html: str) -> str:
|
|
|
71
74
|
field_types_raw = "[]"
|
|
72
75
|
|
|
73
76
|
# data-data is a JSON-quoted string containing an array of row objects.
|
|
74
|
-
# It may contain literal NaN (not valid JSON).
|
|
77
|
+
# It may contain literal NaN (not valid JSON). Replace only bare NaN
|
|
78
|
+
# tokens (not inside quoted string values) to avoid clobbering strings
|
|
79
|
+
# like "NaN handling".
|
|
75
80
|
try:
|
|
76
81
|
inner = json.loads(data_data) # unwrap outer JSON string
|
|
77
|
-
cleaned =
|
|
82
|
+
cleaned = re.sub(r'(?<!")\bNaN\b(?!")', "null", inner)
|
|
78
83
|
rows = json.loads(cleaned)
|
|
79
84
|
except (json.JSONDecodeError, AttributeError):
|
|
80
85
|
return marimo_table_html
|
|
@@ -136,6 +141,10 @@ def extract_outputs(html: bytes, target_hashes: set[str]) -> dict[str, Extracted
|
|
|
136
141
|
|
|
137
142
|
Returns a dict mapping source_hash → ExtractedOutput for each cell whose
|
|
138
143
|
code_hash is in target_hashes and that has a renderable output.
|
|
144
|
+
|
|
145
|
+
If a cell has multiple outputs, only the first renderable one is used
|
|
146
|
+
(in MIME-type priority order: marimo mimebundle → text/html → text/plain).
|
|
147
|
+
Console output (stdout) is always captured independently.
|
|
139
148
|
"""
|
|
140
149
|
cells_raw = _extract_session_cells_raw(html)
|
|
141
150
|
try:
|
|
@@ -165,14 +174,12 @@ def extract_outputs(html: bytes, target_hashes: set[str]) -> dict[str, Extracted
|
|
|
165
174
|
if classified is None:
|
|
166
175
|
continue
|
|
167
176
|
output_type, raw_html = classified
|
|
168
|
-
break #
|
|
177
|
+
break # first renderable output per cell wins
|
|
169
178
|
|
|
170
179
|
if not console_html and not raw_html:
|
|
171
180
|
continue
|
|
172
181
|
|
|
173
|
-
# label is filled in by inject.py after matching; use hash as placeholder
|
|
174
182
|
results[code_hash] = ExtractedOutput(
|
|
175
|
-
label=code_hash,
|
|
176
183
|
raw_html=raw_html,
|
|
177
184
|
output_type=output_type,
|
|
178
185
|
console_html=console_html,
|
|
@@ -14,7 +14,7 @@ def _md5(text: str) -> str:
|
|
|
14
14
|
|
|
15
15
|
def collect_marked_cells(md: str) -> list[MarkedCell]:
|
|
16
16
|
"""Return one MarkedCell for each fenced block that contains an @output marker."""
|
|
17
|
-
seen_labels:
|
|
17
|
+
seen_labels: set[str] = set()
|
|
18
18
|
results: list[MarkedCell] = []
|
|
19
19
|
|
|
20
20
|
for block_match in _BLOCK_RE.finditer(md):
|
|
@@ -30,7 +30,7 @@ def collect_marked_cells(md: str) -> list[MarkedCell]:
|
|
|
30
30
|
raise ValueError(
|
|
31
31
|
f"Duplicate @output label {label!r} — each label must be unique"
|
|
32
32
|
)
|
|
33
|
-
seen_labels
|
|
33
|
+
seen_labels.add(label)
|
|
34
34
|
|
|
35
35
|
results.append(
|
|
36
36
|
MarkedCell(
|
marimo_md_export-0.2.0/PKG-INFO
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: marimo-md-export
|
|
3
|
-
Version: 0.2.0
|
|
4
|
-
Summary: A minimal solution for exporting marimo notebooks to markdown *with* rendered HTML outputs, designed for mkdocs/zensical sites.
|
|
5
|
-
Keywords: marimo,markdown,documentation
|
|
6
|
-
Author: jmarshrossney
|
|
7
|
-
Author-email: jmarshrossney <17361029+jmarshrossney@users.noreply.github.com>
|
|
8
|
-
License-Expression: MIT
|
|
9
|
-
License-File: LICENSE
|
|
10
|
-
Classifier: Development Status :: 3 - Alpha
|
|
11
|
-
Classifier: Intended Audience :: Developers
|
|
12
|
-
Classifier: Topic :: Documentation
|
|
13
|
-
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
14
|
-
Requires-Dist: beautifulsoup4>=4.14.3
|
|
15
|
-
Requires-Dist: lxml>=6.1.0
|
|
16
|
-
Requires-Dist: marimo>=0.23.3
|
|
17
|
-
Requires-Dist: typer>=0.25.0
|
|
18
|
-
Requires-Python: >=3.12
|
|
19
|
-
Project-URL: Homepage, https://jmarshrossney.github.io/marimo-md-export
|
|
20
|
-
Project-URL: Repository, https://github.com/jmarshrossney/marimo-md-export
|
|
21
|
-
Project-URL: Documentation, https://jmarshrossney.github.io/marimo-md-export
|
|
22
|
-
Description-Content-Type: text/markdown
|
|
23
|
-
|
|
24
|
-
# marimo-md-export
|
|
25
|
-
|
|
26
|
-
A `uv` tool that wraps `marimo export`, extracts rendered outputs from the HTML export,
|
|
27
|
-
and injects them into the markdown export. The result is a self-contained markdown
|
|
28
|
-
document with embedded figures (as base64 `<img>` tags) and tables (as GFM or HTML).
|
|
29
|
-
|
|
30
|
-
**[Full documentation](https://jmarshrossney.github.io/marimo-md-export)**
|
|
31
|
-
|
|
32
|
-
## Quick start
|
|
33
|
-
|
|
34
|
-
Mark cells in your marimo `.py` notebook with `# @output: <label>`:
|
|
35
|
-
|
|
36
|
-
```python
|
|
37
|
-
# @output: my_figure
|
|
38
|
-
fig, ax = plt.subplots()
|
|
39
|
-
ax.plot(x, np.sin(x))
|
|
40
|
-
fig
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Then run:
|
|
44
|
-
|
|
45
|
-
```sh
|
|
46
|
-
uvx marimo-md-export notebook.py output.md
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Installation
|
|
50
|
-
|
|
51
|
-
```sh
|
|
52
|
-
uv tool install marimo-md-export # or: pipx install marimo-md-export
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Development
|
|
56
|
-
|
|
57
|
-
```sh
|
|
58
|
-
uv sync # sync packages
|
|
59
|
-
just # run lint, typecheck, test, build docs
|
|
60
|
-
```
|
marimo_md_export-0.2.0/README.md
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# marimo-md-export
|
|
2
|
-
|
|
3
|
-
A `uv` tool that wraps `marimo export`, extracts rendered outputs from the HTML export,
|
|
4
|
-
and injects them into the markdown export. The result is a self-contained markdown
|
|
5
|
-
document with embedded figures (as base64 `<img>` tags) and tables (as GFM or HTML).
|
|
6
|
-
|
|
7
|
-
**[Full documentation](https://jmarshrossney.github.io/marimo-md-export)**
|
|
8
|
-
|
|
9
|
-
## Quick start
|
|
10
|
-
|
|
11
|
-
Mark cells in your marimo `.py` notebook with `# @output: <label>`:
|
|
12
|
-
|
|
13
|
-
```python
|
|
14
|
-
# @output: my_figure
|
|
15
|
-
fig, ax = plt.subplots()
|
|
16
|
-
ax.plot(x, np.sin(x))
|
|
17
|
-
fig
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
Then run:
|
|
21
|
-
|
|
22
|
-
```sh
|
|
23
|
-
uvx marimo-md-export notebook.py output.md
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Installation
|
|
27
|
-
|
|
28
|
-
```sh
|
|
29
|
-
uv tool install marimo-md-export # or: pipx install marimo-md-export
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## Development
|
|
33
|
-
|
|
34
|
-
```sh
|
|
35
|
-
uv sync # sync packages
|
|
36
|
-
just # run lint, typecheck, test, build docs
|
|
37
|
-
```
|
|
File without changes
|
|
File without changes
|
|
File without changes
|