From 1ed57d2f295e3ffd600512f595bfe8e25c5e37c6 Mon Sep 17 00:00:00 2001 From: Mecoli1219 Date: Thu, 14 Nov 2024 22:49:11 -0800 Subject: [PATCH] fix error & debug Signed-off-by: Mecoli1219 --- ...book_rli.py => notebook_literalinclude.py} | 71 ++++++++++--------- docs/conf.py | 2 +- .../jupyter_notebook_interactive_mode.md | 17 +++-- 3 files changed, 49 insertions(+), 41 deletions(-) rename docs/_ext/{notebook_rli.py => notebook_literalinclude.py} (67%) diff --git a/docs/_ext/notebook_rli.py b/docs/_ext/notebook_literalinclude.py similarity index 67% rename from docs/_ext/notebook_rli.py rename to docs/_ext/notebook_literalinclude.py index f627d5a96b..a44320f651 100644 --- a/docs/_ext/notebook_rli.py +++ b/docs/_ext/notebook_literalinclude.py @@ -1,9 +1,9 @@ -import requests +import json from docutils import nodes from docutils.parsers.rst import directives from six import text_type -from typing import Any, Dict, List, Tuple +from typing import Any, Dict, List, Tuple, TYPE_CHECKING from sphinx.application import Sphinx from sphinx.config import Config @@ -12,45 +12,44 @@ from sphinx.util import parselinenos from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import set_source_info +from sphinx.util._pathlib import _StrPath +if TYPE_CHECKING: + import os -class NotebookRemoteLiteralIncludeReader(object): - def __init__(self, url: str, options: Dict[str, Any], config: Config) -> None: - self.url = url +class NotebookLiteralIncludeReader(object): + + def __init__( + self, filename: str | os.PathLike[str], options: Dict[str, Any], config: Config + ) -> None: + self.filename = _StrPath(filename) self.options = options - def read_file(self, url: str) -> List[str]: - response = requests.get(url) - json_data = response.json() + def read_file(self, filename: str | os.PathLike[str]) -> List[str]: + filename = _StrPath(filename) + json_data = json.loads(filename.read_text()) - if json_data: - if not response.status_code == requests.codes.ok: - raise ValueError( - "HTTP request returned error code %s" % response.status_code - ) - cell_idx = int(self.options["cell"]) - if "cell" in self.options: - if len(json_data["cells"]) > cell_idx: - if json_data["cells"][cell_idx]["cell_type"] == "code": - lines = json_data["cells"][cell_idx]["source"] - text = "".join(lines) - else: - raise ValueError("Cell is not a code cell") + cell_idx = int(self.options["cell"]) + if "cell" in self.options: + if len(json_data["cells"]) > cell_idx: + if json_data["cells"][cell_idx]["cell_type"] == "code": + lines = json_data["cells"][cell_idx]["source"] + text = "".join(lines) else: - raise ValueError("Cell exceeds the number of cells in the notebook") + raise ValueError("Cell is not a code cell") else: - raise ValueError("Cell not specified in options") + raise ValueError("Cell exceeds the number of cells in the notebook") + else: + raise ValueError("Cell not specified in options") - if "tab-width" in self.options: - text = text.expandtabs(self.options["tab-width"]) + if "tab-width" in self.options: + text = text.expandtabs(self.options["tab-width"]) - return text.splitlines(True) - else: - raise IOError(__("Include file %r not found or reading it failed") % url) + return text.splitlines(True) def read(self) -> Tuple[str, int]: - lines = self.read_file(self.url) + lines = self.read_file(self.filename) lines = self.lines_filter(lines) return "".join(lines), len(lines) @@ -68,13 +67,13 @@ def lines_filter(self, lines: List[str]) -> List[str]: if lines == []: raise ValueError( __("Line spec %r: no lines pulled from include file %r") - % (linespec, self.url) + % (linespec, self.filename) ) return lines -class NotebookRemoteLiteralInclude(SphinxDirective): +class NotebookLiteralInclude(SphinxDirective): """ Like ``.. include:: :literal:``, but only warns if the include file is not found, and does not raise errors. Also has several options for @@ -103,12 +102,14 @@ def run(self) -> List[nodes.Node]: document.reporter.warning("File insertion disabled", line=self.lineno) ] try: - url = self.arguments[0] + location = self.state_machine.get_source_and_line(self.lineno) + rel_filename, filename = self.env.relfn2path(self.arguments[0]) + self.env.note_dependency(rel_filename) - reader = NotebookRemoteLiteralIncludeReader(url, self.options, self.config) + reader = NotebookLiteralIncludeReader(filename, self.options, self.config) text, lines = reader.read() - retnode = nodes.literal_block(text, text, source=url) + retnode = nodes.literal_block(text, text, source=filename) set_source_info(self, retnode) if "language" in self.options: retnode["language"] = self.options["language"] @@ -137,7 +138,7 @@ def run(self) -> List[nodes.Node]: def setup(app: Sphinx) -> dict: - app.add_directive("nb-rli", NotebookRemoteLiteralInclude) + app.add_directive("nb-literalinclude", NotebookLiteralInclude) return { "parallel_read_safe": True, diff --git a/docs/conf.py b/docs/conf.py index 50dec8f375..c936f1d7bd 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -79,7 +79,7 @@ # custom extensions "auto_examples", "import_projects", - "notebook_rli", + "notebook_literalinclude", ] source_suffix = { diff --git a/docs/user_guide/flyte_fundamentals/jupyter_notebook_interactive_mode.md b/docs/user_guide/flyte_fundamentals/jupyter_notebook_interactive_mode.md index 7ad2b0dad0..8080413160 100644 --- a/docs/user_guide/flyte_fundamentals/jupyter_notebook_interactive_mode.md +++ b/docs/user_guide/flyte_fundamentals/jupyter_notebook_interactive_mode.md @@ -25,15 +25,22 @@ this is not work? :lines: 1 ``` - +```