##// END OF EJS Templates
Dictionary key/value typo
Dictionary key/value typo

File last commit:

r18791:d9a682cb
r18891:84022305
Show More
handlers.py
53 lines | 1.5 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
Thomas Kluyver
Rename get_model() to get()
r18791 model = cm.get(path)
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)
Manuel Riel
correctly handle base64 and json, improve bin-file test
r18161 if model['type'] == 'notebook':
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),
]