##// END OF EJS Templates
move IPython.html to jupyter_notebook
move IPython.html to jupyter_notebook

File last commit:

r21208:e65db1aa
r21208:e65db1aa
Show More
handlers.py
54 lines | 1.6 KiB | text/x-python | PythonLexer
MinRK
finish up FilesHandler...
r18363 """Serve files directly from the ContentsManager."""
Manuel Riel
add new FilesHandler class
r18139
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import mimetypes
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 import json
Manuel Riel
make base64 decoding ipython3 compatible
r18162 import base64
Manuel Riel
add new FilesHandler class
r18139 from tornado import web
from IPython.html.base.handlers import IPythonHandler
class FilesHandler(IPythonHandler):
"""serve files via ContentsManager"""
@web.authenticated
def get(self, path):
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 cm = self.contents_manager
Manuel Riel
remove redundant abs_path
r18168 if cm.is_hidden(path):
Manuel Riel
add new FilesHandler class
r18139 self.log.info("Refusing to serve hidden file, via 404 Error")
raise web.HTTPError(404)
MinRK
Remove separate 'path', 'name' in Contents API...
r18749
path = path.strip('/')
if '/' in path:
_, name = path.rsplit('/', 1)
else:
name = path
Min RK
Get /files/ unmodified from contents...
r20666 model = cm.get(path, type='file')
Min RK
use ?download=1 to trigger download in /files/...
r18556
if self.get_argument("download", False):
self.set_header('Content-Disposition','attachment; filename="%s"' % name)
Min RK
get mime from filename...
r20694 # get mimetype from filename
if name.endswith('.ipynb'):
Manuel Riel
add new FilesHandler class
r18139 self.set_header('Content-Type', 'application/json')
else:
cur_mime = mimetypes.guess_type(name)[0]
if cur_mime is not None:
self.set_header('Content-Type', cur_mime)
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 if model['format'] == 'base64':
Manuel Riel
make base64 decoding ipython3 compatible
r18162 b64_bytes = model['content'].encode('ascii')
self.write(base64.decodestring(b64_bytes))
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 elif model['format'] == 'json':
self.write(json.dumps(model['content']))
else:
self.write(model['content'])
Manuel Riel
add new FilesHandler class
r18139 self.flush()
MinRK
finish up FilesHandler...
r18363 default_handlers = [
(r"/files/(.*)", FilesHandler),
]