Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tritemio/430d68d8b0efbbb13462 to your computer and use it in GitHub Desktop.
Save tritemio/430d68d8b0efbbb13462 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:dd9ac9500d76bd6e4f223aa5277e5008fc30d9ad1df9ccab4997b0cbe52860c7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##IPython Notebook: Open/select file with GUI (Qt Dialog)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Often times it comes handy to select a file graphically. This can be easily done with a Qt dialog:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"try:\n",
" from PySide import QtCore, QtGui\n",
"except ImportError:\n",
" from PyQt4 import QtCore, QtGui\n",
"\n",
"def gui_fname(dir=None):\n",
" \"\"\"Select a file via a dialog and returns the file name.\n",
" \"\"\"\n",
" if dir is None: dir ='./'\n",
" fname = QtGui.QFileDialog.getOpenFileName(None, \"Select data file...\", \n",
" dir, filter=\"All files (*);; SM Files (*.sm)\")\n",
" return fname[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, running this function from an notebook causes the kernel to die (and restart):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"full_fname = gui_fname()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ok, this is espected because we need to setup the QT toolking integration.\n",
"\n",
"So, after restating the kernel, please skip the previous line and run the following instead:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%gui qt\n",
"full_fname = gui_fname()\n",
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"unfortunately we still have a kernel crash.\n",
"\n",
"As a workaround, I found that we can execute the GUI toolkit setup in a separate cell. \n",
"\n",
"In practice, after a kernel restart, skip the previous 2 code cells and execute this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%gui qt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you can execute the following cell as many times as you want and it will always display the *OpenFile* dialog without crashing:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%gui qt\n",
"file_name = gui_fname()\n",
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"file_name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"u'C:/Data/Eitan/r6g_olympus.spc'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
@tritemio
Copy link
Author

tritemio commented May 6, 2014

To view the notebook click here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment