nbcat 0.9.6__py3-none-any.whl → 0.11.0__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.9.6"
1
+ __version__ = "0.11.0"
nbcat/image.py ADDED
@@ -0,0 +1,31 @@
1
+ import base64
2
+ import shutil
3
+ from io import BytesIO
4
+ from platform import system
5
+
6
+ from PIL import Image as PilImage
7
+ from rich.console import Console, ConsoleOptions, RenderResult
8
+ from rich.text import Text
9
+ from timg import METHODS, Renderer
10
+
11
+
12
+ class Image:
13
+ def __init__(self, image: str, method: str = "a24h"):
14
+ img = BytesIO(base64.b64decode(image.replace("\n", "")))
15
+ self.image = PilImage.open(img)
16
+ self.method = method if system() != "Windows" else "ascii"
17
+
18
+ def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
19
+ renderer = Renderer()
20
+ renderer.load_image(self.image)
21
+ width = shutil.get_terminal_size()[0] - 1
22
+ if self.method == "sixel":
23
+ width = width * 6
24
+
25
+ renderer.resize(width)
26
+
27
+ if self.method == "sixel":
28
+ renderer.reduce_colors(16)
29
+
30
+ output = renderer.to_string(METHODS[self.method]["class"])
31
+ yield Text.from_ansi(output, no_wrap=True, end="")
nbcat/main.py CHANGED
@@ -1,18 +1,17 @@
1
1
  import argparse
2
2
  import sys
3
3
  from pathlib import Path
4
- from typing import Union
5
4
 
6
5
  import argcomplete
7
6
  import requests
8
7
  from argcomplete.completers import FilesCompleter
9
8
  from pydantic import ValidationError
10
9
  from rich import box
11
- from rich.console import Console, RenderableType
10
+ from rich.console import Console, Group, RenderableType
11
+ from rich.padding import Padding
12
12
  from rich.panel import Panel
13
13
  from rich.pretty import Pretty
14
14
  from rich.syntax import Syntax
15
- from rich.table import Table
16
15
  from rich.text import Text
17
16
 
18
17
  from . import __version__
@@ -22,6 +21,7 @@ from .exceptions import (
22
21
  NotebookNotFoundError,
23
22
  UnsupportedNotebookTypeError,
24
23
  )
24
+ from .image import Image
25
25
  from .markdown import Markdown
26
26
  from .schemas import Cell, Notebook
27
27
 
@@ -68,7 +68,7 @@ def read_notebook(fp: str, debug: bool = False) -> Notebook:
68
68
  raise InvalidNotebookFormatError(f"Invalid notebook: {e}")
69
69
 
70
70
 
71
- def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
71
+ def render_cell(cell: Cell) -> RenderableType:
72
72
  """
73
73
  Render the content of a notebook cell for display.
74
74
 
@@ -85,16 +85,18 @@ def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
85
85
  """
86
86
 
87
87
  def _render_markdown(input: str) -> Markdown:
88
- return Markdown(input)
88
+ return Markdown(input, code_theme="ansi_dark")
89
89
 
90
- def _render_code(input: str) -> Panel:
91
- return Panel(Syntax(input, "python", theme="ansi_dark"), box=box.SQUARE)
90
+ def _render_code(input: str, language: str = "python") -> Panel:
91
+ return Panel(
92
+ Syntax(input, language, line_numbers=True, theme="ansi_dark", dedent=True), padding=0
93
+ )
92
94
 
93
95
  def _render_raw(input: str) -> Text:
94
96
  return Text(input)
95
97
 
96
- def _render_image(input: str) -> None:
97
- return None
98
+ def _render_image(input: str) -> Image:
99
+ return Image(input)
98
100
 
99
101
  def _render_json(input: str) -> Pretty:
100
102
  return Pretty(input)
@@ -110,31 +112,21 @@ def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
110
112
  OutputCellType.JSON: _render_json,
111
113
  }
112
114
 
113
- rows: list[tuple[Union[str, None], RenderableType]] = []
115
+ rows: list[RenderableType] = []
114
116
  renderer = RENDERERS.get(cell.cell_type)
115
117
  source = renderer(cell.input) if renderer else None
116
118
  if source:
117
- label = f"[green][{cell.execution_count}][/]" if cell.execution_count else None
118
- rows.append(
119
- (
120
- label,
121
- source,
122
- )
123
- )
119
+ rows.append(Padding(source, (1, 0)))
120
+ if not cell.outputs:
121
+ return source
124
122
 
125
123
  for o in cell.outputs:
126
124
  if o.output:
127
125
  renderer = RENDERERS.get(o.output.output_type)
128
126
  output = renderer(o.output.text) if renderer else None
129
127
  if output:
130
- label = f"[blue][{o.execution_count}][/]" if o.execution_count else None
131
- rows.append(
132
- (
133
- label,
134
- output,
135
- )
136
- )
137
- return rows
128
+ rows.append(Panel(output, style="italic", box=box.MINIMAL))
129
+ return Group(*rows)
138
130
 
