From 4588abc6d3cd0a0561767d8a626f08746340d565 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Mon, 13 Mar 2017 19:39:38 -0700 Subject: [PATCH 01/34] create handler for nbconvert post --- notebook/nbconvert/handlers.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/notebook/nbconvert/handlers.py b/notebook/nbconvert/handlers.py index 89995226e1..f59e0b11f0 100644 --- a/notebook/nbconvert/handlers.py +++ b/notebook/nbconvert/handlers.py @@ -15,6 +15,7 @@ path_regex, ) from nbformat import from_dict +from traitlets.config import Config from ipython_genutils.py3compat import cast_bytes from ipython_genutils import text @@ -75,13 +76,10 @@ def get_exporter(format, **kwargs): class NbconvertFileHandler(IPythonHandler): - SUPPORTED_METHODS = ('GET',) - - @web.authenticated - def get(self, format, path): - - exporter = get_exporter(format, config=self.config, log=self.log) - + def call_nbconvert(self, format, path, config = None): + + exporter = get_exporter(format, config=config, log=self.log) + path = path.strip('/') # If the notebook relates to a real file (default contents manager), # give its path to nbconvert. @@ -140,6 +138,22 @@ def get(self, format, path): self.finish(output) + @web.authenticated + def get(self, format, path): + + self.call_nbconvert(format, path, config=self.config) + + @web.authenticated + def post(self, format, path): + + c = Config(self.config) + c.merge(self.get_json_body()) + + self.call_nbconvert(format, path, config=c) + + + + class NbconvertPostHandler(IPythonHandler): SUPPORTED_METHODS = ('POST',) From d197d4d28c8c65f8ef79d303e46387bf3b6d9bd1 Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Fri, 24 Mar 2017 12:06:49 -0700 Subject: [PATCH 02/34] change post handler to expect a notebook in the passed in json --- notebook/nbconvert/handlers.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/notebook/nbconvert/handlers.py b/notebook/nbconvert/handlers.py index f59e0b11f0..af4e62a404 100644 --- a/notebook/nbconvert/handlers.py +++ b/notebook/nbconvert/handlers.py @@ -15,6 +15,7 @@ path_regex, ) from nbformat import from_dict +import nbformat from traitlets.config import Config from ipython_genutils.py3compat import cast_bytes @@ -76,8 +77,10 @@ def get_exporter(format, **kwargs): class NbconvertFileHandler(IPythonHandler): - def call_nbconvert(self, format, path, config = None): + def call_nbconvert(self, format, path, config=None, content=None): + + exporter = get_exporter(format, config=config, log=self.log) path = path.strip('/') @@ -94,8 +97,11 @@ def call_nbconvert(self, format, path, config = None): if model['type'] != 'notebook': # not a notebook, redirect to files return FilesRedirectHandler.redirect_to_files(self, path) - - nb = model['content'] + + if content is None: + nb = model['content'] + else: + nb = nbformat.reads(content, as_version=4) self.set_header('Last-Modified', model['last_modified']) @@ -136,7 +142,7 @@ def call_nbconvert(self, format, path, config = None): self.set_header('Content-Type', '%s; charset=utf-8' % exporter.output_mimetype) - self.finish(output) + self.finish(output + model['name']) @web.authenticated def get(self, format, path): @@ -147,9 +153,10 @@ def get(self, format, path): def post(self, format, path): c = Config(self.config) - c.merge(self.get_json_body()) - - self.call_nbconvert(format, path, config=c) + json_upload = self.get_json_body() + c.merge(json_upload["config"]) + nb_content = json_upload["notebook"] + self.call_nbconvert(format, path, config=c, content=nb_content) From dc4bc76b395fcd1d20c8052f0b13ef064e9ed65c Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Mon, 3 Apr 2017 09:13:13 -0700 Subject: [PATCH 03/34] Lots of changes to try to get the dialog to work --- notebook/nbconvert/handlers.py | 5 +- notebook/static/notebook/js/menubar.js | 72 ++++++++++++++++++++++++++ notebook/templates/notebook.html | 17 +++--- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/notebook/nbconvert/handlers.py b/notebook/nbconvert/handlers.py index af4e62a404..1dc4eb8bc1 100644 --- a/notebook/nbconvert/handlers.py +++ b/notebook/nbconvert/handlers.py @@ -77,10 +77,9 @@ def get_exporter(format, **kwargs): class NbconvertFileHandler(IPythonHandler): - def call_nbconvert(self, format, path, config=None, content=None): + def call_nbconvert(self, format, path, config=None, content=None, post=False): - exporter = get_exporter(format, config=config, log=self.log) path = path.strip('/') @@ -156,7 +155,7 @@ def post(self, format, path): json_upload = self.get_json_body() c.merge(json_upload["config"]) nb_content = json_upload["notebook"] - self.call_nbconvert(format, path, config=c, content=nb_content) + self.call_nbconvert(format, path, config=c, content=nb_content, post=True) diff --git a/notebook/static/notebook/js/menubar.js b/notebook/static/notebook/js/menubar.js index 450ce310cd..0b7d9b10f3 100644 --- a/notebook/static/notebook/js/menubar.js +++ b/notebook/static/notebook/js/menubar.js @@ -129,6 +129,74 @@ define([ this._new_window(url); }; + + MenuBar.prototype._nbconvert_upload_conf = function (format, download) { + + var body = $('
'); + var notebook_path = utils.encode_uri_components(this.notebook.notebook_path); + + var create_json = function(notebook, config){ + var json_to_pass = { + notebook: notebook.toJSON(), + config: config + }; + return json_to_pass; + }; + + var url = utils.url_path_join( + this.base_url, + 'nbconvert', + format, + notebook_path + ) + "?download=" + download.toString(); + + var form = $('
'); + var fileinput = $('').attr('type', 'file'); + + form.append(fileinput); + body.append(form); + + var that = this; + + var handle_nbconvert_post = function (){ + var filereader = new FileReader(); + filereader.readAsText(fileinput[0].files[0]); + filereader.onEvent('loaded', function(){ + var my_config_data = filereader.result; + var json_content = create_json(that.notebook, my_config_data); + var p = $.post(url, json_content, create_new_dl_window, "json"); + p.onReady(function(){ + body.empty().append('

').text('conversion in progress') + }); + }); + + // $.post(url, json_content, create_new_dl_window,"json"); + // get the data from FIleReader and make it json. + return true // close the dialog + // return false to keep it open. + }; + + + var create_new_dl_window = function(){ + return; + }; + + var mod = dialog.modal({ + notebook: notebook, + title : "Edit Command mode Shortcuts", + body : body, + buttons : { + my_end_dialog : { + click: handle_nbconvert_post, + class: 'foo' + } + } + }); + + mod.modal('show'); + + }; + MenuBar.prototype._nbconvert = function (format, download) { download = download || false; @@ -193,6 +261,10 @@ define([ that._nbconvert(ev.target.parentElement.getAttribute('id').substring(9), true); }); + this.element.find('#custom_download').click(function(){ + that._nbconvert_upload_conf('html',true); + }); + this.events.on('trust_changed.Notebook', function (event, trusted) { if (trusted) { that.element.find('#trust_notebook') diff --git a/notebook/templates/notebook.html b/notebook/templates/notebook.html index 7e627b1f92..557415f339 100644 --- a/notebook/templates/notebook.html +++ b/notebook/templates/notebook.html @@ -106,19 +106,20 @@

  • -
  • Custom Download
  • {% trans %}Make a Copy...{% endtrans %}
  • @@ -105,8 +104,8 @@
  • +
  • {% trans %}Custom Download {% endtrans %}