예제 #1
0
        public void serialized_code_cells_have_appropriate_shape()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "//")
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"]
            .Should()
            .ContainSingleItem()
            .Which
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(
                                             new
            {
                cell_type       = "code",
                execution_count = 1,
                metadata        = new
                {
                    dotnet_interactive = new
                    {
                        language = "csharp"
                    }
                },
                source = new[]
                {
                    "//"
                },
                outputs = Array.Empty <object>()
            }
                                             )));
        }
예제 #2
0
        public void serialized_notebook_has_appropriate_metadata()
        {
            var notebook   = new NotebookDocument(Array.Empty <NotebookCell>());
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            using var _ = new AssertionScope();
            jupyter["metadata"]
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(new
            {
                kernelspec = new
                {
                    display_name = ".NET (C#)",
                    language     = "C#",
                    name         = ".net-csharp"
                },
                language_info = new
                {
                    file_extension = ".cs",
                    mimetype       = "text/x-csharp",
                    name           = "C#",
                    pygments_lexer = "csharp",
                    version        = "8.0"
                }
            })));
            jupyter["nbformat"]
            .ToObject <int>()
            .Should()
            .Be(4);
            jupyter["nbformat_minor"]
            .ToObject <int>()
            .Should()
            .Be(4);
        }
예제 #3
0
 public SerializeNotebook(string fileName, NotebookDocument notebook, string newLine, string targetKernelName = null)
     : base(targetKernelName)
 {
     FileName = fileName;
     Notebook = notebook;
     NewLine  = newLine;
 }
예제 #4
0
        public void serialized_markdown_cells_have_appropriate_shape()
        {
            var cells = new[]
            {
                new NotebookCell("markdown", "This is `markdown`.\nThis is more `markdown`.")
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"]
            .Should()
            .ContainSingleItem()
            .Which
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(
                                             new
            {
                cell_type = "markdown",
                metadata  = new { },
                source    = new[]
                {
                    "This is `markdown`.\n",
                    "This is more `markdown`.",
                }
            }
                                             )));
        }
예제 #5
0
        public void notebook_document_can_be_round_tripped_through_supported_file_formats(string extension, bool checkOutputs)
        {
            // not all notebook types retain outputs, so round-tripping them isn't necessarily interesting
            var outputs = checkOutputs
                ? new[]
            {
                new NotebookCellDisplayOutput(new Dictionary <string, object>()
                {
                    { "text/html", "This is html." }
                })
            }
                : Array.Empty <NotebookCellOutput>();
            var cells = new[]
            {
                new NotebookCell("csharp", "//", outputs)
            };
            var originalNotebook     = new NotebookDocument(cells);
            var fileName             = $"notebook{extension}";
            var content              = SerializeToString(fileName, originalNotebook);
            var roundTrippedNotebook = ParseFromString(fileName, content);

            roundTrippedNotebook
            .Should()
            .BeEquivalentToRespectingRuntimeTypes(originalNotebook);
        }
예제 #6
0
        public string SerializeToString(string fileName, NotebookDocument notebook, string newline = "\r\n")
        {
            var rawData = NotebookFileFormatHandler.Serialize(fileName, notebook, newline);
            var content = Encoding.UTF8.GetString(rawData);

            return(content);
        }
예제 #7
0
        public void text_cell_outputs_are_serialized()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "//", new[]
                {
                    new NotebookCellTextOutput("this is text")
                })
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"]
            .Should()
            .ContainSingleItem()
            .Which["outputs"]
            .Should()
            .ContainSingleItem()
            .Which
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(
                                             new
            {
                output_type = "stream",
                name        = "stdout",
                text        = "this is text"
            }
                                             )));
        }
예제 #8
0
        public void multiple_cells_are_serialized_with_appropriate_separators(string newline)
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "// C# line 1\n// C# line 2"),
                new NotebookCell("fsharp", "// F# line 1\n// F# line 2"),
                new NotebookCell("markdown", "This is `markdown`.")
            };
            var notebook      = new NotebookDocument(cells);
            var serialized    = SerializeToString("notebook.dib", notebook, newline);
            var expectedLines = new[]
            {
                "#!csharp",
                "",
                "// C# line 1",
                "// C# line 2",
                "",
                "#!fsharp",
                "",
                "// F# line 1",
                "// F# line 2",
                "",
                "#!markdown",
                "",
                "This is `markdown`.",
                ""
            };
            var expected = string.Join(newline, expectedLines);

            serialized
            .Should()
            .Be(expected);
        }
예제 #9
0
        public void error_cell_outputs_are_serialized()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "//", new[]
                {
                    new NotebookCellErrorOutput("e-name", "e-value", new[] { "at func1()", "at func2()" })
                })
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"]
            .Should()
            .ContainSingleItem()
            .Which["outputs"]
            .Should()
            .ContainSingleItem()
            .Which
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(
                                             new
            {
                output_type = "error",
                ename       = "e-name",
                evalue      = "e-value",
                traceback   = new[]
                {
                    "at func1()",
                    "at func2()"
                }
            }
                                             )));
        }
