ipython-icat 0.0.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.
icat/__init__.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# modified from https://github.com/jktr/matplotlib-backend-kitty
|
|
2
|
+
|
|
3
|
+
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
|
|
4
|
+
from IPython.core.magic import Magics, magics_class, line_magic
|
|
5
|
+
|
|
6
|
+
from subprocess import run
|
|
7
|
+
from io import BytesIO
|
|
8
|
+
import sys
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from matplotlib.backend_bases import _Backend, FigureManagerBase
|
|
12
|
+
from matplotlib.backends.backend_agg import FigureCanvasAgg
|
|
13
|
+
from matplotlib import interactive, is_interactive
|
|
14
|
+
from matplotlib._pylab_helpers import Gcf
|
|
15
|
+
import matplotlib
|
|
16
|
+
|
|
17
|
+
from PIL import Image
|
|
18
|
+
|
|
19
|
+
if hasattr(sys, "ps1") or sys.flags.interactive:
|
|
20
|
+
interactive(True)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _run(*cmd):
|
|
24
|
+
def f(*args, output=True, **kwargs):
|
|
25
|
+
if output:
|
|
26
|
+
kwargs["capture_output"] = True
|
|
27
|
+
kwargs["text"] = True
|
|
28
|
+
r = run(cmd + args, **kwargs)
|
|
29
|
+
if output:
|
|
30
|
+
return r.stdout.rstrip()
|
|
31
|
+
|
|
32
|
+
return f
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
_icat = _run("kitty", "+kitten", "icat", "--align", "left")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class FigureManagerICat(FigureManagerBase):
|
|
39
|
+
def show(self):
|
|
40
|
+
with BytesIO() as buf:
|
|
41
|
+
self.canvas.figure.savefig(buf, format="png")
|
|
42
|
+
_icat(output=False, input=buf.getbuffer())
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class FigureCanvasICat(FigureCanvasAgg):
|
|
46
|
+
manager_class = FigureManagerICat
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@_Backend.export
|
|
50
|
+
class _BackendICatAgg(_Backend):
|
|
51
|
+
FigureCanvas = FigureCanvasICat
|
|
52
|
+
FigureManager = FigureManagerICat
|
|
53
|
+
mainloop = lambda: None
|
|
54
|
+
|
|
55
|
+
@classmethod
|
|
56
|
+
def draw_if_interactive(cls):
|
|
57
|
+
manager = Gcf.get_active()
|
|
58
|
+
if is_interactive() and manager.canvas.figure.get_axes():
|
|
59
|
+
cls.show()
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def show(cls, *args, **kwargs):
|
|
63
|
+
_Backend.show(*args, **kwargs)
|
|
64
|
+
Gcf.destroy_all()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@magics_class
|
|
68
|
+
class ICatMagics(Magics):
|
|
69
|
+
@line_magic
|
|
70
|
+
def plt_icat(self, line):
|
|
71
|
+
matplotlib.use("module://icat")
|
|
72
|
+
print("loaded icat backend for mpl")
|
|
73
|
+
|
|
74
|
+
@magic_arguments()
|
|
75
|
+
@argument("image", help="PIL Image object or path to image file")
|
|
76
|
+
@argument("-w", "--width", type=int, help="Width to resize the image")
|
|
77
|
+
@argument("-h", "--height", type=int, help="Height to resize the image")
|
|
78
|
+
@line_magic
|
|
79
|
+
def icat(self, line):
|
|
80
|
+
args = parse_argstring(self.icat, line)
|
|
81
|
+
image_arg = args.image.strip()
|
|
82
|
+
|
|
83
|
+
# check if the input is a variable in the user's ns
|
|
84
|
+
user_ns = self.shell.user_ns
|
|
85
|
+
if image_arg in user_ns and isinstance(user_ns[image_arg], Image.Image):
|
|
86
|
+
img = user_ns[image_arg]
|
|
87
|
+
elif os.path.isfile(image_arg):
|
|
88
|
+
img = Image.open(image_arg)
|
|
89
|
+
else:
|
|
90
|
+
print(
|
|
91
|
+
f"Error: '{image_arg}' is neither a valid PIL Image nor a path to an image file."
|
|
92
|
+
)
|
|
93
|
+
return
|
|
94
|
+
|
|
95
|
+
# resize the image if width or height is specified
|
|
96
|
+
if args.width or args.height:
|
|
97
|
+
img.thumbnail((args.width or img.width, args.height or img.height))
|
|
98
|
+
|
|
99
|
+
# display image
|
|
100
|
+
with BytesIO() as buf:
|
|
101
|
+
img.save(buf, format="PNG")
|
|
102
|
+
_icat(output=False, input=buf.getbuffer())
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def load_ipython_extension(ipython):
|
|
106
|
+
ipython.register_magics(ICatMagics)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: ipython-icat
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: IPython magic command for displaying images in kitty terminal
|
|
5
|
+
Author-email: Tony Kabilan Okeke <tonykabilanokeke@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/Kabilan108/ipython-icat
|
|
7
|
+
Keywords: ipython,magic,image,display,kitty,icat
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: ipython >=8.18.1
|
|
11
|
+
Requires-Dist: matplotlib >=3.9.2
|
|
12
|
+
Requires-Dist: pillow >=10.4.0
|
|
13
|
+
|
|
14
|
+
# ipython-icat
|
|
15
|
+
|
|
16
|
+
This Python module enables you to display plots created with [matplotlib](https://github.com/matplotlib/matplotlib) and [PIL](https://python-pillow.org/) Image objects directly in your [kitty terminal](https://github.com/kovidgoyal/kitty) when using IPython.
|
|
17
|
+
|
|
18
|
+
This is modified from [matplotlib-backend-kitty](https://github.com/jktr/matplotlib-backend-kitty) but is designed to be used via IPython [magic commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html).
|
|
19
|
+
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
icat/__init__.py,sha256=Oti_NBrWGFNqnLvOaqMru58HmI1btXNlT_m9t8rYKQg,3100
|
|
2
|
+
ipython_icat-0.0.1.dist-info/METADATA,sha256=bVgeWnhqxDYHI0VDyDZZWFXnG_r6RW65Jj3g53FvEGE,964
|
|
3
|
+
ipython_icat-0.0.1.dist-info/WHEEL,sha256=ixB2d4u7mugx_bCBycvM9OzZ5yD7NmPXFRtKlORZS2Y,91
|
|
4
|
+
ipython_icat-0.0.1.dist-info/top_level.txt,sha256=4ry8J0LQzJsrjJaJiPAeJoqXoHg8vCmzFXDvUQHel6Y,5
|
|
5
|
+
ipython_icat-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
icat
|