139
131
 
140
132
  def print_notebook(nb: Notebook):
@@ -148,15 +140,17 @@ def print_notebook(nb: Notebook):
148
140
  console.print("[bold red]Notebook contains no cells.")
149
141
  return
150
142
 
151
- layout = Table.grid(padding=1)
152
- layout.add_column(no_wrap=True, width=6)
153
- layout.add_column()
154
-
155
143
  for cell in nb.cells:
156
- for label, content in render_cell(cell):
157
- layout.add_row(label, content)
158
-
159
- console.print(layout)
144
+ rendered = render_cell(cell)
145
+ if isinstance(rendered, Group):
146
+ out = Panel(
147
+ rendered,
148
+ title=f"[green][{cell.execution_count}][/]" if cell.execution_count else None,
149
+ title_align="left",
150
+ )
151
+ else:
152
+ out = Padding(rendered, (1, 0))
153
+ console.print(out)
160
154
 
161
155
 
162
156
  def main():
nbcat/schemas.py CHANGED
@@ -34,7 +34,7 @@ class DisplayDataOutput(BaseOutput):
34
34
  def output(self) -> Union[CellOutput, None]:
35
35
  data_type_map = {
36
36
  "text/html": OutputCellType.HTML,
37
- "text/png": OutputCellType.IMAGE,
37
+ "image/png": OutputCellType.IMAGE,
38
38
  "text/plain": OutputCellType.PLAIN,
39
39
  "application/vnd.raw.v1+json": OutputCellType.JSON,
40
40
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nbcat
3
- Version: 0.9.6
3
+ Version: 0.11.0
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
@@ -33,6 +33,7 @@ Requires-Dist: argcomplete
33
33
  Requires-Dist: pydantic
34
34
  Requires-Dist: requests
35
35
  Requires-Dist: rich
36
+ Requires-Dist: timg
36
37
  Provides-Extra: dev
37
38
  Requires-Dist: pytest; extra == 'dev'
38
39
  Requires-Dist: pytest-cov; extra == 'dev'
@@ -0,0 +1,13 @@
1
+ nbcat/__init__.py,sha256=raMu9XA9JEjvdoTmFqcOw7qhJX24rYDP7XmS59TAO-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=vchFzwcvPGmv4pg5AHRaOOsZh83ynmxkTqIKl0z_Sys,5749
6
+ nbcat/markdown.py,sha256=K6yL9rY3uTxMPsRIJnxqNHmCX2Qc-f3my8eiHxTLfic,1835
7
+ nbcat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ nbcat/schemas.py,sha256=a7GoKgPTHgun199-J-sZq-ahkdQwvyRaCdQVg1gC798,3135
9
+ nbcat-0.11.0.dist-info/METADATA,sha256=jmAcfrystY8f3iRq_lm6g7PdhRbPntAfcYsr0sx1TsQ,4103
10
+ nbcat-0.11.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
+ nbcat-0.11.0.dist-info/entry_points.txt,sha256=io_GRDsecAkYuCZALsjyea3VBq91VCoSznqlZEAJshY,42
12
+ nbcat-0.11.0.dist-info/licenses/LICENSE,sha256=7GjUnahXdd5opdvlpJdb1BisLbiXt2iOFhzIUduhdkE,1072
13
+ nbcat-0.11.0.dist-info/RECORD,,
@@ -1,12 +0,0 @@
1
- nbcat/__init__.py,sha256=IgVHjr-TeioZYLJSkvpT80LLGi6U3ONzR1cfYfd5XNQ,22
2
- nbcat/enums.py,sha256=Fn8PIcLl_uY4nQIs1EUvmKTwfhNUIZgmhRFiCSJk9wk,411
3
- nbcat/exceptions.py,sha256=Ho7LQz9K70VtIMDNtAwuAtGmb-lFKxGxSj7MN3-EpDA,321
4
- nbcat/main.py,sha256=7RYjHvWmKWoD9LFI1dopKvp_wvqWa2Rk9AYZ_RsrIaw,5805
5
- nbcat/markdown.py,sha256=K6yL9rY3uTxMPsRIJnxqNHmCX2Qc-f3my8eiHxTLfic,1835
6
- nbcat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- nbcat/schemas.py,sha256=fjSbdXa4pHKRV1Z-vUESkYZywkojanCvSu8s7rc9Xkw,3134
8
- nbcat-0.9.6.dist-info/METADATA,sha256=v-8f9HAlHG2t-Mw36CZAIGcMRTeCWiyt0BzZMlS164w,4082
9
- nbcat-0.9.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- nbcat-0.9.6.dist-info/entry_points.txt,sha256=io_GRDsecAkYuCZALsjyea3VBq91VCoSznqlZEAJshY,42
11
- nbcat-0.9.6.dist-info/licenses/LICENSE,sha256=7GjUnahXdd5opdvlpJdb1BisLbiXt2iOFhzIUduhdkE,1072
12
- nbcat-0.9.6.dist-info/RECORD,,
File without changes