Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter Notebook: save all images to svg by program

When I click the Download as markdown in the menu,

enter image description here

it will give me zip file with .md, .png, '.svg'

enter image description here

Note: I use set_matplotlib_formats('png', 'svg'), so the download file returns 2 image file formats.

Now, I want to save all the images in this notebook into svg by program (i.e., writing Python script), how can I do it? Maybe something like

save_images_to_svg('this_notebook.ipynb')
# and it will save all images to '.\images\*.svg'

What I know so far:

The link for Download as markdown is at here

    this.element.find('#download_markdown').click(function () {
        that._nbconvert('markdown', true);
    });

It further binds to the _nbconvert at here

MenuBar.prototype._nbconvert = function (format, download) {
    download = download || false;
    var notebook_path = utils.encode_uri_components(this.notebook.notebook_path);
    var url = utils.url_path_join(
        this.base_url,
        'nbconvert',
        format,
        notebook_path
    ) + "?download=" + download.toString();

    this._new_window(url);
    };

This means that the notebook sends a request to the backend to do the conversion.

I don't know where it is, but it seems like here, because it has a respond_zip method.

I think a quick hack is simply to download the file using python with the url from MenuBar.prototype._nbconvert.

like image 229
cqcn1991 Avatar asked Sep 08 '25 07:09

cqcn1991


1 Answers

Create the plot using matplotlib and save the image with the following command. It works in Sage 8.1 (windows). You will find the figures where your .ipynb file is located.

import numpy
import matplotlib.pyplot as plt

x = numpy.arange(-2 * numpy.pi, 2 * numpy.pi, 0.1)
func = numpy.sin(x)
plt.figure(figsize=(3.3, 3.3))    # inches
plt.plot(x, func, linewidth=2.0, color='blue') 

plt.savefig('demo1.svg')
plt.savefig('demo1.pdf')
plt.savefig('demo1.eps')
plt.savefig('demo1.tif')
plt.show()
# plt.close()
like image 122
Amritendu Mukhopadhyay Avatar answered Sep 10 '25 01:09

Amritendu Mukhopadhyay