PY-29311 Make SciView plots interactive

1. Add image representation as html (mpld3 lib)
2. Add html preview editor for interactive plots
3. Add a suggestion "Install mpld3"
4. Add "Save as html" action
5. Add tests


Merge-request: IJ-MR-114428
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: dab7cc1dac74ec5e90d4bcd1a88fd6aadf06e557
This commit is contained in:
Egor.Eliseev
2023-10-19 12:03:04 +00:00
committed by intellij-monorepo-bot
parent ecaefdbfd2
commit 8e2f73f9f1
3 changed files with 45 additions and 3 deletions

View File

@@ -7,10 +7,22 @@ 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
from mpl_toolkits.mplot3d import Axes3D
from datalore.display import debug, display, SHOW_DEBUG_INFO
PY3 = sys.version_info[0] >= 3
IS_INTERACTIVE_PLOT = False
DEFAULT_FIGURE_WIDTH = 6.08
DEFAULT_FIGURE_HEIGHT = 4.56
STRING_3D = '3D'
if int(os.getenv("PYCHARM_INTERACTIVE_PLOTS", 0)):
try:
import mpld3
IS_INTERACTIVE_PLOT = True
except:
pass
index = int(os.getenv("PYCHARM_MATPLOTLIB_INDEX", 0))
@@ -77,6 +89,26 @@ class FigureCanvasInterAgg(FigureCanvasAgg):
debug("Error: Buffer FigureCanvasAgg.tostring_rgb() is empty")
return
html_string = ""
for elem in self.figure.axes:
if isinstance(elem, Axes3D):
html_string = STRING_3D
break
# mpld3 doesn't support 3D plots
if IS_INTERACTIVE_PLOT and not html_string:
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
self.figure.set_figwidth(DEFAULT_FIGURE_WIDTH)
self.figure.set_figheight(DEFAULT_FIGURE_HEIGHT)
try:
html_string = mpld3.fig_to_html(self.figure)
except:
pass
self.figure.set_figwidth(w)
self.figure.set_figheight(h)
render = self.get_renderer()
width = int(render.width)
debug("Image width: %d" % width)
@@ -88,7 +120,7 @@ class FigureCanvasInterAgg(FigureCanvasAgg):
else:
debug("Using non-interactive mode (Run without Python Console)")
plot_index = index if is_interactive else -1
display(DisplayDataObject(plot_index, width, buffer))
display(DisplayDataObject(plot_index, width, buffer, html_string))
def draw(self):
FigureCanvasAgg.draw(self)
@@ -114,10 +146,11 @@ class FigureManagerInterAgg(FigureManagerBase):
class DisplayDataObject:
def __init__(self, plot_index, width, image_bytes):
def __init__(self, plot_index, width, image_bytes, html_string=""):
self.plot_index = plot_index
self.image_width = width
self.image_bytes = image_bytes
self.html_string = html_string
def _repr_display_(self):
image_bytes_base64 = base64.b64encode(self.image_bytes)
@@ -126,7 +159,8 @@ class DisplayDataObject:
body = {
'plot_index': self.plot_index,
'image_width': self.image_width,
'image_base64': image_bytes_base64
'image_base64': image_bytes_base64,
'html_string': self.html_string
}
return ('pycharm-plot-image', body)

View File

@@ -56,4 +56,7 @@ public interface PydevConsoleRunner {
@TestOnly
void setSdk(Sdk sdk);
@TestOnly
@Nullable Sdk getSdk();
}

View File

@@ -1215,6 +1215,11 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner {
mySdk = sdk;
}
@Override
public @Nullable Sdk getSdk() {
return mySdk;
}
private class ConnectDebuggerAction extends ToggleAction implements DumbAware {
private boolean mySelected = false;
private XDebugSession mySession = null;