Add menu entries for getting converted views of a notebook

This commit is contained in:
Thomas Kluyver 2013-12-06 15:26:59 -08:00
parent a6424052f0
commit a3c2f1b5f9
4 changed files with 48 additions and 22 deletions

View File

@ -28,6 +28,10 @@ class NbconvertFileHandler(IPythonHandler):
info = os.stat(os_path)
self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
if self.get_argument('download', 'false').lower() == 'true':
filename = os.path.splitext(name)[0] + '.' + exporter.file_extension
self.set_header('Content-Disposition',
'attachment; filename="%s"' % filename)
output, resources = exporter.from_filename(os_path)

View File

@ -16,16 +16,17 @@ class NbconvertAPI(object):
def __init__(self, base_url):
self.base_url = base_url
def _req(self, verb, path, body=None):
def _req(self, verb, path, body=None, params=None):
response = requests.request(verb,
url_path_join(self.base_url, 'nbconvert', path),
data=body,
data=body, params=params,
)
response.raise_for_status()
return response
def from_file(self, format, path, name):
return self._req('GET', url_path_join(format, path, name))
def from_file(self, format, path, name, download=False):
return self._req('GET', url_path_join(format, path, name),
params={'download':download})
def from_post(self, format, nbmodel):
body = json.dumps(nbmodel)
@ -70,6 +71,12 @@ class APITest(NotebookTestBase):
with assert_http_error(404):
self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
def test_from_file_download(self):
r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
content_disposition = r.headers['Content-Disposition']
assert 'attachment' in content_disposition
assert 'testnb.py' in content_disposition
def test_from_post(self):
nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
nbmodel = requests.get(nbmodel_url).json()

View File

@ -69,6 +69,26 @@ var IPython = (function (IPython) {
);
};
MenuBar.prototype._nbconvert = function (format, download) {
download = download || false;
var notebook_name = IPython.notebook.get_notebook_name();
if (IPython.notebook.dirty) {
IPython.notebook.save_notebook({async : false});
}
var url = utils.url_path_join(
this.baseProjectUrl(),
'nbconvert',
format,
this.notebookPath(),
notebook_name + '.ipynb'
) + "?download=" + download.toString();
if (download) {
window.location.assign(url);
} else {
window.open(url);
}
}
MenuBar.prototype.bind_events = function () {
// File
@ -102,24 +122,17 @@ var IPython = (function (IPython) {
window.location.assign(url);
});
/* FIXME: download-as-py doesn't work right now
* We will need nbconvert hooked up to get this back
this.element.find('#download_py').click(function () {
var notebook_name = IPython.notebook.get_notebook_name();
if (IPython.notebook.dirty) {
IPython.notebook.save_notebook({async : false});
}
var url = utils.url_path_join(
that.baseProjectUrl(),
'api/notebooks',
that.notebookPath(),
notebook_name + '.ipynb?format=py&download=True'
);
window.location.assign(url);
this.element.find('#print_preview').click(function () {
that._nbconvert('html', false);
});
this.element.find('#download_py').click(function () {
that._nbconvert('python', true);
});
this.element.find('#download_html').click(function () {
that._nbconvert('html', true);
});
*/
this.element.find('#rename_notebook').click(function () {
IPython.save_widget.rename_notebook();

View File

@ -77,10 +77,12 @@ class="notebook_app"
</ul>
</li>
<li class="divider"></li>
<li id="print_preview"><a href="#">Print Preview</a></li>
<li class="dropdown-submenu"><a href="#">Download as</a>
<ul class="dropdown-menu">
<li id="download_ipynb"><a href="#">IPython Notebook (.ipynb)</a></li>
<!-- <li id="download_py"><a href="#">Python (.py)</a></li> -->
<li id="download_py"><a href="#">Python (.py)</a></li>
<li id="download_html"><a href="#">HTML (.html)</a></li>
</ul>
</li>
<li class="divider"></li>