예제 #10
0
        public void serialized_code_cells_with_non_default_jupyter_kernel_language_have_language_metadata_and_no_language_specifier()
        {
            var cells = new[]
            {
                new NotebookCell("fsharp", "let x = 1")
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"][0]
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(new
            {
                cell_type       = "code",
                execution_count = 1,
                metadata        = new
                {
                    dotnet_interactive = new
                    {
                        language = "fsharp"
                    }
                },
                source = new[]
                {
                    "let x = 1"
                },
                outputs = new object[] { }
            })));
        }
        public async Task composite_kernel_can_serialize_notebooks()
        {
            using var kernel = CreateCompositeKernel();

            var notebook = new NotebookDocument(new[]
            {
                new NotebookCell("csharp", "var x = 1;")
            });

            await kernel.SendAsync(new SerializeNotebook("notebook.dib", notebook, "\r\n", ".NET"));

            var expectedLines = new[]
            {
                "#!csharp",
                "",
                "var x = 1;",
                ""
            };
            var expectedText = string.Join("\r\n", expectedLines);

            KernelEvents
            .Should()
            .ContainSingle <NotebookSerialized>()
            .Which
            .RawData
            .AsString()     // passing throught via this helper to make a test failure easier to identify
            .Should()
            .Be(expectedText);
        }
예제 #12
0
        public void serialized_code_cells_with_default_jupyter_kernel_language_dont_have_language_specifier()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "var x = 1;")
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"][0]["source"]
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(new object[]
            {
                "var x = 1;"
            })));
        }
예제 #13
0
        public void code_cells_with_multi_line_text_are_serialized_as_an_array()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "var x = 1;\nvar y = 2;")
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"][0]["source"]
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(new object[]
            {
                "var x = 1;\n",
                "var y = 2;"
            })));
        }
예제 #14
0
        public void serialize_entire_file_to_verify_indention()
        {
            var configuration = new Configuration()
                                .UsingExtension("json")
                                .SetInteractive(Debugger.IsAttached);
            var cells = new[]
            {
                new NotebookCell("csharp", "// this is csharp", new[]
                {
                    new NotebookCellDisplayOutput(new Dictionary <string, object>()
                    {
                        { "text/html", "this is html" }
                    })
                }),
                new NotebookCell("markdown", "This is `markdown`.")
            };
            var notebook = new NotebookDocument(cells);
            var json     = SerializeJupyter(notebook);

            this.Assent(json, configuration);
        }
예제 #15
0
        public void extra_blank_lines_are_removed_from_beginning_and_end_on_save()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "\n\n\n\n// this is csharp\n\n\n")
            };
            var notebook      = new NotebookDocument(cells);
            var serialized    = SerializeDib(notebook);
            var expectedLines = new[]
            {
                "#!csharp",
                "",
                "// this is csharp",
                ""
            };
            var expected = string.Join("\r\n", expectedLines);

            serialized
            .Should()
            .Be(expected);
        }
예제 #16
0
        public void notebook_can_be_serialized_to_different_extensions(string extension)
        {
            var fileName = $"notebook{extension}";
            var cells    = new[]
            {
                new NotebookCell("csharp", "// this is csharp")
            };
            var notebook      = new NotebookDocument(cells);
            var serialized    = SerializeToString(fileName, notebook);
            var expectedLines = new[]
            {
                "#!csharp",
                "",
                "// this is csharp",
                ""
            };
            var expected = string.Join("\r\n", expectedLines);

            serialized
            .Should()
            .Be(expected);
        }
예제 #17
0
        public void empty_cells_arent_serialized()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", ""),
                new NotebookCell("fsharp", "// this is fsharp"),
                new NotebookCell("csharp", "")
            };
            var notebook      = new NotebookDocument(cells);
            var serialized    = SerializeDib(notebook);
            var expectedLines = new[]
            {
                "#!fsharp",
                "",
                "// this is fsharp",
                ""
            };
            var expected = string.Join("\r\n", expectedLines);

            serialized
            .Should()
            .Be(expected);
        }
예제 #18
0
        public void rich_cell_outputs_are_serialized()
        {
            var cells = new[]
            {
                new NotebookCell("csharp", "//", new[]
                {
                    new NotebookCellDisplayOutput(new Dictionary <string, object>
                    {
                        { "text/html", "this is html" }
                    })
                })
            };
            var notebook   = new NotebookDocument(cells);
            var serialized = SerializeJupyter(notebook);
            var jupyter    = JToken.Parse(serialized);

            jupyter["cells"]
            .Should()
            .ContainSingleItem()
            .Which["outputs"]
            .Should()
            .ContainSingleItem()
            .Which
            .Should()
            .BeEquivalentTo(JToken.Parse(JsonConvert.SerializeObject(
                                             new
            {
                output_type = "execute_result",
                data        = new Dictionary <string, string>()
                {
                    { "text/html", "this is html" }
                },
                execution_count = 1,
                metadata        = new { }
            }
                                             )));
        }
예제 #19
0
 public string SerializeDib(NotebookDocument notebook)
 {
     return(SerializeToString("notebook.dib", notebook));
 }
예제 #20
0
 public string SerializeJupyter(NotebookDocument notebook)
 {
     return(SerializeToString("notebook.ipynb", notebook));
 }
예제 #21
0
 public NotebookParsed(NotebookDocument notebook, KernelCommand command = null)
     : base(command)
 {
     Notebook = notebook;
 }