Exemplo n.º 1
0
        public static NotebookParserServerResponse HandleRequest(NotebookParseOrSerializeRequest request)
        {
            try
            {
                switch (request)
                {
                case NotebookParseRequest parse:
                {
                    using var contentStream = new MemoryStream(parse.RawData);
                    var document = request.SerializationType switch
                    {
                        DocumentSerializationType.Dib => CodeSubmission.Read(contentStream, request.DefaultLanguage, WellKnownKernelNames),
                        DocumentSerializationType.Ipynb => Notebook.Read(contentStream, WellKnownKernelNames),
                        _ => throw new NotSupportedException($"Unable to parse an interactive document with type '{request.SerializationType }'"),
                    };
                    return(new NotebookParseResponse(request.Id, document));
                }

                case NotebookSerializeRequest serialize:
                {
                    using var resultStream = new MemoryStream();
                    switch (request.SerializationType)
                    {
                    case DocumentSerializationType.Dib:
                        CodeSubmission.Write(serialize.Document, serialize.NewLine, resultStream);
                        break;

                    case DocumentSerializationType.Ipynb:
                        Notebook.Write(serialize.Document, serialize.NewLine, resultStream);
                        break;

                    default:
                        throw new NotSupportedException($"Unable to serialize a interactive document of type '{request.SerializationType}'");
                    }

                    resultStream.Position = 0;
                    var resultArray = resultStream.ToArray();
                    return(new NotebookSerializeResponse(request.Id, resultArray));
                }

                default:
                    return(new NotebookErrorResponse(request.Id, $"Unsupported request: {request}"));
                }
            }
            catch (Exception ex)
            {
                return(new NotebookErrorResponse(request.Id, ex.ToString()));
            }
        }
Exemplo n.º 2
0
 public InteractiveDocument ParseDib(string content)
 {
     return(CodeSubmission.Parse(content, "csharp", KernelLanguageAliases));
 }