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>() } ))); }
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); }
public SerializeNotebook(string fileName, NotebookDocument notebook, string newLine, string targetKernelName = null) : base(targetKernelName) { FileName = fileName; Notebook = notebook; NewLine = newLine; }
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`.", } } ))); }
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); }
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); }
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" } ))); }
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); }
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()" } } ))); }
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); }
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;" }))); }
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;" }))); }
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); }
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); }
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); }
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); }
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 { } } ))); }
public string SerializeDib(NotebookDocument notebook) { return(SerializeToString("notebook.dib", notebook)); }
public string SerializeJupyter(NotebookDocument notebook) { return(SerializeToString("notebook.ipynb", notebook)); }
public NotebookParsed(NotebookDocument notebook, KernelCommand command = null) : base(command) { Notebook = notebook; }