nbcat 0.12.1__py3-none-any.whl → 0.13.1__py3-none-any.whl

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.
nbcat/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.12.1"
1
+ __version__ = "0.13.1"
nbcat/main.py CHANGED
@@ -5,6 +5,7 @@ from pathlib import Path
5
5
  import argcomplete
6
6
  import requests
7
7
  from argcomplete.completers import FilesCompleter
8
+ from markdownify import markdownify
8
9
  from pydantic import ValidationError
9
10
  from rich.console import Console, Group, RenderableType
10
11
  from rich.padding import Padding
@@ -22,6 +23,7 @@ from .exceptions import (
22
23
  )
23
24
  from .image import Image
24
25
  from .markdown import Markdown
26
+ from .pager import Pager
25
27
  from .schemas import Cell, Notebook
26
28
 
27
29
  console = Console()
@@ -84,7 +86,7 @@ def render_cell(cell: Cell) -> RenderableType:
84
86
  """
85
87
 
86
88
  def _render_markdown(input: str) -> Markdown:
87
- return Markdown(input, code_theme="ansi_dark")
89
+ return Markdown(markdownify(input), code_theme="ansi_dark")
88
90
 
89
91
  def _render_code(input: str, language: str = "python") -> Syntax:
90
92
  return Syntax(input, language, theme="ansi_dark", padding=(1, 2), dedent=True)
@@ -135,20 +137,26 @@ def render_cell(cell: Cell) -> RenderableType:
135
137
  return Group(*rows)
136
138
 
137
139
 
138
- def print_notebook(nb: Notebook):
140
+ def render_notebook(nb: Notebook) -> list[RenderableType]:
139
141
  """
140
- Print the notebook to the console with formatted cell inputs and outputs.
142
+ Convert a Notebook object into a list of rich renderables for terminal display.
143
+
144
+ Each cell in the notebook is processed and rendered using `render_cell`,
145
+ producing a sequence of styled input/output blocks suitable for use in a
146
+ Textual or Rich-based terminal UI.
141
147
 
142
148
  Args:
143
- nb (Notebook): A Notebook object containing a list of cells.
144
- """
145
- if not nb.cells:
146
- console.print("[bold red]Notebook contains no cells.")
147
- return
149
+ nb (Notebook): The notebook object containing parsed cells (e.g., from a .ipynb file).
148
150
 
151
+ Returns
152
+ -------
153
+ list[RenderableType]: A list of rich renderable objects representing the notebook cells.
154
+ Returns an empty list if the notebook has no cells.
155
+ """
156
+ rendered: list[RenderableType] = []
149
157
  for cell in nb.cells:
150
- rendered = render_cell(cell)
151
- console.print(rendered)
158
+ rendered.append(render_cell(cell))
159
+ return rendered
152
160
 
153
161
 
154
162
  def main():
@@ -160,20 +168,38 @@ def main():
160
168
  "file", help="Path or URL to a .ipynb notebook", type=str
161
169
  ).completer = FilesCompleter()
162
170
  parser.add_argument(
171
+ "-v",
163
172
  "--version",
164
173
  help="print version information and quite",
165
174
  action="version",
166
175
  version=__version__,
167
176
  )
168
177
  parser.add_argument(
169
- "--debug", help="enable extended error output", action="store_true", default=False
178
+ "-d", "--debug", help="enable extended error output", action="store_true", default=False
179
+ )
180
+ parser.add_argument(
181
+ "-p",
182
+ "--page",
183
+ help="enable paginated view mode (similar to less)",
184
+ action="store_true",
185
+ default=False,
170
186
  )
171
187
 
172
188
  try:
173
189
  argcomplete.autocomplete(parser)
174
190
  args = parser.parse_args()
191
+
175
192
  notebook = read_notebook(args.file, debug=args.debug)
176
- print_notebook(notebook)
193
+ objects = render_notebook(notebook)
194
+
195
+ if not objects:
196
+ console.print("[bold red]Notebook contains no cells.")
197
+ return
198
+
199
+ if args.page:
200
+ Pager(objects).run()
201
+ else:
202
+ console.print(*objects)
177
203
  except Exception as e:
178
204
  sys.exit(f"nbcat: {e}")
179
205
 
nbcat/markdown.py CHANGED
@@ -41,16 +41,17 @@ class ImageItem(md.ImageItem):
41
41
  def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
42
42
  image_content = None
43
43
  path = Path(self.destination)
44
- if path.exists():
45
- image_content = path.read_bytes()
46
- elif self.destination.startswith("http://") or self.destination.startswith("https://"):
47
- try:
48
- with requests.Session() as req:
49
- res = req.get(self.destination, timeout=5)
50
- res.raise_for_status()
51
- image_content = res.content
52
- except requests.RequestException:
53
- return super().__rich_console__(console, options)
44
+ if path.suffix in [".png", ".jpeg", ".jpg"]:
45
+ if path.exists():
46
+ image_content = path.read_bytes()
47
+ elif self.destination.startswith("http://") or self.destination.startswith("https://"):
48
+ try:
49
+ with requests.Session() as req:
50
+ res = req.get(self.destination, timeout=5)
51
+ res.raise_for_status()
52
+ image_content = res.content
53
+ except requests.RequestException:
54
+ return super().__rich_console__(console, options)
54
55
  if image_content:
