-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the backend work inside a Docker container #11
Open
ayorgo
wants to merge
1
commit into
jktr:master
Choose a base branch
from
ayorgo:docker
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
# SPDX-License-Identifier: CC0-1.0 | ||
|
||
import array | ||
import fcntl | ||
import os | ||
import re | ||
import sys | ||
|
||
from io import BytesIO | ||
import termios | ||
import tty | ||
from base64 import standard_b64encode | ||
from contextlib import suppress | ||
from subprocess import run | ||
|
||
from matplotlib import interactive, is_interactive | ||
|
@@ -16,6 +23,68 @@ | |
if hasattr(sys, 'ps1') or sys.flags.interactive: | ||
interactive(True) | ||
|
||
def term_size_px(): | ||
width_px = height_px = 0 | ||
|
||
# try to get terminal size from ioctl | ||
with suppress(OSError): | ||
buf = array.array('H', [0, 0, 0, 0]) | ||
fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf) | ||
_, _, width_px, height_px = buf | ||
|
||
if width_px != 0 and height_px != 0: | ||
return height_px, width_px | ||
|
||
# fallback to ANSI escape code if ioctl fails | ||
buf = '' | ||
stdin = sys.stdin.fileno() | ||
tattr = termios.tcgetattr(stdin) | ||
|
||
try: | ||
tty.setcbreak(stdin, termios.TCSANOW) | ||
sys.stdout.write('\x1b[14t') | ||
sys.stdout.flush() | ||
|
||
while True: | ||
buf += sys.stdin.read(1) | ||
if buf[-1] == 't': | ||
break | ||
|
||
finally: | ||
termios.tcsetattr(stdin, termios.TCSANOW, tattr) | ||
|
||
# reading the actual values, but what if a keystroke appears while reading | ||
# from stdin? As dirty work around, getpos() returns if this fails: None | ||
try: | ||
matches = re.match(r'^\x1b\[4;(\d*);(\d*)t', buf) | ||
groups = matches.groups() | ||
except AttributeError: | ||
return None | ||
|
||
return (int(groups[0]), int(groups[1])) | ||
|
||
|
||
def serialize_gr_command(**cmd): | ||
payload = cmd.pop('payload', None) | ||
cmd = ','.join(f'{k}={v}' for k, v in cmd.items()) | ||
ans = [] | ||
w = ans.append | ||
w(b'\033_G'), w(cmd.encode('ascii')) | ||
if payload: | ||
w(b';') | ||
w(payload) | ||
w(b'\033\\') | ||
return b''.join(ans) | ||
|
||
|
||
def write_chunked(**cmd): | ||
data = standard_b64encode(cmd.pop('data')) | ||
while data: | ||
chunk, data = data[:4096], data[4096:] | ||
m = 1 if data else 0 | ||
sys.stdout.buffer.write(serialize_gr_command(payload=chunk, m=m, **cmd)) | ||
sys.stdout.flush() | ||
cmd.clear() | ||
|
||
class FigureManagerICat(FigureManagerBase): | ||
|
||
|
@@ -31,29 +100,19 @@ def f(*args, output=True, **kwargs): | |
return f | ||
|
||
def show(self): | ||
|
||
icat = __class__._run('kitty', '+kitten', 'icat') | ||
|
||
if os.environ.get('MPLBACKEND_KITTY_SIZING', 'automatic') != 'manual': | ||
|
||
tput = __class__._run('tput') | ||
|
||
# gather terminal dimensions | ||
rows = int(tput('lines')) | ||
px = icat('--print-window-size') | ||
px = list(map(int, px.split('x'))) | ||
|
||
# account for post-display prompt scrolling | ||
# 3 line shift for [\n, <matplotlib.axes…, >>>] after the figure | ||
px[1] -= int(3*(px[1]/rows)) | ||
term_height_px, term_width_px = term_size_px() | ||
|
||
# resize figure to terminal size & aspect ratio | ||
dpi = self.canvas.figure.dpi | ||
self.canvas.figure.set_size_inches((px[0] / dpi, px[1] / dpi)) | ||
ipd = 1 / self.canvas.figure.dpi | ||
term_width_inch, term_height_inch = term_width_px * ipd, term_height_px * ipd | ||
self.canvas.figure.set_size_inches(term_width_inch, term_height_inch) | ||
|
||
with BytesIO() as buf: | ||
self.canvas.figure.savefig(buf, format='png') | ||
icat('--align', 'left', output=False, input=buf.getbuffer()) | ||
write_chunked(a='T', f=100, data=buf.getvalue()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was getting the following error with
Reproducible example: import io
with io.BytesIO() as buf:
buf.write(b'hello')
res = buf.getbuffer() |
||
|
||
|
||
class FigureCanvasICat(FigureCanvasAgg): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't really see the point of the post-display prompt scrolling. Can change it back if the maintainers insist.