Exemple #1
0
        public static void Display(object value)
        {
            if (value != null)
            {
                if (Evaluation.sbPrint != null && Evaluation.sbPrint.Length > 0)
                {
                    kernel.SendDisplayData("text/plain", Evaluation.sbPrint.ToString());
                    Evaluation.sbPrint.Clear();
                }

                BinaryOutput output = Printers.PrintVariable(value);
                kernel.SendDisplayData(output.ContentType, output.Data);
            }
        }
Exemple #2
0
        internal void executeRequest(KernelMessage msg, ExecuteRequest content)
        {
            Evaluation.sbOut.Clear();
            Evaluation.sbErr.Clear();
            Evaluation.sbPrint.Clear();
            payload.Clear();

            if (!content.silent)
            {
                executionCount++;
            }

            // sending busy
            sendStateBusy(msg);
            sendMessage(ioSocket, msg, "pyin", new Pyin()
            {
                code = content.code, execution_count = executionCount
            });

            // preprocess
            var newCode = preprocessCode(content.code);

            if (!String.IsNullOrEmpty(newCode))
            {
                // evaluate
                try
                {
                    (var value, var errors) = Evaluation.EvalInteractionNonThrowing(newCode);

                    if (errors.Length > 0)
                    {
                        var sb = errors.Aggregate(new StringBuilder(), (ag, n) => ag.AppendLine(n));
                        sendError(sb.ToString(), msg);
                    }
                    var executeReply = new ExecuteReplyOk()
                    {
                        status           = "ok",
                        execution_count  = executionCount,
                        payload          = payload.ToArray(),
                        user_variables   = new Dictionary <string, object>(),
                        user_expressions = new Dictionary <string, object>()
                    };

                    sendMessage(shellSocket, msg, "execute_reply", executeReply);

                    // send all the data
                    if (!content.silent)
                    {
                        var lastExpression = Evaluation.GetLastExpression();

                        if (lastExpression != null)
                        {
                            var printedResult = Printers.PrintVariable(lastExpression);
                            sendDisplayData(printedResult.ContentType, printedResult.Data, "pyout");
                        }
                    }
                }
                catch (AggregateException ae)
                {
                    var esb = new StringBuilder();
                    esb.AppendLine(ae.Message);

                    foreach (var e in ae.InnerExceptions)
                    {
                        esb.AppendLine(e.Message);
                    }

                    sendError(esb.ToString(), msg);
                }
                catch (Exception ex)
                {
                    var error = $"Expression evaluation failed: {ex.Message}\r\n {ex.StackTrace}";
                    sendError(error, msg);
                }
            }

            if (Evaluation.sbPrint.Length > 0)
            {
                sendDisplayData("text/plain", Evaluation.sbPrint.ToString(), "display_data");
            }

            sendStateIdle(msg);
        }