55
56
  # TODO: This part can be improved by changing Image class to accept file objects
56
57
  image = base64.b64encode(image_content).decode("utf-8")
nbcat/pager.py ADDED
@@ -0,0 +1,82 @@
1
+ from rich.console import RenderableType
2
+ from textual.app import App, ComposeResult
3
+ from textual.binding import Binding
4
+ from textual.containers import VerticalScroll
5
+ from textual.widgets import Static
6
+
7
+
8
+ class Pager(App):
9
+ BINDINGS = [
10
+ # Exit
11
+ Binding("q", "quit", "Quit"),
12
+ Binding(":q", "quit", "Quit"),
13
+ Binding("Q", "quit", "Quit"),
14
+ Binding(":Q", "quit", "Quit"),
15
+ Binding("ZZ", "quit", "Quit"),
16
+ # One line
17
+ Binding("j", "scroll_down", "Down"),
18
+ Binding("e", "scroll_down", "Down"),
19
+ Binding("^e", "scroll_down", "Down"),
20
+ Binding("^n", "scroll_down", "Down"),
21
+ Binding("cr", "scroll_down", "Down"), # carriage return = Enter
22
+ Binding("k", "scroll_up", "Up"),
23
+ Binding("y", "scroll_up", "Up"),
24
+ Binding("ctrl+y", "scroll_up", "Up"),
25
+ Binding("ctrl+k", "scroll_up", "Up"),
26
+ Binding("ctrl+p", "scroll_up", "Up"),
27
+ # One window
28
+ Binding("f", "page_down", "Page Down"),
29
+ Binding("ctrl+f", "page_down", "Page Down"),
30
+ Binding("ctrl+v", "page_down", "Page Down"),
31
+ Binding("space", "page_down", "Page Down"),
32
+ Binding("b", "page_up", "Page Up"),
33
+ Binding("ctrl+b", "page_up", "Page Up"),
34
+ Binding("escape+v", "page_up", "Page Up"),
35
+ # Extended window control
36
+ Binding("z", "page_down", "Window Down (set N)"),
37
+ Binding("w", "page_up", "Window Up (set N)"),
38
+ Binding("escape+space", "page_down", "Forward one window, no EOF stop"),
39
+ Binding("d", "half_page_down", "Half Page Down"),
40
+ Binding("ctrl+d", "half_page_down", "Half Page Down"),
41
+ # Jumping
42
+ Binding("g", "go_to_top", "Top of File"),
43
+ Binding("<", "go_to_top", "Top of File"),
44
+ Binding("escape+<", "go_to_top", "Top of File"),
45
+ Binding("G", "go_to_bottom", "Bottom of File"),
46
+ Binding(">", "go_to_bottom", "Bottom of File"),
47
+ Binding("escape+>", "go_to_bottom", "Bottom of File"),
48
+ ]
49
+
50
+ def __init__(self, objects: list[RenderableType]):
51
+ super().__init__()
52
+ self._objects = objects
53
+
54
+ def compose(self) -> ComposeResult:
55
+ with VerticalScroll():
56
+ for obj in self._objects:
57
+ yield Static(obj)
58
+
59
+ def on_mount(self) -> None:
60
+ self.theme = "textual-ansi"
61
+ self.viewer = self.query_one(VerticalScroll)
62
+
63
+ def action_scroll_down(self) -> None:
64
+ self.viewer.scroll_to(y=self.viewer.scroll_y + 1)
65
+
66
+ def action_scroll_up(self) -> None:
67
+ self.viewer.scroll_to(y=self.viewer.scroll_y - 1)
68
+
69
+ def action_page_down(self) -> None:
70
+ self.viewer.scroll_to(y=self.viewer.scroll_y + self.viewer.virtual_size.height)
71
+
72
+ def action_page_up(self) -> None:
73
+ self.viewer.scroll_to(y=max(self.viewer.scroll_y - self.viewer.virtual_size.height, 0))
74
+
75
+ def action_half_page_down(self) -> None:
76
+ self.viewer.scroll_to(y=self.viewer.scroll_y + (self.viewer.virtual_size.height / 2))
77
+
78
+ def action_go_to_top(self) -> None:
79
+ self.viewer.scroll_to(y=0)
80
+
81
+ def action_go_to_bottom(self) -> None:
82
+ self.viewer.scroll_to(y=self.viewer.virtual_size.height)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nbcat
3
- Version: 0.12.1
3
+ Version: 0.13.1
4
4
  Summary: cat for jupyter notebooks
5
5
  Project-URL: Homepage, https://github.com/akopdev/nbcat
6
6
  Project-URL: Repository, https://github.com/akopdev/nbcat
@@ -30,9 +30,11 @@ License: MIT License
30
30
  License-File: LICENSE
31
31
  Requires-Python: >=3.9
32
32
  Requires-Dist: argcomplete
