- 1 year ago
How to load a file to jupyter notebook using browse / file explorer?
To load a file into Jupyter Notebook using the file explorer or browse functionality, you can utilize the FileUpload
widget provided by the ipywidgets
library. Here's an example of how you can do it:
Install the
ipywidgets
library if you haven't already. You can install it using pip:shellpip install ipywidgets
Start a Jupyter Notebook server by running the command:
shelljupyter notebook
In a Jupyter Notebook cell, import the required libraries:
pythonfrom ipywidgets import FileUpload
from IPython.display import display
Create a
FileUpload
widget and display it:pythonuploader = FileUpload()
display(uploader)
Run the cell. You should now see a file uploader widget displayed in the notebook.
Click on the "Browse" button in the file uploader widget, select the file you want to upload from your local file system, and click the "Upload" button.
To access the uploaded file in your code, you can use the
uploader.data
dictionary. The uploaded file is accessible using the file name as the key. For example:pythonuploaded_file = uploader.data['my_file.txt']
You can then read the contents of the file using standard Python file handling methods.
By using the FileUpload
widget, you can enable file selection and uploading directly within your Jupyter Notebook environment.