An ipython notebook is a document that is read by the browser that contains both rich text and python code.
In scientific computing ipython notebooks are often used to perform an analysis some input data file that resides on the local file system.
Instead of manually pasting the full path of the file containing the data into a variable, would be convenient to be able to launch an open-file dialog in order to browse the local file system and select the file. The full path of the file should be returned in a variable (in python).
This can be achieved launching an open-file dialog from a GUI toolkit (i.e. QT). For an example see IPython Notebook: Open/select file with GUI (Qt Dialog).
However, using QT has some disadvantages. First it is an additional dependency. Second it requires enabling the QT gui integration in the notebook and this results in conflicts with the inline plots (see here).
The question here is, is it possible to obtain the full path using only Javascript?
EDIT: The answer posted below only returns the file name, not the full-path.
Right-click on a file or directory and select “Copy Shareable Link” to copy a URL that can be used to open JupyterLab with that file or directory open. Right-click on a file or directory and select “Copy Path” to copy the filesystem relative path.
How to open an IPYNB file. You can open an IPYNB file in Jupyter Notebook (cross-platform), Jupyter Notebook Viewer (Web), Cantor (Linux), or Google Colaboratory (Web). To open an IPYNB file in Jupyter Notebook Viewer, the file must be hosted online (via GitHub or another file hosting service).
Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.
this other StackOverflow "How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?" has already cleared the question : you can't get local fullpath from HTML (5 or previous) interface due to security politic. So it is normal that you need QT (or equivalent) to get what you need.
I've been searching a Flash equivalent, but it seems that you may only have it with AIR according to this StackOverflow : "Flex - How to browse and get the full path of a file on local machine's file system?"
Using the HTML5 construct <input type="file">
is possible to instruct the browser to open a file selector dialog. Then we need to bind a javascript function to the "changed event".
The javascript can use kernel.execute(command)
to execute a command on the python kernel that assign a variable with the selected file path.
Here an example:
input_form = """
<div style="border:solid navy; padding:20px;">
<input type="file" id="file_selector" name="files[]"/>
<output id="list"></output>
</div>
"""
javascript = """
<script type="text/Javascript">
function handleFileSelect(evt) {
var kernel = IPython.notebook.kernel;
var files = evt.target.files; // FileList object
console.log('Executing orig')
console.log(files)
// files is a FileList of File objects. List some properties.
var output = [];
var f = files[0]
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'</_Mli>');
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
var command = 'fname = "' + f.name + '"'
console.log(command)
kernel.execute(command);
}
document.getElementById('file_selector').addEventListener('change', handleFileSelect, false);
</script>
"""
def file_selector():
from IPython.display import HTML, display
display(HTML(input_form + javascript))
After the previous definitions putting in a cell file_selector()
will display a button "Choose file" and after a file is selected the variable fname
in the notebook will contain the file path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With