nbcat 0.11.0__py3-none-any.whl → 0.12.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.11.0"
1
+ __version__ = "0.12.1"
nbcat/main.py CHANGED
@@ -6,7 +6,6 @@ import argcomplete
6
6
  import requests
7
7
  from argcomplete.completers import FilesCompleter
8
8
  from pydantic import ValidationError
9
- from rich import box
10
9
  from rich.console import Console, Group, RenderableType
11
10
  from rich.padding import Padding
12
11
  from rich.panel import Panel
@@ -87,10 +86,8 @@ def render_cell(cell: Cell) -> RenderableType:
87
86
  def _render_markdown(input: str) -> Markdown:
88
87
  return Markdown(input, code_theme="ansi_dark")
89
88
 
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
- )
89
+ def _render_code(input: str, language: str = "python") -> Syntax:
90
+ return Syntax(input, language, theme="ansi_dark", padding=(1, 2), dedent=True)
94
91
 
95
92
  def _render_raw(input: str) -> Text:
96
93
  return Text(input)
@@ -116,16 +113,25 @@ def render_cell(cell: Cell) -> RenderableType:
116
113
  renderer = RENDERERS.get(cell.cell_type)
117
114
  source = renderer(cell.input) if renderer else None
118
115
  if source:
119
- rows.append(Padding(source, (1, 0)))
116
+ s_title = f"[green]In [{cell.execution_count}][/]" if cell.execution_count else None
117
+ if s_title:
118
+ rows.append(Panel(source, title=s_title, title_align="left"))
119
+ else:
120
+ rows.append(Padding(source, (1, 0)))
121
+
120
122
  if not cell.outputs:
121
- return source
123
+ return rows.pop()
122
124
 
123
125
  for o in cell.outputs:
126
+ o_title = f"[blue]Out [{o.execution_count}][/]" if o.execution_count else None
124
127
  if o.output:
125
128
  renderer = RENDERERS.get(o.output.output_type)
126
129
  output = renderer(o.output.text) if renderer else None
127
130
  if output:
128
- rows.append(Panel(output, style="italic", box=box.MINIMAL))
131
+ if o_title:
132
+ rows.append(Panel(output, style="italic", title=o_title, title_align="left"))
133
+ else:
134
+ rows.append(Padding(output, (1, 0), style="italic"))
129
135
  return Group(*rows)
130
136
 
131
137
 
@@ -142,15 +148,7 @@ def print_notebook(nb: Notebook):
142
148
 
143
149
  for cell in nb.cells:
144
150
  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)
151
+ console.print(rendered)
154
152
 
155
153
 
156
154
  def main():
nbcat/markdown.py CHANGED
@@ -10,12 +10,17 @@
10
10
 
11
11
  from __future__ import annotations
12
12
 
13
+ import base64
14
+ from pathlib import Path
13
15
  from typing import ClassVar
14
16
 
17
+ import requests
15
18
  from rich import markdown as md
16
19
  from rich.console import Console, ConsoleOptions, RenderResult
17
20
  from rich.text import Text
18
21
 
22
+ from .image import Image
23
+
19
24
 
20
25
  class Heading(md.Heading):
21
26
  """A heading."""
@@ -30,6 +35,29 @@ class Heading(md.Heading):
30
35
  yield Text(f"{'#' * indent} {self.text}", style=styles.get(self.tag, "dim white"))
31
36
 
32
37
 
38
+ class ImageItem(md.ImageItem):
39
+ """Renders a placeholder for an image."""
40
+
41
+ def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
42
+ image_content = None
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)
54
+ if image_content:
55
+ # TODO: This part can be improved by changing Image class to accept file objects
56
+ image = base64.b64encode(image_content).decode("utf-8")
57
+ return Image(image).__rich_console__(console, options)
58
+ return super().__rich_console__(console, options)
59
+
60
+
33
61
  class Markdown(md.Markdown):
34
62
  elements: ClassVar[dict[str, type[md.MarkdownElement]]] = {
35
63
  "paragraph_open": md.Paragraph,
@@ -41,7 +69,7 @@ class Markdown(md.Markdown):
41
69
  "bullet_list_open": md.ListElement,
42
70
  "ordered_list_open": md.ListElement,
43
71
  "list_item_open": md.ListItem,
44
- "image": md.ImageItem,
72
+ "image": ImageItem,
45
73
  "table_open": md.TableElement,
46
74
  "tbody_open": md.TableBodyElement,
47
75
  "thead_open": md.TableHeaderElement,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nbcat
3
- Version: 0.11.0
3
+ Version: 0.12.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
@@ -68,10 +68,13 @@ Please note, that `nbcat` doesn't aim to replace JupyterLab. If you need a full-
68
68
 
69
69
  ## Installation
70
70
 
71
- From the command line using pip:
72
-
73
71
  ```bash
72
+ # Install from PyPI
74
73
  pip install nbcat
74
+
75
+ # Install via Homebrew
76
+ brew tab akopdev/formulas/nbcat
77
+ brew install nbcat
75
78
  ```
76
79
 
77
80
  ## Quickstart
@@ -0,0 +1,13 @@
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,,
@@ -1,13 +0,0 @@
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,,
File without changes