33
+ Requires-Dist: markdownify
33
34
  Requires-Dist: pydantic
34
35
  Requires-Dist: requests
35
36
  Requires-Dist: rich
37
+ Requires-Dist: textual
36
38
  Requires-Dist: timg
37
39
  Provides-Extra: dev
38
40
  Requires-Dist: pytest; extra == 'dev'
@@ -48,13 +50,16 @@ Description-Content-Type: text/markdown
48
50
 
49
51
  <p align="center">
50
52
  <a href="docs/screenshot.png" target="blank"><img src="docs/screenshot.png" width="400" /></a>
53
+ <a href="docs/screenshot2.png" target="blank"><img src="docs/screenshot2.png" width="400" /></a>
51
54
  </p>
52
55
 
53
56
  ## Features
54
57
 
55
- - Very fast and lightweight with minimal dependencies
56
- - Preview remote notebooks without downloading them
57
- - Supports for all Jupyter notebook versions, including old legacy formats
58
+ - Very fast and lightweight with minimal dependencies.
59
+ - Preview remote notebooks without downloading them.
60
+ - Enable paginated view mode with keyboard navigation (similar to `less`).
61
+ - Supports image rendering (some protocols in beta)
62
+ - Supports for all Jupyter notebook versions, including old legacy formats.
58
63
 
59
64
  ## Motivation
60
65
 
@@ -0,0 +1,14 @@
1
+ nbcat/__init__.py,sha256=Zg3oo58_HXe_ieb_PwWnYkKGH2zTvu6G2jly-7GnPGo,23
2
+ nbcat/enums.py,sha256=Fn8PIcLl_uY4nQIs1EUvmKTwfhNUIZgmhRFiCSJk9wk,411
3
+ nbcat/exceptions.py,sha256=Ho7LQz9K70VtIMDNtAwuAtGmb-lFKxGxSj7MN3-EpDA,321
4
+ nbcat/image.py,sha256=hGZZK5mtij7ckWAvQwzim3thqGC92pLW5ZU5CYbRv_Q,995
5
+ nbcat/main.py,sha256=pTOt0Kt7WJqf7H--JmXtvBgFJG-bBu6WsBG4KFl9wAE,6727
6
+ nbcat/markdown.py,sha256=fbYBy36rR0SKqvNI6Yj75Xv2snwFRmhbCJIC1wCX2Hg,3055
7
+ nbcat/pager.py,sha256=Yu0XIh5MvhvT-cmULYVxo6s2Ufjf2CQ-NDS5fcMQ-IM,3184
8
+ nbcat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ nbcat/schemas.py,sha256=a7GoKgPTHgun199-J-sZq-ahkdQwvyRaCdQVg1gC798,3135
10
+ nbcat-0.13.1.dist-info/METADATA,sha256=qU0l9XpIN1Beqb3sI5-1s4UvHjSgHHTDFp05RAu9fZc,4443
11
+ nbcat-0.13.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
+ nbcat-0.13.1.dist-info/entry_points.txt,sha256=io_GRDsecAkYuCZALsjyea3VBq91VCoSznqlZEAJshY,42
13
+ nbcat-0.13.1.dist-info/licenses/LICENSE,sha256=7GjUnahXdd5opdvlpJdb1BisLbiXt2iOFhzIUduhdkE,1072
14
+ nbcat-0.13.1.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- nbcat/__init__.py,sha256=PAuBI8I6F9Yu_86XjI2yaWn8QmCd9ZvK7tkZLWvEg-Q,23
2
- nbcat/enums.py,sha256=Fn8PIcLl_uY4nQIs1EUvmKTwfhNUIZgmhRFiCSJk9wk,411
3
- nbcat/exceptions.py,sha256=Ho7LQz9K70VtIMDNtAwuAtGmb-lFKxGxSj7MN3-EpDA,321
4
- nbcat/image.py,sha256=hGZZK5mtij7ckWAvQwzim3thqGC92pLW5ZU5CYbRv_Q,995
5
- nbcat/main.py,sha256=gZ8mM2louxU8GgB40F7DE7pc7MAidDHatiNjkLUzeug,5839
6
- nbcat/markdown.py,sha256=GkPNfzBhNLMl89pVcj14U6ytYUVk3yNB0G1zGYRRJ04,2962
7
- nbcat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- nbcat/schemas.py,sha256=a7GoKgPTHgun199-J-sZq-ahkdQwvyRaCdQVg1gC798,3135
9
- nbcat-0.12.1.dist-info/METADATA,sha256=Jq7TL6gCnqsAn_hm5eTHHK-DRGbp3nXn4aq6-w_jv2Y,4164
10
- nbcat-0.12.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
- nbcat-0.12.1.dist-info/entry_points.txt,sha256=io_GRDsecAkYuCZALsjyea3VBq91VCoSznqlZEAJshY,42
12
- nbcat-0.12.1.dist-info/licenses/LICENSE,sha256=7GjUnahXdd5opdvlpJdb1BisLbiXt2iOFhzIUduhdkE,1072
13
- nbcat-0.12.1.dist-info/RECORD,,
File without changes