On Wed, Feb 8, 2017 at 4:14 AM, Thomas Kluyver <[email protected]> wrote:
> In this line: > > On 8 February 2017 at 02:31, Paul Hobson <[email protected]> wrote: > >> with open(nbfile, 'r') as nb: > > > Can you try adding a parameter to open(..., encoding='utf-8') > > I think it's reading the notebook file wrong because of the default > encoding on Windows. > Thanks for the response. By itself, it didn't do much. But while I was trying to trim down a SSCCE to demonstrate that I found the issue. The crux of it was that I was evaluating the notebook, writing that to a new file, then reading it again and extracting the images. The solution was to simply store the evaluating notebook in memory and extract them from there. Looking back at the documentation, I should have started with that approach. For those facing similar issues, here's my final function that reads an un-executed notebook, executes it in memory, writes an RST of the executed notebook, and saves all of the images: def convert(nbfile): basename, _ = os.path.splitext(nbfile) meta = {'metadata': {'path': '.'}} with open(nbfile, 'r', encoding='utf-8') as nbf: nbdata = nbformat.read(nbf, as_version=4, encoding='utf-8') runner = ExecutePreprocessor(timeout=600, kernel_name='probscale') runner.preprocess(nbdata, meta) img_folder = basename + '_files' body_raw, images = RSTExporter().from_notebook_node(nbdata) body_final = body_raw.replace('.. image:: ', '.. image:: {}/'.format(img_folder)) with open(basename + '.rst', 'w', encoding='utf-8') as rst_out: rst_out.write(body_final) for img_name, img_data in images['outputs'].items(): img_path = os.path.join(img_folder, img_name) with open(img_path, 'wb') as img: img.write(img_data) Thanks for the nudge! -paul -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/CADT3MECg7rXDQLOg9NT7V%3D%3D9Sp6tvZqkStWLHtjLoPnp-wg8rg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
