-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_sixel.py
More file actions
25 lines (22 loc) · 1.1 KB
/
Copy path_sixel.py
File metadata and controls
25 lines (22 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import sys
import tempfile
import matplotlib.pyplot as plt
from sixel.converter import SixelConverter
def render():
# sixel is a terminal image protocol (supported by VS Code's integrated
# terminal via "terminal.integrated.enableImages") — save the current
# figure to a temp PNG, then have SixelConverter encode and print it as
# escape codes instead of opening a separate plot window with plt.show()
#
# SixelWriter.draw() (the library's usual entry point) wraps the image in
# a cursor save (ESC 7) / restore (ESC 8) pair, which snaps the cursor
# back to its pre-image position afterward — so the next print() call
# overwrites the same rows and the image appears to flash and vanish.
# Calling SixelConverter directly skips that wrapping and leaves the
# cursor wherever the image output naturally puts it.
# delete=True (the default) removes the file on exit from this block,
# including if savefig/write raises
with tempfile.NamedTemporaryFile(suffix=".png") as f:
plt.savefig(f.name)
SixelConverter(f.name).write(sys.stdout)
plt.close()