public void PublishOutput(Message request, PublisherSocket ioPub, DisplayDataContent data)
        {
            var     content = new ExecuteResultPublishContent(data, _executionCount);
            Message message = new Message(MessageType.ExecuteResult, content, request.Header);

            this._logger.LogInformation(string.Format("Sending message to IOPub {0}", JsonConvert.SerializeObject(message)));
            ioPub.SendMessage(message);
        }
        public void HandleMessage(Message request, RouterSocket serverSocket, PublisherSocket ioPub)
        {
            _logger.LogDebug(string.Format("Message Content {0}", request.Content));
            ExecuteRequestContent executeRequest = request.Content as ExecuteRequestContent;

            _logger.LogInformation(string.Format("Execute Request received with code {0}", executeRequest.Code));

            // 1: Send Busy status on IOPub
            PublishStatus(request, ioPub, KernelState.Busy);

            // 2: Send execute input on IOPub
            PublishInput(request, ioPub, executeRequest.Code);

            // 3: Call the engine with the code
            string code    = executeRequest.Code;
            var    results = _replEngine.Execute(code);

            // 4: Send execute reply to shell socket
            // 5: Send execute result message to IOPub
            if (results.Error != null)
            {
                // SendExecuteErrorMessage(message, serverSocket, results.Error);
                PublishError(request, ioPub, results.Error);
            }
            else
            {
                SendExecuteReplyMessage(request, serverSocket);
                if (results.Output.Any())
                {
                    // 5: Send execute result message to IOPub
                    DisplayDataContent displayData = results.GetDisplayData();
                    PublishOutput(request, ioPub, displayData);
                }
            }

            // 6: Send IDLE status message to IOPub
            this.PublishStatus(request, ioPub, KernelState.Idle);

            // TODO: History
            // The Jupyter Notebook interface does not use history messages
            // However, we're supposed to be storing the history *with output*
            // So that a HistoryHandler can find it when asked
            if (executeRequest.StoreHistory)
            {
                this._executionCount += 1;
            }
        }
Beispiel #3
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var data          = new Dictionary <string, object>();
            var isJupyterData = false;

            // if they override the mimetype, force the issue
            if (!string.IsNullOrEmpty(MimeType))
            {
                isJupyterData = true;
                data.Add(MimeType, Normalize(InputObject.BaseObject, MimeType));
            }
            else
            {
                foreach (var property in InputObject.Properties)
                {
                    var name = property.Name.ToLower();
                    if (types.Keys.Contains(name))
                    {
                        isJupyterData = true;
                        data.Add(types[name], Normalize(property.Value, types[name]));
                    }
                    else if (name.Contains('/'))
                    {
                        isJupyterData = true;
                        data.Add(name, Normalize(property.Value, name));
                    }
                }
            }

            if (!isJupyterData)
            {
                if (InputObject.BaseObject is IDictionary dictionary)
                {
                    foreach (var property in dictionary.Keys)
                    {
                        var name = property.ToString().ToLower();
                        if (types.Keys.Contains(name))
                        {
                            isJupyterData = true;
                            data.Add(types[name], Normalize(dictionary[property], types[name]));
                        }
                        else if (name.Contains('/'))
                        {
                            isJupyterData = true;
                            data.Add(name, Normalize(dictionary[property], name));
                        }
                    }
                }
            }

            if (!isJupyterData)
            {
                if (InputObject.BaseObject is string)
                {
                    data.Add("text/plain", Normalize(InputObject.BaseObject, "text/plain"));
                }
                else
                {
                    data.Add("application/json", Normalize(InputObject.BaseObject, "application/json"));
                }
            }

            var content = new DisplayDataContent(data);

            if (Metadata != null)
            {
                foreach (var key in Metadata.Keys)
                {
                    content.MetaData.Add(key.ToString(), Metadata[key]);
                }
            }

            var type = MessageType.DisplayData;

            if (!string.IsNullOrEmpty(Id))
            {
                content.Transient.Add("display_id", Update);
            }
            if (!string.IsNullOrEmpty(Update))
            {
                type = MessageType.UpdateDisplayData;
                content.Transient.Add("display_id", Update);
            }

            var session = SessionState.PSVariable.GetValue("JupyterSession") as Session;

            Message message = new Message(type, content, new Header(type, null));

            session.PublishSocket.SendMessage(message);
        }