##// END OF EJS Templates
add RichIPythonWidget._append_latex...
Min RK -
Show More
@@ -95,18 +95,17 b' class RichIPythonWidget(IPythonWidget):'
95 95 # 'BaseFrontendMixin' abstract interface
96 96 #---------------------------------------------------------------------------
97 97 def _pre_image_append(self, msg, prompt_number):
98 """ Append the Out[] prompt and make the output nicer
98 """Append the Out[] prompt and make the output nicer
99 99
100 100 Shared code for some the following if statement
101 101 """
102 self.log.debug("execute_result: %s", msg.get('content', ''))
103 102 self._append_plain_text(self.output_sep, True)
104 103 self._append_html(self._make_out_prompt(prompt_number), True)
105 104 self._append_plain_text('\n', True)
106 105
107 106 def _handle_execute_result(self, msg):
108 """ Overridden to handle rich data types, like SVG.
109 """
107 """Overridden to handle rich data types, like SVG."""
108 self.log.debug("execute_result: %s", msg.get('content', ''))
110 109 if self.include_output(msg):
111 110 self.flush_clearoutput()
112 111 content = msg['content']
@@ -129,24 +128,15 b' class RichIPythonWidget(IPythonWidget):'
129 128 self._append_html(self.output_sep2, True)
130 129 elif 'text/latex' in data:
131 130 self._pre_image_append(msg, prompt_number)
132 try:
133 png = latex_to_png(data['text/latex'], wrap=False)
134 except Exception:
135 self.log.error("Failed to render latex: %r", data['text/latex'], exc_info=True)
136 png = None
137 if png is not None:
138 self._append_png(png, True)
139 self._append_html(self.output_sep2, True)
140 else:
141 # Print plain text if png can't be generated
142 return super(RichIPythonWidget, self)._handle_execute_result(msg)
131 self._append_latex(data['text/latex'], True)
132 self._append_html(self.output_sep2, True)
143 133 else:
144 134 # Default back to the plain text representation.
145 135 return super(RichIPythonWidget, self)._handle_execute_result(msg)
146 136
147 137 def _handle_display_data(self, msg):
148 """ Overridden to handle rich data types, like SVG.
149 """
138 """Overridden to handle rich data types, like SVG."""
139 self.log.debug("display_data: %s", msg.get('content', ''))
150 140 if self.include_output(msg):
151 141 self.flush_clearoutput()
152 142 data = msg['content']['data']
@@ -166,16 +156,7 b' class RichIPythonWidget(IPythonWidget):'
166 156 jpg = decodestring(data['image/jpeg'].encode('ascii'))
167 157 self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
168 158 elif 'text/latex' in data:
169 try:
170 png = latex_to_png(data['text/latex'], wrap=False)
171 except Exception:
172 self.log.error("Failed to render latex: %r", data['text/latex'], exc_info=True)
173 png = None
174 if png is not None:
175 self._append_png(png, True)
176 else:
177 # Print plain text if png can't be generated
178 return super(RichIPythonWidget, self)._handle_display_data(msg)
159 self._append_latex(data['text/latex'], True)
179 160 else:
180 161 # Default back to the plain text representation.
181 162 return super(RichIPythonWidget, self)._handle_display_data(msg)
@@ -184,6 +165,18 b' class RichIPythonWidget(IPythonWidget):'
184 165 # 'RichIPythonWidget' protected interface
185 166 #---------------------------------------------------------------------------
186 167
168 def _append_latex(self, latex, before_prompt=False, metadata=None):
169 """ Append latex data to the widget."""
170 try:
171 png = latex_to_png(latex, wrap=False)
172 except Exception as e:
173 self.log.error("Failed to render latex: '%s'", latex, exc_info=True)
174 self._append_plain_text("Failed to render latex:\n%s\nError was: %s" % (
175 latex, e,
176 ), before_prompt)
177 else:
178 self._append_png(png, before_prompt, metadata)
179
187 180 def _append_jpg(self, jpg, before_prompt=False, metadata=None):
188 181 """ Append raw JPG data to the widget."""
189 182 self._append_custom(self._insert_jpg, jpg, before_prompt, metadata=metadata)
General Comments 0
You need to be logged in to leave comments. Login now