SpencerPark / jupyter-jvm-client

A Java library for launching, connecting, and communicating with a Jupyter kernel.
MIT License
4 stars 1 forks source link

How to connect to zmqJupyterClient #2

Open li2109 opened 2 years ago

li2109 commented 2 years ago
List<String> configPaths =Arrays.asList(
            "/Users/xxx/.jupyter",
            "/Users/xxx/opt/anaconda3/etc/jupyter",
            "/usr/local/etc/jupyter",
            "/etc/jupyter");
    List<String> dataPaths =
        Arrays.asList(
            "/Users/xxx/Library/Jupyter",
            "/Users/xxx/opt/anaconda3/share/jupyter",
            "/usr/local/share/jupyter",
            "/usr/share/jupyter");
    List<String> runtimePaths = Arrays.asList("/Users/xxx/Library/Jupyter/runtime");

    JupyterPaths jupyterPaths =
        new JupyterPaths(
            configPaths.stream().map(Path::of).collect(Collectors.toList()),
            dataPaths.stream().map(Path::of).collect(Collectors.toList()),
            runtimePaths.stream().map(Path::of).collect(Collectors.toList()));

    KernelSpecManager kernelSpecManager = KernelSpecManager.fromPaths(jupyterPaths);

    Map<String, KernelSpec> allSpecs = kernelSpecManager.getAllSpecs();
    System.out.println(allSpecs);
    KernelConnectionProperties kernelConnectionProperties =
        new KernelConnectionProperties(
            "127.0.0.1", 8990, 8991, 8992, 8993, 8994, "tcp", "hmac-sha256", "");

    ZmqJupyterClient zmqJupyterClient =
        ZmqJupyterClient.createConnectedTo(kernelConnectionProperties);

    ExecutionResult eval = zmqJupyterClient.eval("print(1+1)", IOProvider.STDIO);
    String data = (String) eval.getValue().getData(MIMEType.APPLICATION_JSON);
    System.out.println(data);

I confused on the "key" parameter when creating KernelConnectionProperties. which config does this "key" exactly indicate? is it c.NotebookNotary.secret? c.GatewayClient.client_key? c.NotebookApp.keyfile? c.Session.key?

In addition, how do you tell if a client is connected to the kernel? I run the code above and the console is logging "INFO: Loop started." but nothing from code execution. I really appreciate the code you open sourced. Thank you!

Since there's no doc about the code, do you mind writing a small example code for this zmqJupyterClient?