use of org.jetbrains.plugins.ipnb.protocol.IpnbConnection in project intellij-community by JetBrains.
the class IpnbConnectionManager method createConnectionListener.
@NotNull
private IpnbConnectionListenerBase createConnectionListener(@Nullable IpnbCodePanel codePanel, boolean[] connectionOpened) {
return new IpnbConnectionListenerBase() {
@Override
public void onOpen(@NotNull IpnbConnection connection) {
connectionOpened[0] = true;
if (codePanel == null)
return;
final String messageId = connection.execute(codePanel.getCell().getSourceAsString());
myUpdateMap.put(messageId, codePanel);
}
@Override
public void onOutput(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
if (!myUpdateMap.containsKey(parentMessageId))
return;
final IpnbCodePanel cell = myUpdateMap.get(parentMessageId);
cell.getCell().setPromptNumber(connection.getExecCount());
cell.updatePanel(null, connection.getOutput());
}
@Override
public void onPayload(@Nullable String payload, @NotNull String parentMessageId) {
if (!myUpdateMap.containsKey(parentMessageId))
return;
final IpnbCodePanel cell = myUpdateMap.remove(parentMessageId);
if (payload != null) {
cell.updatePanel(payload, null);
}
}
@Override
public void onFinished(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
if (!myUpdateMap.containsKey(parentMessageId))
return;
final IpnbCodePanel cell = myUpdateMap.remove(parentMessageId);
cell.getCell().setPromptNumber(connection.getExecCount());
cell.finishExecution();
}
};
}
use of org.jetbrains.plugins.ipnb.protocol.IpnbConnection in project intellij-community by JetBrains.
the class IpnbConnectionManager method executeCell.
public void executeCell(@NotNull final IpnbCodePanel codePanel) {
final IpnbFileEditor fileEditor = codePanel.getFileEditor();
final VirtualFile virtualFile = fileEditor.getVirtualFile();
final String path = virtualFile.getPath();
if (!hasConnection(path)) {
startConnection(codePanel, fileEditor, path);
} else {
IpnbConnection connection = myKernels.get(path);
if (!connection.isAlive()) {
myKernels.remove(path);
startConnection(codePanel, fileEditor, path);
} else {
final String messageId = connection.execute(codePanel.getCell().getSourceAsString());
myUpdateMap.put(messageId, codePanel);
}
}
}
use of org.jetbrains.plugins.ipnb.protocol.IpnbConnection in project intellij-community by JetBrains.
the class WebSocketConnectionTest method testStartAndShutdownKernel.
public void testStartAndShutdownKernel() throws URISyntaxException, IOException, InterruptedException {
final IpnbConnection connection = new IpnbConnection(getTestServerURI(), new IpnbConnectionListenerBase() {
@Override
public void onOpen(@NotNull IpnbConnection connection) {
assertTrue(connection.getKernelId().length() > 0);
connection.shutdown();
}
}, null, DefaultProjectFactory.getInstance().getDefaultProject(), "");
connection.close();
}
use of org.jetbrains.plugins.ipnb.protocol.IpnbConnection in project intellij-community by JetBrains.
the class IpnbConnectionManager method setupConnection.
private boolean setupConnection(@NotNull IpnbCodePanel codePanel, @NotNull String path, @NotNull String urlString, boolean showNotification, boolean[] connectionOpened, boolean isNewFormat) {
try {
final IpnbConnectionListenerBase listener = createConnectionListener(codePanel, connectionOpened);
final VirtualFile file = codePanel.getFileEditor().getVirtualFile();
final String pathToFile = getRelativePathToFile(file);
if (pathToFile == null)
return false;
final IpnbConnection connection = getConnection(urlString, listener, pathToFile, isNewFormat);
int countAttempt = 0;
while (!connectionOpened[0] && countAttempt < MAX_ATTEMPTS) {
countAttempt += 1;
TimeoutUtil.sleep(1000);
}
myKernels.put(path, connection);
} catch (URISyntaxException e) {
if (showNotification) {
showWarning(codePanel.getFileEditor(), "Please, check Jupyter Notebook URL in <a href=\"\">Settings->Tools->Jupyter Notebook</a>", new IpnbSettingsAdapter());
LOG.warn("Jupyter Notebook connection refused: " + e.getMessage());
}
return false;
} catch (UnsupportedOperationException e) {
showWarning(codePanel.getFileEditor(), e.getMessage(), new IpnbSettingsAdapter());
} catch (UnknownHostException e) {
showWarning(codePanel.getFileEditor(), "Please, check Jupyter Notebook URL in <a href=\"\">Settings->Tools->Jupyter Notebook</a>", new IpnbSettingsAdapter());
} catch (IOException e) {
if (IpnbConnection.AUTHENTICATION_NEEDED.equals(e.getMessage())) {
ApplicationManager.getApplication().invokeAndWait(() -> myToken = askForToken(urlString));
if (myToken != null) {
return setupConnection(codePanel, path, urlString, showNotification, connectionOpened, isNewFormat);
}
}
if (showNotification) {
final String message = e.getMessage();
if (message.startsWith(IpnbConnection.UNABLE_LOGIN_MESSAGE)) {
showWarning(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook: login failed", new IpnbSettingsAdapter());
} else if (message.startsWith(CONNECTION_REFUSED) || message.startsWith(IpnbConnection.CANNOT_START_JUPYTER)) {
showWarning(codePanel.getFileEditor(), "Cannot connect to Jupyter Notebook: cannot connect to Jupyter server", new IpnbSettingsAdapter());
}
LOG.warn("Jupyter Notebook connection refused: " + message);
}
return false;
}
return true;
}
use of org.jetbrains.plugins.ipnb.protocol.IpnbConnection in project intellij-community by JetBrains.
the class WebSocketConnectionTest method testCompositeInput.
public void testCompositeInput() throws IOException, URISyntaxException, InterruptedException {
final Ref<Boolean> evaluated = Ref.create(false);
final IpnbConnection connection = new IpnbConnection(getTestServerURI(), new IpnbConnectionListenerBase() {
private String myMessageId;
@Override
public void onOpen(@NotNull IpnbConnection connection) {
myMessageId = connection.execute("def simple_crit_func(feat_sub):\n" + "\n" + " \"\"\" Returns sum of numerical values of an input list. \"\"\" \n" + "\n" + " return sum(feat_sub)\n" + "\n" + "simple_crit_func([1,2,4])");
}
@Override
public void onOutput(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
if (myMessageId.equals(parentMessageId)) {
final IpnbOutputCell output = connection.getOutput();
assertEquals(output.getClass(), IpnbOutOutputCell.class);
final List<String> text = output.getText();
assertNotNull(text);
assertEquals("7", text.get(0));
evaluated.set(true);
connection.shutdown();
}
}
}, null, DefaultProjectFactory.getInstance().getDefaultProject(), "");
connection.close();
assertTrue(evaluated.get());
}
Aggregations