cutter/src/utils/JupyterConnection.cpp

86 lines
3.1 KiB
C++
Raw Normal View History

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)
{
process = nullptr;
}
JupyterConnection::~JupyterConnection()
{
cmdServer->stop();
process->terminate();
2017-12-13 17:36:00 +00:00
}
2018-02-08 14:30:59 +00:00
const char *jupyterPyCode = "import sys\n"
"from notebook.notebookapp import *\n"
2017-12-13 17:36:00 +00:00
"\n"
"\n"
2018-02-08 14:30:59 +00:00
"class CutterNotebookApp(NotebookApp):\n"
" def start(self):\n"
" \"\"\" see NotebookApp.start() \"\"\"\n"
"\n"
" super(NotebookApp, self).start()\n"
"\n"
" self.write_server_info_file()\n"
"\n"
" if self.token and self._token_generated:\n"
" url = url_concat(self.connection_url, {'token': self.token})\n"
" sys.stdout.write(url + \"\\n\")\n"
" sys.stdout.flush()\n"
"\n"
" self.io_loop = ioloop.IOLoop.current()\n"
" if sys.platform.startswith('win'):\n"
" # add no-op to wake every 5s\n"
" # to handle signals that may be ignored by the inner loop\n"
" pc = ioloop.PeriodicCallback(lambda: None, 5000)\n"
" pc.start()\n"
" try:\n"
" self.io_loop.start()\n"
" except KeyboardInterrupt:\n"
" self.log.info(_(\"Interrupted...\"))\n"
" finally:\n"
" self.remove_server_info_file()\n"
" self.cleanup_kernels()\n"
"\n"
"\n"
"if __name__ == \"__main__\":\n"
" CutterNotebookApp.launch_instance()";
2017-12-13 17:36:00 +00:00
void JupyterConnection::start()
{
process = new QProcess(this);
connect(process, &QProcess::readyReadStandardError, this, &JupyterConnection::readStandardError);
connect(process, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readStandardOutput);
connect(process, &QProcess::errorOccurred, this, [](QProcess::ProcessError error){ qWarning() << "Jupyter error occurred:" << error; });
2018-02-08 14:30:59 +00:00
process->start("python3", {"-c", jupyterPyCode});
QThread *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();
2017-12-13 17:36:00 +00:00
}
void JupyterConnection::readStandardError()
{
auto data = process->readAllStandardError();
2018-02-08 14:30:59 +00:00
printf("Jupyter stderr: %s\n", data.constData());
2017-12-13 17:36:00 +00:00
}
void JupyterConnection::readStandardOutput()
{
auto data = process->readAllStandardOutput();
2018-02-08 14:30:59 +00:00
printf("Jupyter stdout: %s\n", data.constData());
emit urlReceived(data);
}