I am trying to launch dialogs for selecting files, resp. directories from jupyter notebook cells.
while I managed to successfully load files that were selected via a Qt5 dialog, trying to select a directory in analogous manner keeps killing the kernel
Here are the cells for the file selection:
%%writefile 'openCSV.py'
from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication
# sys.executable is just the path to the running python executable
def openCSV(directory='./'):
"""Open a file dialog, starting in the given directory, and return
the chosen filename"""
# run this exact file in a separate process, and grab the result
file = check_output([executable, __file__, directory])
return file.strip()
if __name__ == "__main__":
directory = argv[1]
app = QApplication([directory])
fname = QFileDialog.getOpenFileName(None, "Select a file...",
directory, filter="CSV files (*.csv)")
print(fname[0])
import openCSV
f = openCSV.openCSV('C:/Users/Me/Documents/')
print(f'{f=}')
And here the failing attempt to select a directory:
%%writefile selectFolder
from sys import executable, argv
from subprocess import check_output
from PyQt5.QtWidgets import QFileDialog, QApplication
def selectFolder(directory='./'):
folder = check_output([executable, __file__, directory])
return folder.strip()
if __name__ == "__main__":
directory = argv[1]
app = QApplication([directory])
folderpath = QFileDialog.getExistingDirectory(None, "Select folder")
import selectFolder
d = selectFolder.selectFolder()
print(f'{d=}')
As I’m new to the topic my guess is that its a minor thing that needs to be changed, but what is it, resp. what is the essential difference between selecting files and folders?