cutter/src/utils/JupyterConnection.cpp

58 lines
1.8 KiB
C++
Raw Normal View History

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 <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QDebug>
#include <QThread>
2017-12-13 17:36:00 +00:00
#include "JupyterConnection.h"
JupyterConnection::JupyterConnection(QObject *parent) : QObject(parent)
{
}
JupyterConnection::~JupyterConnection()
{
2018-02-09 15:48:02 +00:00
if (cutterNotebookAppInstance)
{
PyEval_RestoreThread(pyThreadState);
auto stopFunc = PyObject_GetAttrString(cutterNotebookAppInstance, "stop");
PyObject_CallObject(stopFunc, nullptr);
Py_DECREF(cutterNotebookAppInstance);
Py_FinalizeEx();
}
cmdServer->stop();
2017-12-13 17:36:00 +00:00
}
void JupyterConnection::start()
{
2018-02-09 15:48:02 +00:00
Py_Initialize();
PyEval_InitThreads();
cutterJupyterModule = PyImport_ImportModule("cutter_jupyter");
auto startFunc = PyObject_GetAttrString(cutterJupyterModule, "start_jupyter");
cutterNotebookAppInstance = PyObject_CallObject(startFunc, nullptr);
auto urlWithToken = PyObject_GetAttrString(cutterNotebookAppInstance, "url_with_token");
auto asciiBytes = PyUnicode_AsASCIIString(urlWithToken);
emit urlReceived(QString::fromUtf8(PyBytes_AsString(asciiBytes)));
Py_DECREF(asciiBytes);
Py_DECREF(urlWithToken);
pyThreadState = PyEval_SaveThread();
2018-02-09 15:48:02 +00:00
cmdServerThread = new QThread(this);
cmdServer = new CommandServer();
cmdServer->moveToThread(cmdServerThread);
connect(cmdServer, &CommandServer::error, this, [](QString err){ qWarning() << "CmdServer error:" << err; });
connect(cmdServerThread, SIGNAL (started()), cmdServer, SLOT (process()));
connect(cmdServer, SIGNAL (finished()), cmdServerThread, SLOT (quit()));
connect(cmdServer, SIGNAL (finished()), cmdServer, SLOT (deleteLater()));
connect(cmdServerThread, SIGNAL (finished()), cmdServerThread, SLOT (deleteLater()));
cmdServerThread->start();
2018-02-08 14:30:59 +00:00
}