cutter/src/utils/JupyterConnection.cpp

51 lines
1.3 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();
}
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");
2018-02-11 11:40:50 +00:00
if (!cutterJupyterModule)
{
qWarning() << "Could not import cutter_jupyter.";
return;
}
2018-02-09 15:48:02 +00:00
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-11 11:40:50 +00:00
}