0

I am trying to write a Jupyter Notebook extension in which I also have to create a submit button which should post the current notebook file to the external server.

The trouble I am facing is how can I get the notebook file object and then post it. I am not sure if there is a way to get the orginal ipynb file and post it.

I can get a browser URL for example "http://localhost:8888/notebooks/Untitled.ipynb", so I figured I could send a get request to this URL and then post it but the get request obviously sends an HTML file of a notebook which is opened in the browser.

Here is my code

var that = Jupyter.menubar;
var browserURL = Jupyter.notebook.container[0].baseURI;
var request = new XMLHttpRequest();
request.open('GET', browserURL, true); // Send get request to browser URL
request.onload = function() {    
    var reader = new FileReader();    
    reader.readAsDataURL(request.response);
    reader.onload =  function(e){
        var notebookFile = e.target.result; // returned File
        $.ajax({
            url : 'http://example.com/hello.php',
            type : "POST",
            dataType: "json",
            crossDomain: true,
            data: {
                'file': notebookFile,
                'msg': "Hello"
            },
            success: function(data) {
                console.log("Success");
            },
            error: function(xhr, status, error) {
                console.log("error");
            }
        });
    };
};
request.send();

My question is that how can I get the notebook file that I am working on, so I can post it to the server.

Thank you

Hamza
  • 1,593
  • 2
  • 19
  • 31
  • Have you tried `var filename = IPython.notebook.notebook_name;` or `var filename = Jupyter.notebook.notebook_name;`? Depending on what you've called the base package (`Jupyter` or `IPython`), you should be able to access the file name with `(IPython|Jupyter).notebook.notebook_name` – Abdou Sep 25 '18 at 22:18
  • Yes, I can get a filename like this. What I need is the file itself that I can post to server. I need to upload the file. – Hamza Sep 26 '18 at 14:36
  • For that you would have to use python since browser javascript doesn't have direct access to your file system for security reasons. You may want to compose the python code and pass it to the `Jupyter.notebook.kernel.execute` function to run it in the notebook itself. So, submit the `POST` request with `urllib` or `requests`. – Abdou Sep 26 '18 at 17:19

0 Answers0