cutter/src/common/JupyterConnection.cpp

98 lines
1.9 KiB
C++
Raw Normal View History

2018-03-02 13:15:53 +00:00
#ifdef CUTTER_ENABLE_JUPYTER
2017-12-13 17:36:00 +00:00
2018-02-09 15:48:02 +00:00
#include <Python.h>
2017-12-13 17:36:00 +00:00
#include "JupyterConnection.h"
2018-02-22 21:08:06 +00:00
#include "NestedIPyKernel.h"
2018-06-23 16:59:23 +00:00
#include "PythonManager.h"
#include <QVariant>
#include <QDebug>
2017-12-13 17:36:00 +00:00
2018-02-22 19:56:15 +00:00
Q_GLOBAL_STATIC(JupyterConnection, uniqueInstance)
JupyterConnection *JupyterConnection::getInstance()
{
return uniqueInstance;
}
2017-12-13 17:36:00 +00:00
JupyterConnection::JupyterConnection(QObject *parent) : QObject(parent)
{
}
JupyterConnection::~JupyterConnection()
{
}
2018-02-22 19:56:15 +00:00
void JupyterConnection::start()
{
2018-06-23 16:59:23 +00:00
if (notebookInstanceExists) {
2018-02-22 19:56:15 +00:00
return;
}
2018-06-23 16:59:23 +00:00
notebookInstanceExists = Python()->startJupyterNotebook();
2018-02-22 19:56:15 +00:00
emit urlReceived(getUrl());
}
QString JupyterConnection::getUrl()
{
2018-06-23 16:59:23 +00:00
if (!notebookInstanceExists) {
2018-02-22 19:56:15 +00:00
return nullptr;
}
2018-06-23 16:59:23 +00:00
QString url = Python()->getJupyterUrl();
return url;
2018-02-22 21:08:06 +00:00
}
long JupyterConnection::startNestedIPyKernel(const QStringList &argv)
{
NestedIPyKernel *kernel = NestedIPyKernel::start(argv);
2018-03-21 20:32:32 +00:00
if (!kernel) {
2018-02-22 21:08:06 +00:00
qWarning() << "Could not start nested IPyKernel.";
return 0;
}
long id = nextKernelId++;
kernels.insert(id, kernel);
return id;
}
NestedIPyKernel *JupyterConnection::getNestedIPyKernel(long id)
{
2018-02-23 12:04:53 +00:00
auto it = kernels.find(id);
2018-03-21 20:32:32 +00:00
if (it == kernels.end()) {
2018-02-23 12:04:53 +00:00
return nullptr;
}
return *it;
2018-02-22 21:08:06 +00:00
}
2018-02-23 16:42:54 +00:00
QVariant JupyterConnection::pollNestedIPyKernel(long id)
{
auto it = kernels.find(id);
2018-03-21 20:32:32 +00:00
if (it == kernels.end()) {
2018-02-23 16:42:54 +00:00
return QVariant(0);
}
NestedIPyKernel *kernel = *it;
QVariant v = kernel->poll();
2018-03-21 20:32:32 +00:00
if (!v.isNull()) {
2018-02-23 16:42:54 +00:00
// if poll of kernel returns anything but None, it has already quit and should be cleaned up
PyThreadState *subinterpreterState = kernel->getThreadState();
delete kernel;
kernels.erase(it);
PyThreadState *parentThreadState = PyThreadState_Swap(subinterpreterState);
Py_EndInterpreter(subinterpreterState);
PyThreadState_Swap(parentThreadState);
}
return v;
}
2018-03-02 13:15:53 +00:00
#endif