Skip to content

Commit

Permalink
Re #5939. Add an IPython console as the script interpreter.
Browse files Browse the repository at this point in the history
For now, requires IPython 1.0 to be installed locally.
  • Loading branch information
RussellTaylor committed Aug 26, 2013
1 parent f4fa114 commit 1497115
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
15 changes: 14 additions & 1 deletion Code/Mantid/MantidPlot/CMakeLists.txt
Expand Up @@ -852,14 +852,24 @@ copy_python_files_to_dir ( "${MTDPLOTPY_FILES}"
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/mantidplotpy
MTDPLOT_INSTALL_FILES )

# IPython scripts
set( IPY_FILES
__init__.py
mantid_ipython_widget.py
)
copy_python_files_to_dir ( "${IPY_FILES}"
${CMAKE_CURRENT_SOURCE_DIR}/ipython_widget
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/ipython_widget
IPYTHON_INSTALL_FILES )

###########################################################################
# MantidPlot executable
###########################################################################

add_executable ( MantidPlot ${WIN_CONSOLE} MACOSX_BUNDLE ${ALL_SRC} src/main.cpp
${INC_FILES} ${QTIPLOT_C_SRC} ${UI_HDRS}
${RES_FILES} ${MANTID_RC_FILE}
${PYTHON_INSTALL_FILES} ${MTDPLOT_INSTALL_FILES}
${PYTHON_INSTALL_FILES} ${MTDPLOT_INSTALL_FILES} ${IPYTHON_INSTALL_FILES}
)

# Library dependencies
Expand Down Expand Up @@ -992,6 +1002,9 @@ install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR} )
foreach(PY_FILE ${MTDPLOTPY_FILES} )
install ( FILES mantidplotpy/${PY_FILE} DESTINATION ${BIN_DIR}/mantidplotpy )
endforeach()
foreach(PY_FILE ${IPY_FILES} )
install ( FILES ipython_widget/${PY_FILE} DESTINATION ${BIN_DIR}/ipython_widget )
endforeach()

if ( APPLE )
configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/FixBundle.cmake.in
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/MantidPlot/ipython_widget/__init__.py
@@ -0,0 +1 @@
from mantid_ipython_widget import *
25 changes: 25 additions & 0 deletions Code/Mantid/MantidPlot/ipython_widget/mantid_ipython_widget.py
@@ -0,0 +1,25 @@
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager

class MantidIPythonWidget(RichIPythonWidget):

def __init__(self, *args, **kw):
super(MantidIPythonWidget, self).__init__(*args, **kw)

# Create an in-process kernel
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel = kernel_manager.kernel
kernel.gui = 'qt4'

import mantidplot
kernel.shell.push(mantidplot.__dict__)
import mantid.simpleapi
kernel.shell.push(mantid.simpleapi.__dict__)

kernel_client = kernel_manager.client()
kernel_client.start_channels()

self.kernel_manager = kernel_manager
self.kernel_client = kernel_client

2 changes: 1 addition & 1 deletion Code/Mantid/MantidPlot/mantidplotrc.py
Expand Up @@ -20,7 +20,7 @@ def get_default_python_api():
settings = QtCore.QSettings()
settings.beginGroup("Mantid")
settings.beginGroup("Python")
api_version = settings.value("APIVersion", 2).toInt()
api_version = int(settings.value("APIVersion", 2))
settings.endGroup()
settings.endGroup()
if type(api_version) == tuple:
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -499,8 +499,9 @@ void ApplicationWindow::init(bool factorySettings, const QStringList& args)
//Scripting
m_script_envs = QHash<QString, ScriptingEnv*>();
setScriptingLanguage(defaultScriptingLang);
//delete m_interpreterDock->widget();
delete m_interpreterDock->widget();
m_iface_script = NULL;
runPythonScript("from ipython_widget import *\nw = _qti.app._getInterpreterDock()\nw.setWidget(MantidIPythonWidget())",false,true,true);
loadCustomActions();

// Print a warning message if the scripting language is set to muParser
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/ApplicationWindow.h
Expand Up @@ -1354,6 +1354,8 @@ public slots:
//! The scripting language to use for new projects.
QString defaultScriptingLang;

QDockWidget *m_interpreterDock;

private:
mutable MdiSubWindow *d_active_window;
MdiSubWindow* getActiveWindow() const;
Expand Down Expand Up @@ -1390,7 +1392,6 @@ public slots:
QTranslator *appTranslator, *qtTranslator;
QDockWidget *explorerWindow, *undoStackWindow;
MantidQt::MantidWidgets::MessageDisplay *resultsLog;
QDockWidget *m_interpreterDock;
QMdiArea *d_workspace;

QToolBar *standardTools, *plotTools, *displayBar;
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/MantidPlot/src/PythonScripting.cpp
Expand Up @@ -176,6 +176,9 @@ bool PythonScripting::start()
return false;
}

// Our use of the IPython console requires that we use the v2 api for these PyQt types
// This has to be set before the very first import of PyQt (which happens in init_qti)
PyRun_SimpleString("import sip\nsip.setapi('QString',2)\nsip.setapi('QVariant',2)");
//Embedded qti module needs sip definitions initializing before it can be used
init_qti();

Expand Down
6 changes: 6 additions & 0 deletions Code/Mantid/MantidPlot/src/qti.sip
Expand Up @@ -1124,6 +1124,12 @@ public:

MDIWindowList windowsList() /PyName=windows/;

// Required for setting the IPython console
QDockWidget _getInterpreterDock();
%MethodCode
sipRes = sipCpp->m_interpreterDock;
%End

// folders
Folder *activeFolder();
%MethodCode
Expand Down

0 comments on commit 1497115

Please sign in to comment.