How to save Jupyter notebook via API?

Good day!

Is it possible to achieve “File” → “Save and Checkpoint” programmatically?

I know there is JS command, but it has no callback and so I can’t be sure that file is actually saved.

I also found “Save file” section in Jupyter APIs:

Tried without success:

import requests
response = requests.request(
method=‘PUT’,
url=‘http://127.0.0.1:8888/api/contents/my-notebook.ipynb’,
headers={‘Authorization’: ‘token 6ec2282d28e6e755629e9007787299a929a0154e54c59df7’},
)
print(response.status_code)
print(response.json())

Don’t you know is it possible to achieve this?

This is helpful.
https://jupyter-notebook.readthedocs.io/en/stable/extending/contents.html

Maybe some error in your put data. put data is content which will be used in ContentManager.

1 Like

@00Kai0 Could you specify more details pls?

This is the JS code in jupyternotebook:

    Contents.prototype.save = function(path, model) {
        /**
         * We do the call with settings so we can set cache to false.
         */
        var settings = {
            processData : false,
            type : "PUT",
            dataType: "json",
            data : JSON.stringify(model),
            contentType: 'application/json',
        };
        var url = this.api_url(path);
        return utils.promising_ajax(url, settings);
    };

The problem is that you need put data in your request.The API will use ContentsManager.save(model, path) to save file. Your need know how to decode an .ipynb to a model, then change something in it, at last put your model to Save API to save .ipynb file.