2017-12-13 17:36:00 +00:00
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
2018-01-29 14:13:16 +00:00
|
|
|
#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()
|
|
|
|
{
|
2018-01-29 14:13:16 +00:00
|
|
|
cmdServer->stop();
|
|
|
|
process->terminate();
|
|
|
|
urlProcess->terminate();
|
2017-12-13 17:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *urlPy = "from notebook import notebookapp\n"
|
|
|
|
"import json\n"
|
|
|
|
"import time\n"
|
|
|
|
"\n"
|
|
|
|
"time.sleep(3)\n"
|
|
|
|
"\n"
|
|
|
|
"servers = [si for si in notebookapp.list_running_servers()]\n"
|
|
|
|
"print(json.dumps(servers))";
|
|
|
|
|
|
|
|
void JupyterConnection::start()
|
|
|
|
{
|
|
|
|
process = new QProcess(this);
|
|
|
|
connect(process, &QProcess::readyReadStandardError, this, &JupyterConnection::readStandardError);
|
|
|
|
connect(process, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readStandardOutput);
|
2018-01-29 14:13:16 +00:00
|
|
|
connect(process, &QProcess::errorOccurred, this, [](QProcess::ProcessError error){ qWarning() << "Jupyter error occurred:" << error; });
|
2017-12-13 17:36:00 +00:00
|
|
|
process->start("jupyter", {"notebook", "--no-browser", "-y"});
|
|
|
|
|
|
|
|
urlProcess = new QProcess(this);
|
|
|
|
connect(urlProcess, &QProcess::readyReadStandardOutput, this, &JupyterConnection::readUrlStandardOutput);
|
|
|
|
urlProcess->start("python3", {"-c", urlPy});
|
2018-01-29 14:13:16 +00:00
|
|
|
|
|
|
|
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-01-29 14:13:16 +00:00
|
|
|
printf("Jupyter stderr: %s", data.constData());
|
2017-12-13 17:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JupyterConnection::readStandardOutput()
|
|
|
|
{
|
|
|
|
auto data = process->readAllStandardOutput();
|
2018-01-29 14:13:16 +00:00
|
|
|
printf("Jupyter stdout: %s", data.constData());
|
2017-12-13 17:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JupyterConnection::readUrlStandardOutput()
|
|
|
|
{
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(urlProcess->readAllStandardOutput());
|
|
|
|
|
|
|
|
for(QJsonValue value : doc.array())
|
|
|
|
{
|
|
|
|
QJsonObject serverObject = value.toObject();
|
|
|
|
QString url = serverObject["url"].toString() + "?token=" + serverObject["token"].toString();
|
|
|
|
emit urlReceived(url);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|