nbcat 0.10.0__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.10.0"
1
+ __version__ = "0.11.0"
nbcat/image.py CHANGED
@@ -10,19 +10,22 @@ from timg import METHODS, Renderer
10
10
 
11
11
 
12
12
  class Image:
13
- def __init__(self, image: str):
13
+ def __init__(self, image: str, method: str = "a24h"):
14
14
  img = BytesIO(base64.b64decode(image.replace("\n", "")))
15
15
  self.image = PilImage.open(img)
16
-
17
- @property
18
- def method_class(self):
19
- # TODO: auto detect terminal to benefit from sixel protocol support
20
- method = "a24h" if system() != "Windows" else "ascii"
21
- return METHODS[method]["class"]
16
+ self.method = method if system() != "Windows" else "ascii"
22
17
 
23
18
  def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
24
- img = Renderer()
25
- img.load_image(self.image)
26
- img.resize(shutil.get_terminal_size()[0] - 1)
27
- output = img.to_string(self.method_class)
28
- yield Text.from_ansi(output)
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__
@@ -69,7 +68,7 @@ def read_notebook(fp: str, debug: bool = False) -> Notebook:
69
68
  raise InvalidNotebookFormatError(f"Invalid notebook: {e}")
70
69
 
71
70
 
72
- def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
71
+ def render_cell(cell: Cell) -> RenderableType:
73
72
  """
74
73
  Render the content of a notebook cell for display.
75
74
 
@@ -86,15 +85,17 @@ def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
86
85
  """
87
86
 
88
87
  def _render_markdown(input: str) -> Markdown:
89
- return Markdown(input)
88
+ return Markdown(input, code_theme="ansi_dark")
90
89
 
91
- def _render_code(input: str) -> Panel:
92
- 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
+ )
93
94
 
94
95
  def _render_raw(input: str) -> Text:
95
96
  return Text(input)
96
97
 
97
- def _render_image(input: str) -> None:
98
+ def _render_image(input: str) -> Image:
98
99
  return Image(input)
99
100
 
100
101
  def _render_json(input: str) -> Pretty:
@@ -111,31 +112,21 @@ def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
111
112
  OutputCellType.JSON: _render_json,
112
113
  }
113
114
 
114
- rows: list[tuple[Union[str, None], RenderableType]] = []
115
+ rows: list[RenderableType] = []
115
116
  renderer = RENDERERS.get(cell.cell_type)
116
117
  source = renderer(cell.input) if renderer else None
117
118
  if source:
118
- label = f"[green][{cell.execution_count}][/]" if cell.execution_count else None
119
- rows.append(
120
- (
121
- label,
122
- source,
123
- )
124
- )
119
+ rows.append(Padding(source, (1, 0)))
120
+ if not cell.outputs:
121
+ return source
125
122
 
126
123
  for o in cell.outputs:
127
124
  if o.output:
128
125
  renderer = RENDERERS.get(o.output.output_type)
129
126
  output = renderer(o.output.text) if renderer else None
130
127
  if output:
131
- label = f"[blue][{o.execution_count}][/]" if o.execution_count else None
132
- rows.append(
133
- (
134
- label,
135
- output,
136
- )
137
- )
138
- return rows
128
+ rows.append(Panel(output, style="italic", box=box.MINIMAL))
129
+ return Group(*rows)
139
130
 
140
131
 
141
132
  def print_notebook(nb: Notebook):
@@ -149,15 +140,17 @@ def print_notebook(nb: Notebook):
149
140
  console.print("[bold red]Notebook contains no cells.")
150
141
  return
151
142
 
152
- layout = Table.grid(padding=1)
153
- layout.add_column(no_wrap=True, width=6)
154
- layout.add_column()
155
-
156
143
  for cell in nb.cells:
157
- for label, content in render_cell(cell):
158
- layout.add_row(label, content)
159
-
160
- 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)
161
154
 
162
155
 
163
156
  def main():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nbcat
3
- Version: 0.10.0
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
@@ -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,13 +0,0 @@
1
- nbcat/__init__.py,sha256=v4zmKjsKOPZbp6BrWoz7iK4ST0sdZdUh9bQSJmluZ5o,23
2
- nbcat/enums.py,sha256=Fn8PIcLl_uY4nQIs1EUvmKTwfhNUIZgmhRFiCSJk9wk,411
3
- nbcat/exceptions.py,sha256=Ho7LQz9K70VtIMDNtAwuAtGmb-lFKxGxSj7MN3-EpDA,321
4
- nbcat/image.py,sha256=xv_IH2JMyzelUvLjVUbubfc8blmRuxqa-FcKXn25LSk,909
5
- nbcat/main.py,sha256=89qxlA6viuRP6_ugbf7zRE4clH4oX67-1t1EbpABUfA,5838
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.10.0.dist-info/METADATA,sha256=jAKupKwXwBtwqMmTT96IE1LihLl3GUhrfLBd0dfTAxw,4103
10
- nbcat-0.10.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
11
- nbcat-0.10.0.dist-info/entry_points.txt,sha256=io_GRDsecAkYuCZALsjyea3VBq91VCoSznqlZEAJshY,42
12
- nbcat-0.10.0.dist-info/licenses/LICENSE,sha256=7GjUnahXdd5opdvlpJdb1BisLbiXt2iOFhzIUduhdkE,1072
13
- nbcat-0.10.0.dist-info/RECORD,,
File without changes