Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: improve docs styling and parse rst docs (#744)

Myles Scolnick941ebe31194c
diff --git a/docs/helpers/md2rst.py b/docs/helpers/md2rst.py
index 0c19832e18..b08d9d03c2 100644
--- a/docs/helpers/md2rst.py
+++ b/docs/helpers/md2rst.py
@@ -8,6 +8,6 @@ import pypandoc
def md2rst(app, what, name, obj, options, lines):
del app, what, name, obj, options
md = "\n".join(lines)
- rst = pypandoc.convert_text(md, "rst", format="md")
+ rst = pypandoc.convert_text(source=md, format="md", to="rst")
lines.clear()
lines += rst.splitlines()
diff --git a/frontend/src/components/editor/chrome/panels/documentation-panel.css b/frontend/src/components/editor/chrome/panels/documentation-panel.css
deleted file mode 100644
index 61e0caa6a7..0000000000
--- a/frontend/src/components/editor/chrome/panels/documentation-panel.css
+++ /dev/null
@@ -1,6 +0,0 @@
-.docs-panel pre,
-.docs-panel code {
- white-space: break-spaces;
- font-size: 0.875rem;
- line-height: 150%;
-}
diff --git a/frontend/src/components/editor/chrome/panels/documentation-panel.tsx b/frontend/src/components/editor/chrome/panels/documentation-panel.tsx
index ba1e14d55d..44a25989a9 100644
--- a/frontend/src/components/editor/chrome/panels/documentation-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/documentation-panel.tsx
@@ -3,7 +3,7 @@ import { documentationAtom } from "@/core/documentation/state";
import { renderHTML } from "@/plugins/core/RenderHTML";
import { useAtomValue } from "jotai";
import React from "react";
-import "./documentation-panel.css";
+import "../../documentation.css";
import { BookMarkedIcon } from "lucide-react";
export const DocumentationPanel: React.FC = () => {
@@ -21,7 +21,7 @@ export const DocumentationPanel: React.FC = () => {
}
return (
- <div className="p-4 overflow-y-auto overflow-x-hidden h-full docs-panel flex flex-col gap-6">
+ <div className="p-3 overflow-y-auto overflow-x-hidden h-full docs-documentation flex flex-col gap-4">
{renderHTML({ html: documentation })}
</div>
);
diff --git a/frontend/src/components/editor/documentation.css b/frontend/src/components/editor/documentation.css
new file mode 100644
index 0000000000..63264e26e5
--- /dev/null
+++ b/frontend/src/components/editor/documentation.css
@@ -0,0 +1,133 @@
+.docs-documentation {
+ line-height: normal;
+
+ @apply font-prose;
+
+ ol {
+ list-style-type: decimal;
+ padding-left: 1.5rem;
+ }
+
+ ul {
+ list-style-type: disc;
+ padding-left: 1.5rem;
+ }
+
+ p {
+ padding: 0.4rem 0;
+ white-space: pre-wrap;
+ }
+
+ .section {
+ padding: 0.5rem 0;
+ }
+
+ dt {
+ padding-top: 0.6rem;
+ }
+
+ ul,
+ li,
+ ol,
+ .docutils p,
+ .docutils dd,
+ .paragraph {
+ white-space: initial;
+ }
+
+ table {
+ @apply text-sm border rounded my-2;
+
+ th,
+ td {
+ @apply border p-2;
+ }
+
+ th {
+ @apply bg-[var(--slate-2)];
+ }
+
+ tr:nth-child(2n) {
+ @apply bg-[var(--slate-1)];
+ }
+ }
+
+ code {
+ @apply border px-1 rounded font-mono bg-[var(--slate-1)] text-sm;
+ }
+
+ h1 {
+ font-weight: 600;
+ @apply text-xl border-b pb-1 mb-1 text-primary;
+ }
+
+ dt {
+ @apply font-bold;
+
+ span {
+ @apply font-normal;
+ }
+ }
+
+ .doctest-block,
+ .literal-block {
+ white-space: pre-wrap;
+ @apply bg-[var(--slate-2)] p-2 rounded text-sm;
+ }
+
+ .docutils.literal {
+ @apply bg-[var(--slate-2)] px-1 border rounded-md text-sm font-mono;
+ }
+
+ .codehilite code {
+ width: 100%;
+ display: inline-block;
+ white-space: pre-wrap;
+ @apply bg-[var(--slate-2)] border p-2 rounded-md font-mono text-sm;
+ }
+
+ blockquote {
+ white-space: pre-wrap;
+
+ @apply pl-2 ml-2;
+
+ p,
+ dl.docutils,
+ .docutils dt {
+ padding: 0;
+ white-space: normal;
+ font-weight: initial;
+ @apply text-sm font-mono;
+ }
+
+ .docutils dd {
+ white-space: pre-wrap;
+ padding-left: 1rem;
+ }
+ }
+}
+
+/* Custom overrides when in a tooltip */
+.mo-cm-tooltip.docs-documentation {
+ @apply text-sm;
+
+ h1 {
+ @apply text-base;
+ }
+
+ code,
+ .doctest-block,
+ .literal-block,
+ .docutils.literal,
+ .codehilite code {
+ @apply text-xs;
+ }
+
+ blockquote {
+ p,
+ dl.docutils,
+ .docutils dt {
+ @apply text-xs;
+ }
+ }
+}
diff --git a/frontend/src/core/codemirror/completion/Autocompleter.ts b/frontend/src/core/codemirror/completion/Autocompleter.ts
index c6afb0174b..25ed4c0425 100644
--- a/frontend/src/core/codemirror/completion/Autocompleter.ts
+++ b/frontend/src/core/codemirror/completion/Autocompleter.ts
@@ -9,6 +9,7 @@ import { sendCodeCompletionRequest } from "@/core/network/requests";
import { Tooltip } from "@codemirror/view";
import { DeferredRequestRegistry } from "@/core/network/DeferredRequestRegistry";
import { CodeCompletionRequest } from "@/core/network/types";
+import "../../../components/editor/documentation.css";
function constructCompletionInfoNode(innerHtml?: string): HTMLElement | null {
if (!innerHtml) {
@@ -16,9 +17,10 @@ function constructCompletionInfoNode(innerHtml?: string): HTMLElement | null {
}
const container = document.createElement("span");
container.classList.add("mo-cm-tooltip");
+ container.classList.add("docs-documentation");
container.style.display = "flex";
container.style.flexDirection = "column";
- container.style.gap = "1rem";
+ container.style.gap = ".8rem";
container.innerHTML = innerHtml;
return container;
}
diff --git a/frontend/src/css/codemirror.css b/frontend/src/css/codemirror.css
index 80cddfa10b..c63d1ab31e 100644
--- a/frontend/src/css/codemirror.css
+++ b/frontend/src/css/codemirror.css
@@ -26,7 +26,6 @@
/* -- Tooltips: code completion and type hints -- */
#App .cm-tooltip {
- font-size: 0.875rem;
border: none;
box-shadow: var(--light-shadow);
border-radius: 4px;
@@ -50,59 +49,11 @@
padding: 0.625rem 0.875rem;
}
-#App .cm-tooltip code {
- font-size: 0.8125rem;
-}
-
-#App .cm-tooltip .external-docs {
- font-size: 0.75rem;
-}
-
/* Hover tooltips get highest priority in display. */
#App .cm-tooltip.cm-tooltip-hover {
z-index: 1000;
}
-.mo-cm-tooltip ul {
- white-space: normal;
- display: block;
- list-style-type: disc;
- margin-block: 0 0;
- margin-inline: 0 0;
- padding-inline-start: 40px;
-}
-
-.mo-cm-tooltip ol {
- /* provide by the browser, but removed from 'reset' */
- white-space: normal;
- display: block;
- list-style-type: decimal;
- margin-block: 0 0;
- margin-inline: 0 0;
- padding-inline-start: 40px;
-}
-
-.mo-cm-tooltip li {
- /* provided by the browser, but removed from 'reset' */
- white-space: normal;
- display: list-item;
- text-align: -webkit-match-parent;
-}
-
-.mo-cm-tooltip blockquote {
- display: contents;
- margin-block: 0 0;
- margin-inline: 0 0;
-}
-
-.mo-cm-tooltip pre {
- margin: 0;
-
- /* We previously used pre-wrap, but this caused tooltips at the end
- * (right) of a cell to obscure code. */
- white-space: pre;
-}
-
/* -- Completion Info -- */
/* Compeletion Symbol */
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 1abc716bfe..c17af1ec8d 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -50,12 +50,12 @@ class table(
A table component with selectable rows. Get the selected rows with
`table.value`.
- The table data can be supplied a :
+ The table data can be supplied a:
- 1. a list of dicts, with one dict for each row, keyed by column names;
- 2. a list of values, representing a table with a single column;
- 3. a Pandas dataframe; or
- 4. a Polars dataframe.
+ 1. a list of dicts, with one dict for each row, keyed by column names;
+ 2. a list of values, representing a table with a single column;
+ 3. a Pandas dataframe; or
+ 4. a Polars dataframe.
**Examples.**
diff --git a/marimo/_runtime/complete.py b/marimo/_runtime/complete.py
index 5b79e7b039..d619be4a74 100644
--- a/marimo/_runtime/complete.py
+++ b/marimo/_runtime/complete.py
@@ -16,6 +16,7 @@ from marimo._output.md import _md
from marimo._runtime import dataflow
from marimo._runtime.requests import CompletionRequest
from marimo._utils.format_signature import format_signature
+from marimo._utils.rst_to_html import convert_rst_to_html
LOGGER = loggers.marimo_logger()
@@ -82,9 +83,21 @@ def _get_docstring(completion: jedi.api.classes.BaseName) -> str:
if completion.module_name.startswith("marimo"):
body = _md(body, apply_markdown_class=False).text
else:
- # TODO: this would look better parsed as RST
- # for non-marimo modules
- body = "<pre class='external-docs'>" + html.escape(body) + "</pre>"
+ try:
+ body = (
+ "<div class='external-docs'>"
+ + convert_rst_to_html(body)
+ + "</div>"
+ )
+ except Exception as e:
+ # if docutils chokes, we don't want to crash the completion
+ # worker
+ LOGGER.debug("Converting RST to HTML failed: ", e)
+ body = (
+ "<pre class='external-docs'>"
+ + html.escape(body)
+ + "</pre>"
+ )
if signature_text and body:
docstring = signature_text + "\n\n" + body
diff --git a/marimo/_utils/rst_to_html.py b/marimo/_utils/rst_to_html.py
new file mode 100644
index 0000000000..1dc4272db5
--- /dev/null
+++ b/marimo/_utils/rst_to_html.py
@@ -0,0 +1,22 @@
+# Copyright 2024 Marimo. All rights reserved.
+import contextlib
+import io
+
+from docutils.core import publish_parts # type: ignore[import-untyped]
+
+
+def convert_rst_to_html(rst_content: str) -> str:
+ """Convert RST content to HTML."""
+
+ # redirect stderr and ignore it to silence error messages
+ with contextlib.redirect_stderr(io.StringIO()) as _:
+ parts = publish_parts(
+ rst_content,
+ writer_name="html",
+ settings_overrides={
+ "warning_stream": None,
+ "file_insertion_enabled": False,
+ "report_level": 5,
+ },
+ )
+ return parts["html_body"] # type: ignore[no-any-return]
diff --git a/pyproject.toml b/pyproject.toml
index c934ccb25a..7b3deb2a4a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -35,6 +35,8 @@ dependencies = [
"websockets >= 10.0.0,<13.0.0",
# python <=3.10 compatibility
"typing_extensions>=4.4.0; python_version < \"3.10\"",
+ # for rst parsing
+ "docutils>=0.17.0",
# for cell formatting; if user version is not compatible, no-op
# so no lower bound needed
"black",
diff --git a/tests/_utils/test_rst_to_html.py b/tests/_utils/test_rst_to_html.py
new file mode 100644
index 0000000000..9b79162257
--- /dev/null
+++ b/tests/_utils/test_rst_to_html.py
@@ -0,0 +1,28 @@
+from marimo._utils.rst_to_html import convert_rst_to_html
+
+
+def test_basic_conversion() -> None:
+ rst_content = "**bold text**"
+ expected_html = (
+ '<div class="document">\n<p><strong>bold text</strong></p>\n</div>\n'
+ )
+ result = convert_rst_to_html(rst_content)
+ assert result.strip() == expected_html.strip()
+
+
+def test_empty_input() -> None:
+ rst_content = ""
+ expected_html = '<div class="document">\n</div>\n'
+ result = convert_rst_to_html(rst_content)
+ assert result.strip() == expected_html.strip()
+
+
+def test_code_block() -> None:
+ rst_content = "```python\nprint('Hello, world!')\n```"
+ expected_html = (
+ '<div class="document">\n<p><tt class="docutils literal">`python\n'
+ '<span class="pre">print(\'Hello,</span> <span class="pre">world!\')'
+ "</span>\n`</tt></p>\n</div>"
+ )
+ result = convert_rst_to_html(rst_content)
+ assert result.strip() == expected_html.strip()