##// END OF EJS Templates
add RichIPythonWidget._append_latex...
Min RK -
Show More
@@ -95,18 +95,17 b' class RichIPythonWidget(IPythonWidget):'
95 # 'BaseFrontendMixin' abstract interface
95 # 'BaseFrontendMixin' abstract interface
96 #---------------------------------------------------------------------------
96 #---------------------------------------------------------------------------
97 def _pre_image_append(self, msg, prompt_number):
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 Shared code for some the following if statement
100 Shared code for some the following if statement
101 """
101 """
102 self.log.debug("execute_result: %s", msg.get('content', ''))
103 self._append_plain_text(self.output_sep, True)
102 self._append_plain_text(self.output_sep, True)
104 self._append_html(self._make_out_prompt(prompt_number), True)
103 self._append_html(self._make_out_prompt(prompt_number), True)
105 self._append_plain_text('\n', True)
104 self._append_plain_text('\n', True)
106
105
107 def _handle_execute_result(self, msg):
106 def _handle_execute_result(self, msg):
108 """ Overridden to handle rich data types, like SVG.
107 """Overridden to handle rich data types, like SVG."""
109 """
108 self.log.debug("execute_result: %s", msg.get('content', ''))
110 if self.include_output(msg):
109 if self.include_output(msg):
111 self.flush_clearoutput()
110 self.flush_clearoutput()
112 content = msg['content']
111 content = msg['content']
@@ -129,24 +128,15 b' class RichIPythonWidget(IPythonWidget):'
129 self._append_html(self.output_sep2, True)
128 self._append_html(self.output_sep2, True)
130 elif 'text/latex' in data:
129 elif 'text/latex' in data:
131 self._pre_image_append(msg, prompt_number)
130 self._pre_image_append(msg, prompt_number)
132 try:
131 self._append_latex(data['text/latex'], True)
133 png = latex_to_png(data['text/latex'], wrap=False)
132 self._append_html(self.output_sep2, True)
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)
143 else:
133 else:
144 # Default back to the plain text representation.
134 # Default back to the plain text representation.
145 return super(RichIPythonWidget, self)._handle_execute_result(msg)
135 return super(RichIPythonWidget, self)._handle_execute_result(msg)
146
136
147 def _handle_display_data(self, msg):
137 def _handle_display_data(self, msg):
148 """ Overridden to handle rich data types, like SVG.
138 """Overridden to handle rich data types, like SVG."""
149 """
139 self.log.debug("display_data: %s", msg.get('content', ''))
150 if self.include_output(msg):
140 if self.include_output(msg):
151 self.flush_clearoutput()
141 self.flush_clearoutput()
152 data = msg['content']['data']
142 data = msg['content']['data']
@@ -166,16 +156,7 b' class RichIPythonWidget(IPythonWidget):'
166 jpg = decodestring(data['image/jpeg'].encode('ascii'))
156 jpg = decodestring(data['image/jpeg'].encode('ascii'))
167 self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
157 self._append_jpg(jpg, True, metadata=metadata.get('image/jpeg', None))
168 elif 'text/latex' in data:
158 elif 'text/latex' in data:
169 try:
159 self._append_latex(data['text/latex'], True)
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)
179 else:
160 else:
180 # Default back to the plain text representation.
161 # Default back to the plain text representation.
181 return super(RichIPythonWidget, self)._handle_display_data(msg)
162 return super(RichIPythonWidget, self)._handle_display_data(msg)
@@ -184,6 +165,18 b' class RichIPythonWidget(IPythonWidget):'
184 # 'RichIPythonWidget' protected interface
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 def _append_jpg(self, jpg, before_prompt=False, metadata=None):
180 def _append_jpg(self, jpg, before_prompt=False, metadata=None):
188 """ Append raw JPG data to the widget."""
181 """ Append raw JPG data to the widget."""
189 self._append_custom(self._insert_jpg, jpg, before_prompt, metadata=metadata)
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