Skip to content

Commit

Permalink
Merge pull request #705 from parente/localfile-nav
Browse files Browse the repository at this point in the history
localfile navigation, naming improvements
  • Loading branch information
parente authored Jul 5, 2017
2 parents 5a85442 + 2fa55a4 commit 5ac2643
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
6 changes: 4 additions & 2 deletions nbviewer/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,10 @@ def filter_formats(self, nb, raw):
app_log.info("failed to test %s: %s", self.request.uri, name)

@gen.coroutine
def finish_notebook(self, json_notebook, download_url, provider_url=None,
def finish_notebook(self, json_notebook, download_url, provider_url=None,
provider_icon=None, provider_label=None, msg=None,
breadcrumbs=None, public=False, format=None, request=None):
breadcrumbs=None, public=False, format=None, request=None,
title=None):
"""render a notebook from its JSON body.
download_url is required, provider_url is not.
Expand Down Expand Up @@ -663,6 +664,7 @@ def finish_notebook(self, json_notebook, download_url, provider_url=None,
format_base=self.request.uri.replace(self.format_prefix, "").replace(self.base_url, '/'),
date=datetime.utcnow().strftime(date_fmt),
breadcrumbs=breadcrumbs,
title=title,
**config)
html_time.stop()

Expand Down
47 changes: 34 additions & 13 deletions nbviewer/providers/local/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,36 @@ class LocalFileHandler(RenderingHandler):
"""
# cache key is full uri to avoid mixing download vs view paths
_cache_key_attr = 'uri'
# provider root path
_localfile_path = '/localfile'

@property
def localfile_path(self):
return os.path.abspath(self.settings.get('localfile_path', ''))

def breadcrumbs(self, path):
"""Build a list of breadcrumbs leading up to and including the
given local path.
Parameters
----------
path: str
Relative path up to and including the leaf directory or file to include
in the breadcrumbs list
Returns
-------
list
Breadcrumbs suitable for the link_breadcrumbs() jinja macro
"""
provider_path = '/localfile'
breadcrumbs = [{
'url': url_path_join(self.base_url, self._localfile_path),
'name': 'home'
}]
breadcrumbs.extend(super(LocalFileHandler, self).breadcrumbs(path, self._localfile_path))
return breadcrumbs

@gen.coroutine
def download(self, abspath):
"""Download the file at the given absolute path.
Expand All @@ -49,8 +74,9 @@ def download(self, abspath):
st = os.stat(abspath)

self.set_header('Content-Length', st.st_size)
# Escape commas to workaround Chrome issue with commas in download filenames
self.set_header('Content-Disposition',
'attachment; filename={};'.format(filename))
'attachment; filename={};'.format(filename.replace(',', '_')))

content = web.StaticFileHandler.get_content(abspath)
if isinstance(content, bytes):
Expand Down Expand Up @@ -114,7 +140,9 @@ def get(self, path):
msg="file from localfile: %s" % path,
public=False,
format=self.format,
request=self.request)
request=self.request,
breadcrumbs=self.breadcrumbs(path),
title=os.path.basename(path))

def show_dir(self, abspath, path):
"""Render the directory view template for a given filesystem path.
Expand All @@ -131,14 +159,6 @@ def show_dir(self, abspath, path):
str
Rendered HTML
"""
base_url = '/localfile'

breadcrumbs = [{
'url': url_path_join(self.base_url, base_url),
'name': 'home'
}]
breadcrumbs.extend(self.breadcrumbs(path, base_url))

entries = []
dirs = []
ipynbs = []
Expand All @@ -165,7 +185,7 @@ def show_dir(self, abspath, path):
st = os.stat(absf)
dt = datetime.utcfromtimestamp(st.st_mtime)
entry['modtime'] = dt.isoformat()
entry['url'] = url_path_join(base_url, path, f)
entry['url'] = url_path_join(self._localfile_path, path, f)
entry['class'] = 'fa fa-folder-open'
dirs.append(entry)
elif f.endswith('.ipynb'):
Expand All @@ -175,7 +195,7 @@ def show_dir(self, abspath, path):
st = os.stat(absf)
dt = datetime.utcfromtimestamp(st.st_mtime)
entry['modtime'] = dt.isoformat()
entry['url'] = url_path_join(base_url, path, f)
entry['url'] = url_path_join(self._localfile_path, path, f)
entry['class'] = 'fa fa-book'
ipynbs.append(entry)

Expand All @@ -187,5 +207,6 @@ def show_dir(self, abspath, path):

html = self.render_template('dirview.html',
entries=entries,
breadcrumbs=breadcrumbs)
breadcrumbs=self.breadcrumbs(path),
title=url_path_join(path, '/'))
return html
5 changes: 4 additions & 1 deletion nbviewer/providers/url/tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
import unittest

import requests

Expand All @@ -13,7 +14,9 @@ class URLTestCase(NBViewerTestCase):
def test_url(self):
url = self.url('url/jdj.mit.edu/~stevenj/IJulia Preview.ipynb')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
# Base class overrides assertIn to do unicode in unicode checking
# We want to use the original unittest implementation
unittest.TestCase.assertIn(self, r.status_code, (200, 202))
self.assertIn('Download Notebook', r.text)


Expand Down
2 changes: 1 addition & 1 deletion nbviewer/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<head>
<meta charset="utf-8">
<title>{{title|default("Jupyter Notebook Viewer")}}</title>
<title>{{title|default("Jupyter Notebook Viewer", true)}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="{{ description | default('') }}">
<meta name="author" content="{{ author | default('') }}">
Expand Down

0 comments on commit 5ac2643

Please sign in to comment.