11import argparse
22import sys
33from pathlib import Path
4- from typing import Union
54
65import argcomplete
76import requests
87from argcomplete .completers import FilesCompleter
98from pydantic import ValidationError
109from rich import box
11- from rich .console import Console , RenderableType
10+ from rich .console import Console , Group , RenderableType
11+ from rich .padding import Padding
1212from rich .panel import Panel
1313from rich .pretty import Pretty
1414from rich .syntax import Syntax
15- from rich .table import Table
1615from rich .text import Text
1716
1817from . import __version__
@@ -69,7 +68,7 @@ def read_notebook(fp: str, debug: bool = False) -> Notebook:
6968 raise InvalidNotebookFormatError (f"Invalid notebook: { e } " )
7069
7170
72- def render_cell (cell : Cell ) -> list [ tuple [ Union [ str , None ], RenderableType ]] :
71+ def render_cell (cell : Cell ) -> RenderableType :
7372 """
7473 Render the content of a notebook cell for display.
7574
@@ -86,15 +85,17 @@ def render_cell(cell: Cell) -> list[tuple[Union[str, None], RenderableType]]:
8685 """
8786
8887 def _render_markdown (input : str ) -> Markdown :
89- return Markdown (input )
88+ return Markdown (input , code_theme = "ansi_dark" )
9089
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+ )
9394
9495 def _render_raw (input : str ) -> Text :
9596 return Text (input )
9697
97- def _render_image (input : str ) -> None :
98+ def _render_image (input : str ) -> Image :
9899 return Image (input )
99100
100101 def _render_json (input : str ) -> Pretty :
@@ -111,31 +112,21 @@ def _render_json(input: str) -> Pretty:
111112 OutputCellType .JSON : _render_json ,
112113 }
113114
114- rows : list [tuple [ Union [ str , None ], RenderableType ] ] = []
115+ rows : list [RenderableType ] = []
115116 renderer = RENDERERS .get (cell .cell_type )
116117 source = renderer (cell .input ) if renderer else None
117118 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
125122
126123 for o in cell .outputs :
127124 if o .output :
128125 renderer = RENDERERS .get (o .output .output_type )
129126 output = renderer (o .output .text ) if renderer else None
130127 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 )
139130
140131
141132def print_notebook (nb : Notebook ):
@@ -149,15 +140,17 @@ def print_notebook(nb: Notebook):
149140 console .print ("[bold red]Notebook contains no cells." )
150141 return
151142
152- layout = Table .grid (padding = 1 )
153- layout .add_column (no_wrap = True , width = 6 )
154- layout .add_column ()
155-
156143 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 )
161154
162155
163156def main ():
0 commit comments