Save and export as html automatically

581 views
Skip to first unread message

skin...@gmail.com

unread,
Apr 15, 2016, 2:31:25 PM4/15/16
to Project Jupyter
Hi,

I'm trying to automatically export as html at the end of execution. (i.e. enter some parameters, 'run all' and have an html output).

From searching the group here, I came up with:

%%javascript
Jupyter.notebook.save_notebook();
Jupyter.notebook.kernel.execute("window_loc = '" + window.location + "'")
Jupyter.notebook.kernel.execute("notebook_base = '" + IPython.notebook.base_url + "'")
Jupyter.notebook.kernel.execute("notebook_path = '" + IPython.notebook.notebook_path + "'")


then on the next cell:

filename = 'outout.html'
from notebook.utils import url_path_join
import requests
scheme
= requests.utils.urlparse(window_loc).scheme
netloc
= requests.utils.urlparse(window_loc).netloc
base_url
= requests.utils.urlunparse((scheme,netloc, '', '', '', ''))
url
= url_path_join(base_url, 'nbconvert', 'html', notebook_base, notebook_path)
r
= requests.request('GET', url, params={'download':True})

if r.status_code == 200:
   
with open(filename, 'wb') as f:
       
for chunk in r.iter_content(1024):
            f
.write(chunk)



they work okay when I execute one by one, but when I do 'run-all', it seems like the javascript ran async, so the next cell try to execute before it finishes saving and collecting the variables.

Any thoughts?

Cheers,
SL

Thomas Kluyver

unread,
Apr 15, 2016, 2:43:25 PM4/15/16
to Project Jupyter
On 15 April 2016 at 19:31, <skin...@gmail.com> wrote:
but when I do 'run-all', it seems like the javascript ran async, so the next cell try to execute before it finishes saving and collecting the variables.

The code the Javascript is sending to the kernel will certainly be out of order. You might try defining a function to do what you want in the kernel, and then sending code from the JS to call it.

Thomas

skin...@gmail.com

unread,
Apr 15, 2016, 3:42:35 PM4/15/16
to Project Jupyter
Thanks Thomas. Would you have any pointers/links on how this can be done? Don't have a whole lot of js background. Cheers, SL

Thomas Kluyver

unread,
Apr 15, 2016, 4:16:18 PM4/15/16
to Project Jupyter
I meant something much like you're already doing, except in the Python code, do:

def export_html(notebook_path):
   ...

Then in the JS:

Jupyter.notebook.kernel.execute("export_html('"+notebook_path+"')");

However, looking at what it is you're trying to do more broadly, you might be interested in using nbparameterise:
https://github.com/takluyver/nbparameterise

Have a go with both the demos in the examples directory. After installing nbparameterise (pip install nbparameterise), run:

python webapp.py "Stock Display.ipynb"
python batch.py

The idea of nbparameterise is to change some parameters at the start of a notebook, then execute that notebook, updating the outputs. The resulting notebook can easily be saved or fed into nbconvert to turn it into HTML (for instance). And it doesn't need a browser to run.

Best wishes,
Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/37c6baee-3959-4f7c-bf3c-0b5b41b8a6dd%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

skin...@gmail.com

unread,
Apr 18, 2016, 8:30:29 PM4/18/16
to Project Jupyter
Thanks Thomas.

I'll give nbparameterise a try.

I was able to make that function work, but only if it is on the last cell. If there are more cells after that, when I do 'run-all', it doesn't work properly. Still investigating why... something to do with the order of execution maybe.

Cheers,
SL
Reply all
Reply to author
Forward
0 new messages