Skip to content

Commit 34b975b

Browse files
committed
Initial commit
0 parents  commit 34b975b

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Joshua Brule
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# oscplotlib
2+

oscplotlib/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .osc1337 import show, show_all

oscplotlib/osc1337.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
3+
import base64
4+
import io
5+
import matplotlib.pyplot as plt
6+
7+
IMG_STRING = "\n\033]1337;File=inline=1;preserveAspectRatio=1:{}\a\n"
8+
9+
class OSC1337:
10+
"""
11+
Wrapper class for OSC 1337 images
12+
13+
OSC1337 objects display (__repr__) as images, but are implemented as
14+
a thin wrapper around a base64 encoded png.
15+
"""
16+
def __init__(self, b64str):
17+
self.b64str = b64str
18+
19+
def __repr__(self):
20+
return IMG_STRING.format(self.b64str)
21+
22+
def savefig(self, fname):
23+
"""Save the current OSC1337 object"""
24+
with open(fname, "wb") as f:
25+
f.write(base64.decodebytes(self.b64str.encode()))
26+
27+
28+
def show():
29+
"""
30+
Returns an OSC1337 object wrapping the render of figure or of the
31+
currently active figure, if figure is None.
32+
"""
33+
figure = plt.gcf()
34+
35+
buf = io.BytesIO()
36+
figure.savefig(buf, format='png')
37+
s = base64.b64encode(buf.getvalue()).decode()
38+
buf.close()
39+
40+
return OSC1337(s)
41+
42+
43+
def show_all():
44+
"""
45+
Return a list of all active figures as OSC1337 objects
46+
"""
47+
old_fignum = plt.gcf().number
48+
49+
l = []
50+
for i in plt.get_fignums():
51+
plt.figure(i)
52+
l.append(show())
53+
54+
plt.figure(old_fignum)
55+
56+
return l
57+

0 commit comments

Comments
 (0)