rich_jupyter_widget.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. # Copyright (c) IPython Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from base64 import b64decode
  4. import os
  5. import re
  6. from warnings import warn
  7. from qtpy import QtCore, QtGui, QtWidgets
  8. from ipython_genutils.path import ensure_dir_exists
  9. from traitlets import Bool
  10. from qtconsole.svg import save_svg, svg_to_clipboard, svg_to_image
  11. from .jupyter_widget import JupyterWidget
  12. try:
  13. from IPython.lib.latextools import latex_to_png
  14. except ImportError:
  15. latex_to_png = None
  16. class LatexError(Exception):
  17. """Exception for Latex errors"""
  18. class RichIPythonWidget(JupyterWidget):
  19. """Dummy class for config inheritance. Destroyed below."""
  20. class RichJupyterWidget(RichIPythonWidget):
  21. """ An JupyterWidget that supports rich text, including lists, images, and
  22. tables. Note that raw performance will be reduced compared to the plain
  23. text version.
  24. """
  25. # RichJupyterWidget protected class variables.
  26. _payload_source_plot = 'ipykernel.pylab.backend_payload.add_plot_payload'
  27. _jpg_supported = Bool(False)
  28. # Used to determine whether a given html export attempt has already
  29. # displayed a warning about being unable to convert a png to svg.
  30. _svg_warning_displayed = False
  31. #---------------------------------------------------------------------------
  32. # 'object' interface
  33. #---------------------------------------------------------------------------
  34. def __init__(self, *args, **kw):
  35. """ Create a RichJupyterWidget.
  36. """
  37. kw['kind'] = 'rich'
  38. super(RichJupyterWidget, self).__init__(*args, **kw)
  39. # Configure the ConsoleWidget HTML exporter for our formats.
  40. self._html_exporter.image_tag = self._get_image_tag
  41. # Dictionary for resolving document resource names to SVG data.
  42. self._name_to_svg_map = {}
  43. # Do we support jpg ?
  44. # it seems that sometime jpg support is a plugin of QT, so try to assume
  45. # it is not always supported.
  46. self._jpg_supported = 'jpeg' in QtGui.QImageReader.supportedImageFormats()
  47. #---------------------------------------------------------------------------
  48. # 'ConsoleWidget' public interface overides
  49. #---------------------------------------------------------------------------
  50. def export_html(self):
  51. """ Shows a dialog to export HTML/XML in various formats.
  52. Overridden in order to reset the _svg_warning_displayed flag prior
  53. to the export running.
  54. """
  55. self._svg_warning_displayed = False
  56. super(RichJupyterWidget, self).export_html()
  57. #---------------------------------------------------------------------------
  58. # 'ConsoleWidget' protected interface
  59. #---------------------------------------------------------------------------
  60. def _context_menu_make(self, pos):
  61. """ Reimplemented to return a custom context menu for images.
  62. """
  63. format = self._control.cursorForPosition(pos).charFormat()
  64. name = format.stringProperty(QtGui.QTextFormat.ImageName)
  65. if name:
  66. menu = QtWidgets.QMenu(self)
  67. menu.addAction('Copy Image', lambda: self._copy_image(name))
  68. menu.addAction('Save Image As...', lambda: self._save_image(name))
  69. menu.addSeparator()
  70. svg = self._name_to_svg_map.get(name, None)
  71. if svg is not None:
  72. menu.addSeparator()
  73. menu.addAction('Copy SVG', lambda: svg_to_clipboard(svg))
  74. menu.addAction('Save SVG As...',
  75. lambda: save_svg(svg, self._control))
  76. else:
  77. menu = super(RichJupyterWidget, self)._context_menu_make(pos)
  78. return menu
  79. #---------------------------------------------------------------------------
  80. # 'BaseFrontendMixin' abstract interface
  81. #---------------------------------------------------------------------------
  82. def _pre_image_append(self, msg, prompt_number):
  83. """Append the Out[] prompt and make the output nicer
  84. Shared code for some the following if statement
  85. """
  86. self._append_plain_text(self.output_sep, True)
  87. self._append_html(self._make_out_prompt(prompt_number), True)
  88. self._append_plain_text('\n', True)
  89. def _handle_execute_result(self, msg):
  90. """Overridden to handle rich data types, like SVG."""
  91. self.log.debug("execute_result: %s", msg.get('content', ''))
  92. if self.include_output(msg):
  93. self.flush_clearoutput()
  94. content = msg['content']
  95. prompt_number = content.get('execution_count', 0)
  96. data = content['data']
  97. metadata = msg['content']['metadata']
  98. if 'image/svg+xml' in data:
  99. self._pre_image_append(msg, prompt_number)
  100. self._append_svg(data['image/svg+xml'], True)
  101. self._append_html(self.output_sep2, True)
  102. elif 'image/png' in data:
  103. self._pre_image_append(msg, prompt_number)
  104. png = b64decode(data['image/png'].encode('ascii'))
  105. self._append_png(png, True, metadata=metadata.get('image/png',
  106. None))
  107. self._append_html(self.output_sep2, True)
  108. elif 'image/jpeg' in data and self._jpg_supported:
  109. self._pre_image_append(msg, prompt_number)
  110. jpg = b64decode(data['image/jpeg'].encode('ascii'))
  111. self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg',
  112. None))
  113. self._append_html(self.output_sep2, True)
  114. elif 'text/latex' in data:
  115. self._pre_image_append(msg, prompt_number)
  116. try:
  117. self._append_latex(data['text/latex'], True)
  118. except LatexError:
  119. return super(RichJupyterWidget, self)._handle_display_data(msg)
  120. self._append_html(self.output_sep2, True)
  121. else:
  122. # Default back to the plain text representation.
  123. return super(RichJupyterWidget, self)._handle_execute_result(msg)
  124. def _handle_display_data(self, msg):
  125. """Overridden to handle rich data types, like SVG."""
  126. self.log.debug("display_data: %s", msg.get('content', ''))
  127. if self.include_output(msg):
  128. self.flush_clearoutput()
  129. data = msg['content']['data']
  130. metadata = msg['content']['metadata']
  131. # Try to use the svg or html representations.
  132. # FIXME: Is this the right ordering of things to try?
  133. self.log.debug("display: %s", msg.get('content', ''))
  134. if 'image/svg+xml' in data:
  135. svg = data['image/svg+xml']
  136. self._append_svg(svg, True)
  137. elif 'image/png' in data:
  138. # PNG data is base64 encoded as it passes over the network
  139. # in a JSON structure so we decode it.
  140. png = b64decode(data['image/png'].encode('ascii'))
  141. self._append_png(png, True, metadata=metadata.get('image/png', None))
  142. elif 'image/jpeg' in data and self._jpg_supported:
  143. jpg = b64decode(data['image/jpeg'].encode('ascii'))
  144. self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
  145. elif 'text/latex' in data and latex_to_png:
  146. try:
  147. self._append_latex(data['text/latex'], True)
  148. except LatexError:
  149. return super(RichJupyterWidget, self)._handle_display_data(msg)
  150. else:
  151. # Default back to the plain text representation.
  152. return super(RichJupyterWidget, self)._handle_display_data(msg)
  153. #---------------------------------------------------------------------------
  154. # 'RichJupyterWidget' protected interface
  155. #---------------------------------------------------------------------------
  156. def _is_latex_math(self, latex):
  157. """
  158. Determine if a Latex string is in math mode
  159. This is the only mode supported by qtconsole
  160. """
  161. basic_envs = ['math', 'displaymath']
  162. starable_envs = ['equation', 'eqnarray' 'multline', 'gather', 'align',
  163. 'flalign', 'alignat']
  164. star_envs = [env + '*' for env in starable_envs]
  165. envs = basic_envs + starable_envs + star_envs
  166. env_syntax = [r'\begin{{{0}}} \end{{{0}}}'.format(env).split() for env in envs]
  167. math_syntax = [
  168. (r'\[', r'\]'), (r'\(', r'\)'),
  169. ('$$', '$$'), ('$', '$'),
  170. ]
  171. for start, end in math_syntax + env_syntax:
  172. inner = latex[len(start):-len(end)]
  173. if start in inner or end in inner:
  174. return False
  175. if latex.startswith(start) and latex.endswith(end):
  176. return True
  177. return False
  178. def _append_latex(self, latex, before_prompt=False, metadata=None):
  179. """ Append latex data to the widget."""
  180. png = None
  181. if self._is_latex_math(latex):
  182. png = latex_to_png(latex, wrap=False, backend='dvipng')
  183. # Matplotlib only supports strings enclosed in dollar signs
  184. if png is None and latex.startswith('$') and latex.endswith('$'):
  185. # To avoid long and ugly errors, like the one reported in
  186. # spyder-ide/spyder#7619
  187. try:
  188. png = latex_to_png(latex, wrap=False, backend='matplotlib')
  189. except Exception:
  190. pass
  191. if png:
  192. self._append_png(png, before_prompt, metadata)
  193. else:
  194. raise LatexError
  195. def _append_jpg(self, jpg, before_prompt=False, metadata=None):
  196. """ Append raw JPG data to the widget."""
  197. self._append_custom(self._insert_jpg, jpg, before_prompt, metadata=metadata)
  198. def _append_png(self, png, before_prompt=False, metadata=None):
  199. """ Append raw PNG data to the widget.
  200. """
  201. self._append_custom(self._insert_png, png, before_prompt, metadata=metadata)
  202. def _append_svg(self, svg, before_prompt=False):
  203. """ Append raw SVG data to the widget.
  204. """
  205. self._append_custom(self._insert_svg, svg, before_prompt)
  206. def _add_image(self, image):
  207. """ Adds the specified QImage to the document and returns a
  208. QTextImageFormat that references it.
  209. """
  210. document = self._control.document()
  211. name = str(image.cacheKey())
  212. document.addResource(QtGui.QTextDocument.ImageResource,
  213. QtCore.QUrl(name), image)
  214. format = QtGui.QTextImageFormat()
  215. format.setName(name)
  216. return format
  217. def _copy_image(self, name):
  218. """ Copies the ImageResource with 'name' to the clipboard.
  219. """
  220. image = self._get_image(name)
  221. QtWidgets.QApplication.clipboard().setImage(image)
  222. def _get_image(self, name):
  223. """ Returns the QImage stored as the ImageResource with 'name'.
  224. """
  225. document = self._control.document()
  226. image = document.resource(QtGui.QTextDocument.ImageResource,
  227. QtCore.QUrl(name))
  228. return image
  229. def _get_image_tag(self, match, path = None, format = "png"):
  230. """ Return (X)HTML mark-up for the image-tag given by match.
  231. Parameters
  232. ----------
  233. match : re.SRE_Match
  234. A match to an HTML image tag as exported by Qt, with
  235. match.group("Name") containing the matched image ID.
  236. path : string|None, optional [default None]
  237. If not None, specifies a path to which supporting files may be
  238. written (e.g., for linked images). If None, all images are to be
  239. included inline.
  240. format : "png"|"svg"|"jpg", optional [default "png"]
  241. Format for returned or referenced images.
  242. """
  243. if format in ("png","jpg"):
  244. try:
  245. image = self._get_image(match.group("name"))
  246. except KeyError:
  247. return "<b>Couldn't find image %s</b>" % match.group("name")
  248. if path is not None:
  249. ensure_dir_exists(path)
  250. relpath = os.path.basename(path)
  251. if image.save("%s/qt_img%s.%s" % (path, match.group("name"), format),
  252. "PNG"):
  253. return '<img src="%s/qt_img%s.%s">' % (relpath,
  254. match.group("name"),format)
  255. else:
  256. return "<b>Couldn't save image!</b>"
  257. else:
  258. ba = QtCore.QByteArray()
  259. buffer_ = QtCore.QBuffer(ba)
  260. buffer_.open(QtCore.QIODevice.WriteOnly)
  261. image.save(buffer_, format.upper())
  262. buffer_.close()
  263. return '<img src="data:image/%s;base64,\n%s\n" />' % (
  264. format,re.sub(r'(.{60})',r'\1\n', str(ba.toBase64().data().decode())))
  265. elif format == "svg":
  266. try:
  267. svg = str(self._name_to_svg_map[match.group("name")])
  268. except KeyError:
  269. if not self._svg_warning_displayed:
  270. QtWidgets.QMessageBox.warning(self, 'Error converting PNG to SVG.',
  271. 'Cannot convert PNG images to SVG, export with PNG figures instead. '
  272. 'If you want to export matplotlib figures as SVG, add '
  273. 'to your ipython config:\n\n'
  274. '\tc.InlineBackend.figure_format = \'svg\'\n\n'
  275. 'And regenerate the figures.',
  276. QtWidgets.QMessageBox.Ok)
  277. self._svg_warning_displayed = True
  278. return ("<b>Cannot convert PNG images to SVG.</b> "
  279. "You must export this session with PNG images. "
  280. "If you want to export matplotlib figures as SVG, add to your config "
  281. "<span>c.InlineBackend.figure_format = 'svg'</span> "
  282. "and regenerate the figures.")
  283. # Not currently checking path, because it's tricky to find a
  284. # cross-browser way to embed external SVG images (e.g., via
  285. # object or embed tags).
  286. # Chop stand-alone header from matplotlib SVG
  287. offset = svg.find("<svg")
  288. assert(offset > -1)
  289. return svg[offset:]
  290. else:
  291. return '<b>Unrecognized image format</b>'
  292. def _insert_jpg(self, cursor, jpg, metadata=None):
  293. """ Insert raw PNG data into the widget."""
  294. self._insert_img(cursor, jpg, 'jpg', metadata=metadata)
  295. def _insert_png(self, cursor, png, metadata=None):
  296. """ Insert raw PNG data into the widget.
  297. """
  298. self._insert_img(cursor, png, 'png', metadata=metadata)
  299. def _insert_img(self, cursor, img, fmt, metadata=None):
  300. """ insert a raw image, jpg or png """
  301. if metadata:
  302. width = metadata.get('width', None)
  303. height = metadata.get('height', None)
  304. else:
  305. width = height = None
  306. try:
  307. image = QtGui.QImage()
  308. image.loadFromData(img, fmt.upper())
  309. if width and height:
  310. image = image.scaled(width, height,
  311. QtCore.Qt.IgnoreAspectRatio,
  312. QtCore.Qt.SmoothTransformation)
  313. elif width and not height:
  314. image = image.scaledToWidth(width, QtCore.Qt.SmoothTransformation)
  315. elif height and not width:
  316. image = image.scaledToHeight(height, QtCore.Qt.SmoothTransformation)
  317. except ValueError:
  318. self._insert_plain_text(cursor, 'Received invalid %s data.'%fmt)
  319. else:
  320. format = self._add_image(image)
  321. cursor.insertBlock()
  322. cursor.insertImage(format)
  323. cursor.insertBlock()
  324. def _insert_svg(self, cursor, svg):
  325. """ Insert raw SVG data into the widet.
  326. """
  327. try:
  328. image = svg_to_image(svg)
  329. except ValueError:
  330. self._insert_plain_text(cursor, 'Received invalid SVG data.')
  331. else:
  332. format = self._add_image(image)
  333. self._name_to_svg_map[format.name()] = svg
  334. cursor.insertBlock()
  335. cursor.insertImage(format)
  336. cursor.insertBlock()
  337. def _save_image(self, name, format='PNG'):
  338. """ Shows a save dialog for the ImageResource with 'name'.
  339. """
  340. dialog = QtWidgets.QFileDialog(self._control, 'Save Image')
  341. dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
  342. dialog.setDefaultSuffix(format.lower())
  343. dialog.setNameFilter('%s file (*.%s)' % (format, format.lower()))
  344. if dialog.exec_():
  345. filename = dialog.selectedFiles()[0]
  346. image = self._get_image(name)
  347. image.save(filename, format)
  348. # Clobber RichIPythonWidget above:
  349. class RichIPythonWidget(RichJupyterWidget):
  350. """Deprecated class. Use RichJupyterWidget."""
  351. def __init__(self, *a, **kw):
  352. warn("RichIPythonWidget is deprecated, use RichJupyterWidget",
  353. DeprecationWarning)
  354. super(RichIPythonWidget, self).__init__(*a, **kw)