Search in sources :

Example 1 with IpnbConnection

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();
        }
    };
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel) NotNull(org.jetbrains.annotations.NotNull) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IpnbConnection

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);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbFileEditor(org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)

Example 3 with IpnbConnection

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();
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)

Example 4 with IpnbConnection

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IOException(java.io.IOException) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)

Example 5 with IpnbConnection

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());
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) List(java.util.List) IpnbOutOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutOutputCell) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)

Aggregations

IpnbConnection (org.jetbrains.plugins.ipnb.protocol.IpnbConnection)6 IpnbConnectionListenerBase (org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 List (java.util.List)2 IpnbOutOutputCell (org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutOutputCell)2 IpnbOutputCell (org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell)2 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 IpnbFileEditor (org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)1 IpnbCodePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)1