Merge pull request #6978 from takluyver/nbconvert-script

Nbconvert to script for any kernel language
This commit is contained in:
Min RK 2014-11-24 12:56:20 -08:00
commit 535bbee883
5 changed files with 49 additions and 14 deletions

View File

@ -43,7 +43,7 @@ def respond_zip(handler, name, output, resources):
# Prepare the zip file
buffer = io.BytesIO()
zipf = zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED)
output_filename = os.path.splitext(name)[0] + '.' + resources['output_extension']
output_filename = os.path.splitext(name)[0] + resources['output_extension']
zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
for filename, data in output_files.items():
zipf.writestr(os.path.basename(filename), data)
@ -96,7 +96,7 @@ class NbconvertFileHandler(IPythonHandler):
# Force download if requested
if self.get_argument('download', 'false').lower() == 'true':
filename = os.path.splitext(name)[0] + '.' + resources['output_extension']
filename = os.path.splitext(name)[0] + resources['output_extension']
self.set_header('Content-Disposition',
'attachment; filename="%s"' % filename)

View File

@ -69,17 +69,21 @@ define([
MenuBar.prototype._nbconvert = function (format, download) {
download = download || false;
var notebook_path = this.notebook.notebook_path;
if (this.notebook.dirty) {
this.notebook.save_notebook({async : false});
}
var url = utils.url_join_encode(
this.base_url,
'nbconvert',
format,
notebook_path
) + "?download=" + download.toString();
window.open(url);
var w = window.open()
if (this.notebook.dirty) {
this.notebook.save_notebook().then(function() {
w.location = url;
});
} else {
w.location = url;
}
};
MenuBar.prototype.bind_events = function () {
@ -129,10 +133,6 @@ define([
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);
});
@ -308,6 +308,16 @@ define([
this.events.on('checkpoint_created.Notebook', function (event, data) {
that.update_restore_checkpoint(that.notebook.checkpoints);
});
this.events.on('notebook_loaded.Notebook', function() {
var langinfo = that.notebook.metadata.language_info || {};
that.update_nbconvert_script(langinfo);
});
this.events.on('kernel_ready.Kernel', function(event, data) {
var langinfo = data.kernel.info_reply.language_info || {};
that.update_nbconvert_script(langinfo);
});
};
MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
@ -340,6 +350,31 @@ define([
);
});
};
MenuBar.prototype.update_nbconvert_script = function(langinfo) {
// Set the 'Download as foo' menu option for the relevant language.
var el = this.element.find('#download_script');
var that = this;
// Set menu entry text to e.g. "Python (.py)"
var langname = (langinfo.name || 'Script')
langname = langname.charAt(0).toUpperCase()+langname.substr(1) // Capitalise
el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
// Unregister any previously registered handlers
el.off('click');
if (langinfo.nbconvert_exporter) {
// Metadata specifies a specific exporter, e.g. 'python'
el.click(function() {
that._nbconvert(langinfo.nbconvert_exporter, true);
});
} else {
// Use generic 'script' exporter
el.click(function() {
that._nbconvert('script', true);
});
}
};
// Backwards compatability.
IPython.MenuBar = MenuBar;

View File

@ -1944,7 +1944,7 @@ define([
var start = new Date().getTime();
var that = this;
this.contents.save(this.notebook_path, model).then(
return this.contents.save(this.notebook_path, model).then(
$.proxy(this.save_notebook_success, this, start),
function (error) {
that.events.trigger('notebook_save_failed.Notebook', error);

View File

@ -105,7 +105,7 @@ class="notebook_app"
<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_script"><a href="#">Script</a></li>
<li id="download_html"><a href="#">HTML (.html)</a></li>
<li id="download_rst"><a href="#">reST (.rst)</a></li>
<li id="download_pdf"><a href="#">PDF (.pdf)</a></li>

View File

@ -23,7 +23,7 @@ class PythonExporter(TemplateExporter):
Exports a Python code file.
"""
def _file_extension_default(self):
return 'py'
return '.py'
def _template_file_default(self):
return 'python'