Skip to content

Commit 9628d19

Browse files
authored
feat: add DoclingDocument viewer to CLI (#99)
feat: add docling document viewer to CLI Signed-off-by: Panos Vagenas <[email protected]>
1 parent b546c0a commit 9628d19

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

docling_core/cli/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""CLI package."""

docling_core/cli/view.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# Copyright IBM Corp. 2024 - 2024
3+
# SPDX-License-Identifier: MIT
4+
#
5+
6+
"""CLI for docling viewer."""
7+
import importlib
8+
import tempfile
9+
import webbrowser
10+
from pathlib import Path
11+
from typing import Annotated, Optional
12+
13+
import typer
14+
15+
from docling_core.types.doc import DoclingDocument
16+
from docling_core.types.doc.base import ImageRefMode
17+
from docling_core.utils.file import resolve_source_to_path
18+
19+
app = typer.Typer(
20+
name="Docling",
21+
no_args_is_help=True,
22+
add_completion=False,
23+
pretty_exceptions_enable=False,
24+
)
25+
26+
27+
def version_callback(value: bool):
28+
"""Callback for version inspection."""
29+
if value:
30+
docling_core_version = importlib.metadata.version("docling-core")
31+
print(f"Docling Core version: {docling_core_version}")
32+
raise typer.Exit()
33+
34+
35+
@app.command(no_args_is_help=True)
36+
def view(
37+
source: Annotated[
38+
str,
39+
typer.Argument(
40+
...,
41+
metavar="source",
42+
help="Docling JSON file to view.",
43+
),
44+
],
45+
version: Annotated[
46+
Optional[bool],
47+
typer.Option(
48+
"--version",
49+
callback=version_callback,
50+
is_eager=True,
51+
help="Show version information.",
52+
),
53+
] = None,
54+
):
55+
"""Display a Docling JSON file on the default browser."""
56+
path = resolve_source_to_path(source=source)
57+
doc = DoclingDocument.load_from_json(filename=path)
58+
target_path = Path(tempfile.mkdtemp()) / "out.html"
59+
html_output = doc.export_to_html(image_mode=ImageRefMode.EMBEDDED)
60+
with open(target_path, "w") as f:
61+
f.write(html_output)
62+
webbrowser.open(url=f"file://{target_path.absolute().resolve()}")
63+
64+
65+
click_app = typer.main.get_command(app)
66+
67+
if __name__ == "__main__":
68+
app()

poetry.lock

+83-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ packages = [{ include = "docling_core" }]
4444
validate = "docling_core.utils.validate:main"
4545
generate_jsonschema = "docling_core.utils.generate_jsonschema:main"
4646
generate_docs = "docling_core.utils.generate_docs:main"
47+
docling-view = "docling_core.cli.view:app"
4748

4849
[tool.poetry.dependencies]
4950
python = "^3.9"
@@ -57,6 +58,7 @@ pyyaml = ">=5.1,<7.0.0"
5758
typing-extensions = "^4.12.2"
5859
transformers = { version = "^4.34.0", optional = true }
5960
semchunk = { version = "^2.2.0", optional = true }
61+
typer = "^0.12.5"
6062

6163
[tool.poetry.extras]
6264
chunking = ["transformers", "semchunk"]

0 commit comments

Comments
 (0)