Repository: ipython/ipython

Issue:
Uninformative error when saving notebook to HTML w/o pygments
If you try to save your Notebook to HTML (or do "print preview") and pygments is not installed, you get a very unhelpful 500 error page.

When I looked at stdout/err from notebook I see:

```
ERROR:tornado.access:500 GET /nbconvert/html/XXX.ipynb?download=false (XXX) 3.13ms referer=XXX
WARNING:tornado.general:500 GET /nbconvert/html/XXX?download=false (XXX): Could not construct Exporter: No module named pygments.formatters
ERROR:tornado.access:{
  "Accept-Language": "en-US,en;q=0.8", 
  "Accept-Encoding": "gzip,deflate,sdch", 
  "Connection": "keep-alive", 
  "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
  "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36", 
  "Host": "XXXX:8888", 
  "Referer": "XXXX.ipynb", 
  "Cache-Control": "max-age=0"
}
```

The solution was to install `pygments`, but it would be nice if the HTML page said that rather than a 500 error.



Discussion:
Are you seeing a page that looks like this?

![screen shot 2014-08-14 at 17 56 10](https://cloud.githubusercontent.com/assets/151929/3929044/1ae0cbcc-2417-11e4-8f9c-09675a7a5774.PNG)

No. I see:

```
<html><title>500: Internal Server Error</title><body>500: Internal Server Error</body></html>
```

which version of IPython ?

2.2.0

On Fri, Aug 15, 2014 at 1:11 PM, Matthias Bussonnier <
notifications@github.com> wrote:

> which version of IPython ?
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/ipython/ipython/issues/6312#issuecomment-52332319.

Could the version of tornado, etc matter?

> Could the version of tornado, etc matter?

No. But I guess the screenshot show y @minrk is the one of 3.0.0-dev, so I guess this could be considered as fixed now.

What the timeline for the 3.0 release?

Fall-ish winter-ish. but you are welcome to try master whenever you want.

I might do that, I was just curious. Is there actually going to be a 2.3 release?

Yes, we're expecting to make a 2.3 release, because the notebook format will change in 3.0, and we want the 2.x series to be able to read the new notebooks.

What is the difference is notebook format?
https://github.com/ipython/ipython/pull/6045 ?

On Fri, Aug 15, 2014 at 1:32 PM, Thomas Kluyver notifications@github.com
wrote:

> Yes, we're expecting to make a 2.3 release, because the notebook format
> will change in 3.0, and we want the 2.x series to be able to read the new
> notebooks.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/ipython/ipython/issues/6312#issuecomment-52334685.

It will be a roll up of various changes, like getting rid of 'worksheets' (which we don't use, but remain in the format because at one point we thought we might). See #6045 .

It looks like 2.2 should have the same error page behavior as master. What version of tornado do you have?

4.0.1

On Fri, Aug 15, 2014 at 2:36 PM, Min RK notifications@github.com wrote:

> It looks like 2.2 should have the same error page behavior. What version
> of tornado do you have?
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/ipython/ipython/issues/6312#issuecomment-52342151.

Intriguing. I'll try to figure out how to reproduce what you are seeing.

FWIW, the exceptions are slightly different too:

```
No module named pygments.formatters
```

vs

```
No module named pygments
```

Wait, so you _do_ see the exception in the HTML page, or is that from the server log? The difference isn't a big deal, it's probably just a change in the imports in nbconvert.

What is `IPython.sys_info()`?

I do NOT see the exception in the HTML page. I just meant the exception you posted in your screen-capture is different from the one I see in the log.

```
{'commit_hash': 'd87a8cd',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/usr/local/lib/python2.7/dist-packages/IPython',
 'ipython_version': '2.2.0',
 'os_name': 'posix',
 'platform': 'Linux-3.11.0-15-generic-x86_64-with-Ubuntu-13.10-saucy',
 'sys_executable': '/usr/bin/python',
 'sys_platform': 'linux2',
 'sys_version': '2.7.5+ (default, Feb 27 2014, 19:37:08) \n[GCC 4.8.1]'}
```

Reproduced it. Something changed in the error rendering in tornado 4. I'm on the case!

Relevant bit is the last bullet in [the 4.0 backwards-compatibility notes](http://www.tornadoweb.org/en/stable/releases/v4.0.0.html#backwards-compatibility-notes).

I guess it was tornado after all :-)


Resolving patch:
diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py
--- a/IPython/html/base/handlers.py
+++ b/IPython/html/base/handlers.py
@@ -235,12 +235,13 @@ def get_json_body(self):
             raise web.HTTPError(400, u'Invalid JSON in body of request')
         return model
 
-    def get_error_html(self, status_code, **kwargs):
+    def write_error(self, status_code, **kwargs):
         """render custom error pages"""
-        exception = kwargs.get('exception')
+        exc_info = kwargs.get('exc_info')
         message = ''
         status_message = responses.get(status_code, 'Unknown HTTP Error')
-        if exception:
+        if exc_info:
+            exception = exc_info[1]
             # get the custom message, if defined
             try:
                 message = exception.log_message % exception.args
@@ -260,13 +261,16 @@ def get_error_html(self, status_code, **kwargs):
             exception=exception,
         )
         
+        self.set_header('Content-Type', 'text/html')
         # render the template
         try:
             html = self.render_template('%s.html' % status_code, **ns)
         except TemplateNotFound:
             self.log.debug("No template for %d", status_code)
             html = self.render_template('error.html', **ns)
-        return html
+        
+        self.write(html)
+        
 
 
 class Template404(IPythonHandler):


Test patch: