示例#1
0
    static void Main(string[] args)
    {
        //Initializing the Jupyter client
        //The constructor of JupyterBlockingClient will throw an execption if the jupyter framework is not found
        //It is searched on the folders defined on the PATH system variable
        //You can also pass the folder where python.exe is located as an argument of the constructor
        // (since the jupyter framework is located on the python folder)
        var client = new JupyterBlockingClient();

        //Getting available kernels
        var kernels = client.GetKernels();

        if (kernels.Count == 0)
        {
            throw new Exception("No kernels found");
        }

        //Connecting to the first kernel found
        Console.WriteLine($"Connecting to kernel {kernels.First().Value.spec.display_name}");
        client.StartKernel(kernels.First().Key);
        Console.WriteLine("Connected\n");

        //A callback that is executed when there is any information that needs to be shown to the user
        client.OnOutputMessage += Client_OnOutputMessage;

        //Executing some code
        client.Execute("print(\"Hello from Jupyter\")");

        //Closing the kernel
        client.Shutdown();

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
    }
示例#2
0
    private static void MainLoop(JupyterBlockingClient client)
    {
        //Using the component ReadLine, which has some nice features like code completion and history support
        ReadLine.HistoryEnabled        = true;
        ReadLine.AutoCompletionHandler = new AutoCompletionHandler(client);
        var enteredCode  = new StringBuilder();
        var startNewCode = true;
        var lineIdent    = string.Empty;

        while (true)
        {
            enteredCode.Append(ReadLine.Read(startNewCode ? Prompt : PromptWhite + lineIdent));
            var code = enteredCode.ToString();
            if (code == "Q")
            {
                //When the user types Q we terminates the application
                return;
            }
            else if (string.IsNullOrWhiteSpace(code))
            {
                //No code entered, do nothing
            }
            else
            {
                //Asking the kernel if the code entered by the user so far is a complete statement.
                // If not, for example because it is the first line of a function definition,
                // we aske the user to enter one more lone
                var isComplete = client.IsComplete(code);
                switch (isComplete.status)
                {
                case JupyterMessage.IsCompleteStatusEnum.complete:
                    //the code is complete, execute it
                    //the results are given on the OnOutputMessage callback
                    client.Execute(code);
                    startNewCode = true;
                    break;

                case JupyterMessage.IsCompleteStatusEnum.incomplete:
                    lineIdent = isComplete.indent;
                    enteredCode.Append("\n" + lineIdent);
                    startNewCode = false;
                    break;

                case JupyterMessage.IsCompleteStatusEnum.invalid:
                case JupyterMessage.IsCompleteStatusEnum.unknown:
                    Console.WriteLine("Invalid code: " + code);
                    startNewCode = true;
                    break;
                }
            }

            if (startNewCode)
            {
                enteredCode.Clear();
            }
        }
    }
示例#3
0
    static void Main(string[] args)
    {
        var client = new JupyterBlockingClient();

        //Getting available kernels
        var kernels = client.GetKernels();

        if (kernels.Count == 0)
        {
            throw new Exception("No kernels found");
        }

        //Connecting to the first kernel found
        Console.WriteLine($"Connecting to kernel {kernels.First().Value.spec.display_name}");
        client.StartKernel(kernels.First().Key);
        Console.WriteLine("Connected");

        //Creating a notebook and adding a code cell
        var nb   = new Notebook(client.KernelSpec, client.KernelInfo.language_info);
        var cell = nb.AddCode("print(\"Hello from Jupyter\")");

        //Setting up the callback so that the outputs are written on the notebook
        client.OnOutputMessage += (sender, message) => { if (ShouldWrite(message))
                                                         {
                                                             cell.AddOutputFromMessage(message);
                                                         }
        };

        //executing the code
        client.Execute(cell.source);

        //saving the notebook
        nb.Save("test.ipynb");
        Console.WriteLine("File test.ipynb written");

        //Closing the kernel
        client.Shutdown();

        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
    }
示例#4
0
    static void Main(string[] args)
    {
        var client = new JupyterBlockingClient();

        //Getting available kernels
        var kernels = client.GetKernels();

        if (kernels.Count == 0)
        {
            throw new Exception("No kernels found");
        }

        //Connecting to the first kernel found
        Console.WriteLine($"Connecting to kernel {kernels.First().Value.spec.display_name}");
        client.StartKernel(kernels.First().Key);
        Console.WriteLine("Connected\n");

        //Loading a script containing the definition of the function do_something
        client.Execute("%run script.py");

        //Creating an event handler that stores the result of the computation in a TaskCompletionSource object
        var promise = new TaskCompletionSource <string>();
        EventHandler <JupyterMessage> hanlder = (sender, message) =>
        {
            if (message.header.msg_type == JupyterMessage.Header.MsgType.execute_result)
            {
                var content = (JupyterMessage.ExecuteResultContent)message.content;
                promise.SetResult(content.data[MimeTypes.TextPlain]);
            }
            else if (message.header.msg_type == JupyterMessage.Header.MsgType.error)
            {
                var content = (JupyterMessage.ErrorContent)message.content;
                promise.SetException(new Exception($"Jupyter kenel error: {content.ename} {content.evalue}"));
            }
        };

        client.OnOutputMessage += hanlder;
        //calling the function do_something
        client.Execute("do_something(2)");
        //removing event handler, since the TaskCompletionSource cannot be reused
        client.OnOutputMessage -= hanlder;

        //getting the result
        try
        {
            Console.WriteLine("Result:");
            if (promise.Task.IsCompleted)
            {
                Console.WriteLine(promise.Task.Result);
            }
            else
            {
                Console.WriteLine("No result received");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            //Closing the kernel
            client.Shutdown();
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }