<commit_before>import tornado
from tornado import gen
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
import json
import os
from traitlets.config import Application
from traitlets import Unicode, Integer, Type, Bool, List
from nbconvert.exporters import HTMLExporter

from nbserver.publisher import Publisher, FileSystemPublisher


class MainHandler(tornado.web.RequestHandler):

    def __init__(self, *args, **kwargs):
        self.publisher = kwargs.pop('publisher')
        super().__init__(*args, **kwargs)

    @gen.coroutine
    def get(self, filename):
        exporter = HTMLExporter()
        file_handle, mimetype = yield self.publisher.content_for_url_segment(filename)
        try:
            NbServer.instance().log.error(mimetype)
            if mimetype == 'application/x-ipynb+json':
                # FIXME: Steal from nbviewer how to do this non-blocking way!
                html, res = exporter.from_file(file_handle)
                self.write(html)
            else:
                return self.handle_static_file(file_handle, mimetype)
        except FileNotFoundError:
            # Note: This doesn't seem to catch errors from the static file handling,
            # since we're just directly returning a future that's unwrapped by tornado
            # Figure out how to deal with that properly!
            raise tornado.web.HTTPError(404)
        finally:
            # FIXME: Test and verify that this is actually closed properly
            file_handle.close()

    @classmethod
    def get_chunked_content(cls, file, start=None, end=None):
        # Stolen and adapted from StaticFileHandler
        if start is not None:<commit_msg>Handle 404 errors properly<commit_after>import tornado
from tornado import gen
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
import json
import os
from traitlets.config import Application
from traitlets import Unicode, Integer, Type, Bool, List
from nbconvert.exporters import HTMLExporter

from nbserver.publisher import Publisher, FileSystemPublisher


class MainHandler(tornado.web.RequestHandler):

    def __init__(self, *args, **kwargs):
        self.publisher = kwargs.pop('publisher')
        super().__init__(*args, **kwargs)

    @gen.coroutine
    def get(self, filename):
        file_handle = None
        try:
            file_handle, mimetype = yield self.publisher.content_for_url_segment(filename)
            if mimetype == 'application/x-ipynb+json':
                # FIXME: Steal from nbviewer how to do this non-blocking way!
                exporter = HTMLExporter()
                html, res = exporter.from_file(file_handle)
                self.write(html)
                self.finish()
            else:
                yield from self.handle_static_file(file_handle, mimetype)
                self.finish()
        except FileNotFoundError:
            # Note: This doesn't seem to catch errors from the static file handling,
            # since we're just directly returning a future that's unwrapped by tornado
            # Figure out how to deal with that properly!
            raise tornado.web.HTTPError(404)
        finally:
            if file_handle is not None:
                # FIXME: Test and verify that this is actually closed properly
                file_handle.close()

    @classmethod
    def get_chunked_content(cls, file, start=None, end=None):
        # Stolen and adapted from StaticFileHandler
        if start is not None:
{'fraction_non_alphanumeric': 0.07006920415224914, 'fraction_numerical': 0.0025951557093425604, 'mean_word_length': 2.594818652849741, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3776318', 'n_tokens_mistral': 950, 'n_tokens_neox': 895, 'n_words': 295}