Skip to content

Commit

Permalink
Python: cleaned up the code for the Python Console window.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Oct 30, 2019
1 parent 32d6ccd commit b75f7cf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 36 deletions.
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR" sourcelanguage="en_GB">
<context>
<name>OpenCOR::PythonConsoleWindow::PythonConsoleWindowPlugin</name>
<message>
<source>Python Console</source>
<translation>Console Python</translation>
</message>
<message>
<source>Show/hide the Python Console window</source>
<translation>Montrer/cacher la fenêtre de la Console Python</translation>
</message>
</context>
<context>
<name>PythonConsoleWindow</name>
<message>
<source>Python Console</source>
<translation>Console Python</translation>
</message>
</context>
</TS>
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file alias="${PLUGIN_NAME}_fr">${PROJECT_BUILD_DIR}/${PLUGIN_NAME}_fr.qm</file>
</qresource>
</RCC>
Expand Up @@ -39,9 +39,17 @@ namespace PythonConsoleWindow {

//==============================================================================

// An IPython console
PythonConsoleWindow::PythonConsoleWindow(QWidget *pParent) :
Core::WindowWidget(pParent),
mGui(new Ui::PythonConsoleWindow)
{
// Set up the GUI

static QString PythonConsole = R"PYTHON(
mGui->setupUi(this);

// Create a Python module to setup the console

static const QString PythonConsole = R"PYTHON(
import logging
import sys
Expand All @@ -59,91 +67,80 @@ class QtConsole(object):
# Create and start an in-process kernel manager
self._kernel_manager = QtInProcessKernelManager()
self._kernel_manager.start_kernel(show_banner=False)
# Set the kernel's GUI to PythonQt
self._kernel = self._kernel_manager.kernel
self._kernel.gui = 'pythonqt'
self._kernel.shell.enable_gui()
self._kernel.shell.enable_matplotlib()
# Start the kernel's client
self._kernel_client = self._kernel_manager.client()
self._kernel_client.start_channels()
# Create a Jupyter widget and connect it with our kernel and
# its client
# Create a Jupyter widget and connect it with our kernel and its client
self._widget = QtInProcessRichJupyterWidget(font_size=10, enable_calltips=False)
self._widget.kernel_manager = self._kernel_manager
self._widget.kernel_client = self._kernel_client
self._widget.kernel_manager = self._kernel_manager
# `sys.stderr` will now be redirected to the console, however
# logging is still going to PythonQt (and the terminal shell)
# so we redirect logging output to the console.
# sys.stderr will now be redirected to the console, however logging is
# still going to PythonQt (and the terminal shell), so we redirect
# logging output to the console
logging.getLogger().handlers[0].stream = sys.stderr
def widget(self):
return self._widget
# The console we are creating
# Note: we make it a global so it doesn't get garbage collected
# Note: we make it a global so it doesn't get garbage collected...
qt_console = None
def create_ipython_widget():
global qt_console
qt_console = QtConsole()
return qt_console.widget()
)PYTHON";

//==============================================================================

PythonConsoleWindow::PythonConsoleWindow(QWidget *pParent) :
Core::WindowWidget(pParent),
mGui(new Ui::PythonConsoleWindow)
{
// Set up the GUI

mGui->setupUi(this);

// Get the PythonQt Qt manager

PythonQt *pythonQtInstance = PythonQt::self();

// Create a Python module to setup the console

PythonQtObjectPtr qtConsoleModule = pythonQtInstance->createModuleFromScript("opencor.qtconsole", PythonConsole);

if (qtConsoleModule == nullptr) {
if (PyErr_Occurred() != nullptr) {
PyErr_Print(); // This goes to stderr; should error be reported as a plugin load error??
PyErr_Print();
} else {
std::cerr << "Cannot create QT Console module" << std::endl;
std::cerr << "The Qt Console module could not be created." << std::endl;
}

return;
}

// IPython tracebacks are noisy if modules have an empty filename, so set one
// IPython tracebacks are noisy if modules have an empty filename, so set
// one

qtConsoleModule.addVariable("__file__", "QtConsole");

// Create and retrive an IPython widget
// Create and retrieve an IPython widget

PyObject *createWidget = pythonQtInstance->lookupObject(qtConsoleModule, "create_ipython_widget");
PyObject *ipythonWidget = pythonQtInstance->callAndReturnPyObject(createWidget);
auto widgetWrapper = reinterpret_cast<PythonQtInstanceWrapper *>(ipythonWidget);

if (widgetWrapper == nullptr) {
if (PyErr_Occurred() != nullptr) {
PyErr_Print(); // This goes to stderr; should error be reported as a plugin load error??
PyErr_Print();
} else {
std::cerr << "Cannot create IPython widget" << std::endl;
std::cerr << "An IPython widget could not be created." << std::endl;
}

return;
Expand All @@ -157,7 +154,7 @@ PythonConsoleWindow::PythonConsoleWindow(QWidget *pParent) :

setFocusProxy(mPythonConsoleWidget);

// Add the widget to our window
// Add the IPython widget to our layout

#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
mGui->layout->addWidget(new Core::BorderedWidget(mPythonConsoleWidget,
Expand All @@ -179,6 +176,15 @@ PythonConsoleWindow::~PythonConsoleWindow()

//==============================================================================

void PythonConsoleWindow::retranslateUi()
{
// Retranslate our whole window

mGui->retranslateUi(this);
}

//==============================================================================

} // namespace PythonConsoleWindow
} // namespace OpenCOR

Expand Down
Expand Up @@ -30,6 +30,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include <QtGlobal>

//==============================================================================

#include <QWidget>

//==============================================================================
Expand All @@ -53,6 +56,8 @@ Q_OBJECT
explicit PythonConsoleWindow(QWidget *pParent);
~PythonConsoleWindow() override;

void retranslateUi() override;

private:
Ui::PythonConsoleWindow *mGui;

Expand Down
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>280</height>
<width>320</width>
<height>240</height>
</rect>
</property>
<property name="windowTitle">
Expand Down
Expand Up @@ -22,8 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "coreguiutils.h"
#include "pythonconsolewindowplugin.h"
#include "pythonconsolewindow.h"
#include "pythonconsolewindowplugin.h"

//==============================================================================

Expand Down
Expand Up @@ -25,8 +25,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

//==============================================================================

#include "plugininfo.h"
#include "i18ninterface.h"
#include "plugininfo.h"
#include "plugininterface.h"
#include "windowinterface.h"

Expand Down

0 comments on commit b75f7cf

Please sign in to comment.