2

In a jupyter notebook I'd like to use html/javascipt for a file selection dialog instead of tkinter. After some messing around I was able to get this to work and even make the result available to the python kernel. BUT, now I think I see why no one else does this... javascript is not aware of the full path to these files.

I was quite proud of this (inside of a jupyter cell):

%%html
<input type="file" accept=".dat" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
    function handleFileSelect(evt) {
        var files = evt.target.files;
        var output = [];
        IPython.notebook.kernel.execute("lstFiles=[]");
        for (var i = 0, f; f = files[i]; i++) {
            IPython.notebook.kernel.execute("lstFiles.append('" + f.name + "')");
            output.push('<li><strong>', f.name, '</strong></li>');
        }
        document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
    }

    document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

then in another cell...

print(lstFiles)

This gets me the list of files, but not the path. Is there any trick to getting the path? Or even the browser's last used location?

etarhan
  • 4,062
  • 2
  • 10
  • 25
jayveesea
  • 1,832
  • 7
  • 19
  • 2
    the path is NOT avaiable in javascript form upload, only the filename. There is NO way the path to file is known – Nikos M. Apr 03 '19 at 11:20
  • Thanks for the feedback, this is what I feared. I was hoping there was some jupyter magic that could be leveraged. I was able to get the path by searching for the returned files via `os.walk` (in python) but this is not very clean or efficient. I guess for now the best option is tkinter. – jayveesea Apr 05 '19 at 00:19
  • This is not allowed by Javascript due to security concerns, see also [How to get full path of selected file on change of using javascript, jquery-ajax?](https://stackoverflow.com/questions/15201071/how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav). – user2314737 Apr 08 '19 at 18:55

0 Answers0