mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
initial support for matplotlib toolwindow
Plots toolwindow is implemented as part of SciView toolwindow. DataView merged into SciView. Features: - open toolwindow with plot on plot available (run/console/rebug) - plots in toolwindow are in separate tabs with image editor inside - tabs in toolwindow are closeable - save plot as file action - DnD to/from editor supported - plots toolwindow could be disabled from Settings
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import matplotlib
|
||||
import os
|
||||
import socket
|
||||
import struct
|
||||
from matplotlib._pylab_helpers import Gcf
|
||||
from matplotlib.backend_bases import FigureManagerBase, ShowBase
|
||||
from matplotlib.backends.backend_agg import FigureCanvasAgg
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
HOST = 'localhost'
|
||||
PORT = os.getenv("PYCHARM_MATPLOTLIB_PORT")
|
||||
PORT = int(PORT) if PORT is not None else None
|
||||
PORT = PORT if PORT != -1 else None
|
||||
|
||||
rcParams = matplotlib.rcParams
|
||||
verbose = matplotlib.verbose
|
||||
|
||||
|
||||
class Show(ShowBase):
|
||||
def __call__(self, **kwargs):
|
||||
managers = Gcf.get_all_fig_managers()
|
||||
if not managers:
|
||||
return
|
||||
|
||||
for manager in managers:
|
||||
manager.show(**kwargs)
|
||||
|
||||
def mainloop(self):
|
||||
pass
|
||||
|
||||
|
||||
show = Show()
|
||||
|
||||
|
||||
# from pyplot API
|
||||
def new_figure_manager(num, *args, **kwargs):
|
||||
FigureClass = kwargs.pop('FigureClass', Figure)
|
||||
figure = FigureClass(*args, **kwargs)
|
||||
return new_figure_manager_given_figure(num, figure)
|
||||
|
||||
|
||||
# from pyplot API
|
||||
def new_figure_manager_given_figure(num, figure):
|
||||
canvas = FigureCanvasInterAgg(figure)
|
||||
manager = FigureManagerInterAgg(canvas, num)
|
||||
if matplotlib.is_interactive():
|
||||
manager.show()
|
||||
return manager
|
||||
|
||||
|
||||
# from pyplot API
|
||||
class FigureCanvasInterAgg(FigureCanvasAgg):
|
||||
def __init__(self, figure):
|
||||
FigureCanvasAgg.__init__(self, figure)
|
||||
|
||||
def show(self):
|
||||
FigureCanvasAgg.draw(self)
|
||||
if PORT is None:
|
||||
return
|
||||
|
||||
if matplotlib.__version__ < '1.2':
|
||||
buffer = self.tostring_rgb(0, 0)
|
||||
else:
|
||||
buffer = self.tostring_rgb()
|
||||
|
||||
render = self.get_renderer()
|
||||
width, height = int(render.width), int(render.height) # pass to the socket
|
||||
|
||||
try:
|
||||
sock = socket.socket()
|
||||
sock.connect((HOST, PORT))
|
||||
sock.send(struct.pack('>i', width))
|
||||
sock.send(buffer)
|
||||
except ConnectionRefusedError as _:
|
||||
# nothing bad. It just means, that our tool window doesn't run yet
|
||||
pass
|
||||
|
||||
|
||||
class FigureManagerInterAgg(FigureManagerBase):
|
||||
def __init__(self, canvas, num):
|
||||
FigureManagerBase.__init__(self, canvas, num)
|
||||
self.canvas = canvas
|
||||
self._num = num
|
||||
self._shown = False
|
||||
|
||||
def show(self, **kwargs):
|
||||
self.canvas.show()
|
||||
Gcf.destroy(self._num)
|
||||
Reference in New Issue
Block a user