Ask Your Question

Revision history [back]

One way to enable user input for file paths in a Jupyter Notebook without allowing code edits is to use the widgets module.

First, import the widgets module:

from ipywidgets import widgets

Next, create a text box widget for the user to input the file path:

file_path = widgets.Text(
    value='',
    placeholder='Enter a file path',
    description='File path:',
    disabled=False
)
display(file_path)

The value parameter sets the initial value of the text box, the placeholder parameter provides a hint to the user on what to enter, and the description parameter adds a label to the widget.

Finally, use the value attribute of the file_path widget to access the user input in your code:

with open(file_path.value, 'r') as f:
    # do something with the file

This allows the user to input the file path without having to edit the code, and the widgets module takes care of all the user input handling.