Often times it comes handy to select a file graphically. This can be easily done with a Qt dialog:
try:
from PySide import QtCore, QtGui
except ImportError:
from PyQt4 import QtCore, QtGui
def gui_fname(dir=None):
"""Select a file via a dialog and returns the file name.
"""
if dir is None: dir ='./'
fname = QtGui.QFileDialog.getOpenFileName(None, "Select data file...",
dir, filter="All files (*);; SM Files (*.sm)")
return fname[0]
However, running this function from an notebook causes the kernel to die (and restart):
full_fname = gui_fname()
Ok, this is espected because we need to setup the QT toolking integration.
So, after restating the kernel, please skip the previous line and run the following instead:
%gui qt
full_fname = gui_fname()
%matplotlib inline
unfortunately we still have a kernel crash.
As a workaround, I found that we can execute the GUI toolkit setup in a separate cell.
In practice, after a kernel restart, skip the previous 2 code cells and execute this:
%gui qt
Now you can execute the following cell as many times as you want and it will always display the OpenFile dialog without crashing:
%gui qt
file_name = gui_fname()
%matplotlib inline
file_name
u'C:/Data/Eitan/r6g_olympus.spc'
Note: In the previous cell I use the magic
matplotlib inline
because, while it is handy to load a file name via GUI, I still want my plots to be inline.