Dear community, I am looking for help.
I wanted to create a filebrowser so I can use a generic notebook for similar datafiles. The 2nd cell shows the user which *.csv files exist in the current folder and in the 3rd cell an input field is created which then loads the user input as filename into a function to create a DataFrame. After execution, the focus of the notebook is back to the 3rd cell, instead of jumping to the 4th one which is like a loop for non-professional users. How can I make this usersafe and after the execution the focus of the notebook automatically (not by down arrow, I know this) jumps to the next cell?
In the beginning I tried to use inline javascript with execute to create clickable links of all available data files, but the data from the links were out of scope for the function of the cell above.
This is my code:
files = [f for f in os.listdir('.') if os.path.isfile(f) and not f.startswith('.git') and not f.endswith('.ipynb')]
for file in files:
display(Markdown(f"[{file}](#{file})"))
def load_dataset(filename):
try:
df = pd.read_csv(filename)
display(Markdown("Data ✅"))
return df
except UnicodeDecodeError:
!pip install chardet
import chardet
with open(filename, 'rb') as f:
result = chardet.detect(f.read(10000))
print("Encoding: " + result['encoding'])
try:
df = pd.read_csv(filename, encoding=result['encoding'])
display(Markdown("Data ✅"))
return df
except Exception as e:
display(Markdown('Error ❌'))
print(e)
except Exception as e:
display(Markdown('Error ❌'))
print(e)
filename = input("Enter the file path: ")
if os.path.exists(filename):
df = load_dataset(filename)
else:
print(f"File could not be read: {filename}")
I tried pynput.keyboard:
df = load_dataset(filename)
keyboard = Controller()
keyboard.press(Key.down)
keyboard.release(Key.down)
Which needs an additional install. What could I add after “df = load_dataset(filename)” to jump to the next cell?