static void Main(string[] args) { client = new JupyterBlockingClient(); kernels = client.GetKernels(); if (kernels.Count == 0) { throw new Exception("No kernels found"); } //Connecting to the first kernel found, kernels will contains all the available kerenels Console.WriteLine($"Connecting to kernel {kernels.First().Value.spec.display_name}"); client.StartKernel(kernels.First().Key); DisplayKernelInfo(client.KernelInfo); client.OnOutputMessage += Client_OnOutputMessage; client.OnInputRequest += Client_OnInputRequest; //Mainlook asks code to execute and executes it. Console.WriteLine("\n\nEnter code to execute or Q <enter> to terminate:"); MainLoop(client); //terminating the kernel process Console.WriteLine("SHUTTING DOWN KERNEL"); client.Shutdown(); }
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(); }
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(); }
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(); } }