88import nbformat
99
1010import textual
11+ import textual_image .widget
1112
1213import traitlets
1314
@@ -19,8 +20,20 @@ class JupyterNetbook(jupyter_core.application.JupyterApp, jupyter_client.console
1920 description = """
2021 A terminal-based app for jupyter notebooks.
2122 """
23+ graphics = traitlets .Enum (
24+ ["auto" , "kitty" , "sixel" , "halfcell" , "unicode" , "tmux-kitty" ],
25+ default_value = "auto" ,
26+ help = "Choice of image rendering protocol" ,
27+ config = True ,
28+ )
2229 flags = traitlets .Dict ({** jupyter_core .application .base_flags , ** jupyter_client .consoleapp .app_flags })
23- aliases = traitlets .Dict ({** jupyter_core .application .base_aliases , ** jupyter_client .consoleapp .app_aliases })
30+ aliases = traitlets .Dict (
31+ {
32+ ** jupyter_core .application .base_aliases ,
33+ ** jupyter_client .consoleapp .app_aliases ,
34+ ** {"graphics" : "JupyterNetbook.graphics" },
35+ }
36+ )
2437
2538 kernel_client_class = jupyter_client .asynchronous .AsyncKernelClient
2639 # kernel_manager_class = jupyter_client.manager.AsyncKernelManager
@@ -42,6 +55,26 @@ def initialize(self, argv):
4255 if self ._dispatching :
4356 return
4457
58+ match self .graphics :
59+ case "auto" :
60+ image_class = textual_image .widget .AutoImage
61+ case "kitty" :
62+ image_class = textual_image .widget .TGPImage
63+ case "sixel" :
64+ image_class = textual_image .widget .SixelImage
65+ case "halfcell" :
66+ image_class = textual_image .widget .HalfcellImage
67+ case "unicode" :
68+ image_class = textual_image .widget .UnicodeImage
69+ case "tmux-kitty" :
70+ import textual_image .renderable as r
71+
72+ r .tgp ._TGP_MESSAGE_START = "\x1b Ptmux;\x1b \x1b _G"
73+ r .tgp ._TGP_MESSAGE_END = "\x1b \x1b \\ \x1b \\ "
74+ image_class = textual_image .widget .TGPImage
75+ case _:
76+ assert False , "unrichable"
77+
4578 nb = None
4679 nbkernel = None
4780 # Determine the notebook name and content
@@ -59,16 +92,16 @@ def initialize(self, argv):
5992 suffix = suffix + 1 if suffix else 1
6093 nbfile = f"Untitled{ suffix } .ipynb"
6194
62- if nbkernel and (
63- "JupyterNetbook" not in self .cli_config or "kernel_name" not in self .cli_config ["JupyterNetbook" ]
64- ):
95+ if nbkernel and "kernel_name" not in self .cli_config .get ("JupyterNetbook" , {}):
6596 # If kernel_name is not explicitly specify but is present in the notebook, use that one
6697 self .kernel_name = nbkernel
6798
6899 jupyter_client .consoleapp .JupyterConsoleApp .initialize (self , argv )
69100
70101 # TODO: if --existing is specified, then self.kernel_manager is None.
71- self .textual_app = JupyterTextualApp (self .kernel_manager , self .kernel_client , nbfile , nb )
102+ self .textual_app = JupyterTextualApp (
103+ self .kernel_manager , self .kernel_client , nbfile , nb , image_class = image_class
104+ )
72105
73106 def start (self ):
74107 jupyter_core .application .JupyterApp .start (self )
0 commit comments