Skip to content

Commit

Permalink
fix error & debug
Browse files Browse the repository at this point in the history
Signed-off-by: Mecoli1219 <[email protected]>
  • Loading branch information
Mecoli1219 committed Nov 15, 2024
1 parent 41d0a64 commit 1ed57d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
71 changes: 36 additions & 35 deletions docs/_ext/notebook_rli.py → docs/_ext/notebook_literalinclude.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
# custom extensions
"auto_examples",
"import_projects",
"notebook_rli",
"notebook_literalinclude",
]

source_suffix = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ this is not work?
:lines: 1
```

<!-- ```{literalinclude} /examples/basics/basics/basic_interactive_mode.ipynb
:caption: basics/basic_interactive_mode.ipynb
test

```{literalinclude} /examples/papermill_plugin/papermill_plugin/nb_simple.ipynb
:caption: papermill_plugin/nb_simple.ipynb
:lines: 1
```

```{nb-literalinclude} /examples/papermill_plugin/papermill_plugin/nb_simple.ipynb
:caption: papermill_plugin/nb_simple.ipynb
:cell: 5
:lines: 1-2
```

```{literalinclude} /examples/basics/basics/basic_interactive_mode.ipynb
:caption: basics/basic_interactive_mode.ipynb
```{nb-literalinclude} /examples/papermill_plugin/papermill_plugin/nb_simple.ipynb
:caption: papermill_plugin/nb_simple.ipynb
:cell: 3
``` -->
```

<!-- [flytesnacks]: https://github.com/flyteorg/flytesnacks/tree/master/examples/basics -->

0 comments on commit 1ed57d2

Please sign in to comment.