コード例 #1
0
        public void TestReadingJpyterDocument2()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("01-Python Crash Course.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            Assert.IsTrue(document != null); //Phase success
        }
コード例 #2
0
        public IActionResult PreviewHtml(JupyterPreviewModel model)
        {
            if (string.IsNullOrEmpty(model.SelectedItem))
            {
                return(RedirectToAction("Index"));
            }

            var jupyterText = model.PreviewJson;

            if (!string.IsNullOrEmpty(jupyterText))
            {
                //convert to document
                var document = Jupyter.Parse(jupyterText);

                string htmlText = null;
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = new StreamWriter(memoryStream))
                    {
                        var htmlRenderer = new HtmlRenderer(writer)
                        {
                            RendererHeaderAndFooter = true,
                            CssText = GetCssFromSelection(model.SelectedItem)
                        };
                        htmlRenderer.Render(document);
                    }
                    htmlText = Encoding.UTF8.GetString(memoryStream.ToArray());
                }

                //preview
                return(Content(htmlText, "text/html", Encoding.UTF8));
            }

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public void TestReadingJpyterDocument1()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("Discover Sentiments in Tweets.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            Assert.IsTrue(document != null); //Phase success
        }
コード例 #4
0
        public void TestCodeCellExecuteOutput()
        {
            var jupyterText = @"{
  ""cells"": [
    {
        ""cell_type"": ""code"",
        ""execution_count"": 7,
        ""metadata"": {},
        ""outputs"": [
        {
            ""data"": {
                ""text/plain"": ""Hello""
            },
            ""execution_count"": 7,
            ""metadata"": {},
            ""output_type"": ""execute_result""
        }
        ],
        ""source"": [
        ""1 * 3""
        ]
    }]
}";
            var document    = Jupyter.Parse(jupyterText);
            var codeCell    = document.Cells.FirstOrDefault() as CodeCell;
            var codeOutput  = codeCell.Outputs.FirstOrDefault() as ExecuteResultOutput;

            Assert.AreEqual(OutputType.ExecuteResult, codeOutput.OutputType); //Type
            Assert.AreEqual(7, codeOutput.ExecutionCount);                    //ExecutionCount
            Assert.AreEqual(null, codeOutput.MetaData.Image);                 //MetaData
            Assert.AreEqual("Hello", codeOutput.Data.TextPlain.Text);         //Data
        }
コード例 #5
0
        public IActionResult DownloadToHtml(JupyterPreviewModel model)
        {
            var jupyterText = model.PreviewJson;

            if (!string.IsNullOrEmpty(jupyterText))
            {
                //convert to document
                var document = Jupyter.Parse(jupyterText);

                //convert to html
                string fileName  = "jupyterDocument.html";
                byte[] fileBytes = null;
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = new StreamWriter(memoryStream))
                    {
                        var htmlRenderer = new HtmlRenderer(writer)
                        {
                            RendererHeaderAndFooter = true
                        };
                        htmlRenderer.Render(document);
                    }
                    fileBytes = memoryStream.ToArray();
                }

                //diwnload
                return(File(fileBytes, "application/force-download", fileName));
            }

            return(RedirectToAction("Index"));
        }
コード例 #6
0
        public void TestMarkdownCell()
        {
            var jupyterText  = @"{
  ""cells"": [
    {
      ""cell_type"": ""markdown"",
      ""metadata"": {
        
      },
      ""source"": [
        ""___\n"",
        ""\n"",
        ""<a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>\n"",
        ""___\n"",
        ""# Python Crash Course\n"",
      ]
    }]
}";
            var document     = Jupyter.Parse(jupyterText);
            var markdownCell = document.Cells.FirstOrDefault() as MarkdownCell;

            Assert.AreEqual(CellType.Markdown, markdownCell.CellType);                    //Type
            Assert.AreEqual(typeof(JObject), markdownCell.Metadata.GetType());            //Metadata
            Assert.AreEqual(4, markdownCell.MarkdownDocument.Count);                      //Text
            Assert.IsTrue(markdownCell.MarkdownDocument.LastOrDefault() is HeadingBlock); //Text
        }
コード例 #7
0
        public void TestCodeCellStreamOutput()
        {
            var jupyterText  = @"{
  ""cells"": [
    {
   ""cell_type"": ""code"",
   ""execution_count"": 22,
   ""metadata"": {},
   ""outputs"": [
    {
     ""name"": ""stdout"",
     ""output_type"": ""stream"",
     ""text"": [
      ""hello\n""
     ]
    }
   ],
   ""source"": [
    ""print(x)""
   ]
  }
 ]
}";
            var document     = Jupyter.Parse(jupyterText);
            var codeCell     = document.Cells.FirstOrDefault() as CodeCell;
            var streamOutput = codeCell.Outputs.FirstOrDefault() as StreamOutput;

            Assert.AreEqual(OutputType.Stream, streamOutput.OutputType); //Type
            Assert.AreEqual("stdout", streamOutput.Name);                //Name
            Assert.AreEqual(1, streamOutput.Text.Count());               //Text
            Assert.AreEqual("hello", streamOutput.Text.LastOrDefault()); //Text
        }
コード例 #8
0
        public void TestCodeCell()
        {
            var jupyterText = @"{
  ""cells"": [
    {
        ""cell_type"": ""code"",
        ""execution_count"": 7,
        ""metadata"": {},
        ""outputs"": [
        {
            ""data"": {
                ""text/plain"": [
                ""3""
                ]
            },
            ""execution_count"": 7,
            ""metadata"": {},
            ""output_type"": ""execute_result""
        }
        ],
        ""source"": [
        ""1 * 3""
        ]
    }]
}";
            var document    = Jupyter.Parse(jupyterText);
            var codeCell    = document.Cells.FirstOrDefault() as CodeCell;

            Assert.AreEqual(CellType.Code, codeCell.CellType);         //Type
            Assert.AreEqual(7, codeCell.ExecutionCount);               //ExecutionCount
            Assert.AreEqual(false, codeCell.MetaData.Collapsed);       //MetaData
            Assert.AreEqual(false, codeCell.MetaData.AutoScroll);      //MetaData
            Assert.AreEqual(1, codeCell.Source.Count());               //Source
            Assert.AreEqual("1 * 3", codeCell.Source.LastOrDefault()); //Source
        }
コード例 #9
0
        public void TestReadingJpyterDocument2()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("01-Python Crash Course.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            //convert to html and open
            ConvertDocumentToHtmlFile(document, "01-Python Crash Course.pdf", false);
        }
コード例 #10
0
        public void TestReadingJpyterDocument1()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("Discover Sentiments in Tweets.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            //convert to html and open
            ConvertDocumentToHtmlFile(document, "Discover Sentiments in Tweets.pdf", false);
        }
コード例 #11
0
        public void TestMetadata()
        {
            var jupyterText  = @"{
  ""metadata"": {
  ""kernelspec"": {
   ""display_name"": ""Python 3"",
   ""language"": ""python"",
   ""name"": ""python3""
  },
  ""language_info"": {
   ""codemirror_mode"": {
    ""name"": ""ipython"",
    ""version"": 3
   },
   ""file_extension"": "".py"",
   ""mimetype"": ""text/x-python"",
   ""name"": ""python"",
   ""nbconvert_exporter"": ""python"",
   ""pygments_lexer"": ""ipython3"",
   ""version"": ""3.6.2""
  }
 },
 ""nbformat"": 4,
 ""nbformat_minor"": 1
}";
            var document     = Jupyter.Parse(jupyterText);
            var kernelSpec   = document.Metadata.KernelSpec;
            var kernelInfo   = document.Metadata.KernelInfo;
            var languageInfo = document.Metadata.LanguageInfo;

            //Spec
            Assert.AreEqual("Python 3", kernelSpec.DisplayName);
            Assert.AreEqual("python", kernelSpec.Language);
            Assert.AreEqual("python3", kernelSpec.Name);

            //KernelInfo
            Assert.AreEqual((object)null, kernelInfo.Name);

            //LanguageInfo
            Assert.AreEqual("ipython", languageInfo.CodemirrorMode.Name);
            Assert.AreEqual("3", languageInfo.CodemirrorMode.Version);

            Assert.AreEqual(".py", languageInfo.FileExtension);
            Assert.AreEqual("text/x-python", languageInfo.MimeType);
            Assert.AreEqual("python", languageInfo.Name);
            Assert.AreEqual("python", languageInfo.NbconvertExporter);
            Assert.AreEqual("ipython3", languageInfo.PygmentsLexer);
            Assert.AreEqual("3.6.2", languageInfo.Version);

            //NbFormat
            Assert.AreEqual(4, document.NbFormat);

            //NbFormatMinor
            Assert.AreEqual(1, document.NbFormatMinor);
        }
コード例 #12
0
        public IActionResult Preview(IFormFile file)
        {
            if (file == null)
            {
                ModelState.AddModelError("Error!", "File is empty.");
                return(StatusCode(400));
            }

            if (!file.FileName.EndsWith(".ipynb"))
            {
                ModelState.AddModelError("Error!", "Upload Jupyter document file.");
                return(StatusCode(400));
            }

            try
            {
                //get jupyter document raw text
                var jupyterString = string.Empty;
                using (var reader = new StreamReader(file.OpenReadStream()))
                {
                    jupyterString = reader.ReadToEnd();
                }

                //convert to document
                var document = Jupyter.Parse(jupyterString);

                //Html text
                var htmlText = "";
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = new StreamWriter(memoryStream))
                    {
                        var htmlRenderer = new HtmlRenderer(writer);
                        htmlRenderer.Render(document);
                        writer.Flush();
                    }
                    htmlText = Encoding.UTF8.GetString(memoryStream.ToArray());
                }

                //return view
                return(View(new JupyterPreviewModel
                {
                    StyleSelections = SelectListItemHelpers.EnumDropDownList(JupyterStyle.Chesterish),
                    PreviewHtml = htmlText,
                    PreviewJson = jupyterString
                }));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                ModelState.AddModelError("Error!", "Error occurred while pharsing the file");
                return(RedirectToAction("Index"));
            }
        }
コード例 #13
0
        public void TestReadingJpyterDocument2()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("01-Python Crash Course.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            //convert to json
            var documentJson = ConvertDocumentToJson(document);

            //convert to document again
            var document2 = Jupyter.Parse(documentJson);

            //Two class's property should be equal
            Assert.IsTrue(CompareHelper.CompareWithJsonFormat(document, document2));
        }
コード例 #14
0
        public void TestReadingJpyterDocument1()
        {
            var jupyterText = JupyterDocumentHelper.GetFileStringByFileName("Discover Sentiments in Tweets.ipynb");
            var document    = Jupyter.Parse(jupyterText);

            //convert to json
            var documentJson = ConvertDocumentToJson(document);

            //convert to document again
            var document2 = Jupyter.Parse(documentJson);

            //Two class's property should be equal
            Assert.IsTrue(CompareHelper.CompareWithJsonFormat(document, document2));

            //Change name
            document2.Metadata.KernelInfo.Name = "AAAA";

            //Should not equal
            Assert.IsFalse(CompareHelper.CompareWithJsonFormat(document, document2));
        }
コード例 #15
0
        public void TestCodeCellErrorOutput()
        {
            var jupyterText  = @"{
  ""cells"": [
    {
   ""cell_type"": ""code"",
   ""execution_count"": 44,
   ""metadata"": {},
   ""outputs"": [
    {
     ""ename"": ""TypeError"",
     ""evalue"": ""'tuple' object does not support item assignment"",
     ""output_type"": ""error"",
     ""traceback"": [
      ""\u001b[0;31m---------------------------------------------------------------------------\u001b[0m"",
      ""\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)"",
      ""\u001b[0;32m<ipython-input-44-97e4e33b36c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'NEW'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m"",
      ""\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment""
     ]
    }
   ],
   ""source"": [
    ""t[0] = 'NEW'""
   ]
  }
 ]
}";
            var documentText = Jupyter.Parse(jupyterText);
            var codeCell     = documentText.Cells.FirstOrDefault() as CodeCell;
            var errorOutput  = codeCell.Outputs.FirstOrDefault() as ErrorOutput;

            Assert.AreEqual(OutputType.Error, errorOutput.OutputType);                              //Type
            Assert.AreEqual("TypeError", errorOutput.Ename);                                        //Ename
            Assert.AreEqual("'tuple' object does not support item assignment", errorOutput.Evalue); //Evalue
            Assert.AreEqual(4, errorOutput.Traceback.Count());                                      //Data
            Assert.AreEqual(true,
                            errorOutput.Traceback.LastOrDefault()
                            .Contains("'tuple' object does not support item assignment")); //Source
        }
コード例 #16
0
        public void TestCodeCellDisplayDataOutput()
        {
            var jupyterText       = @"{
  ""cells"": [
    {
        ""cell_type"": ""code"",
        ""execution_count"": 7,
        ""metadata"": {
            ""scrolled"": true,
            ""collapsed"": false
        },
        ""outputs"": [
        {
            ""output_type"": ""display_data"",
            ""data"": {
                ""image/png"": ""iVBORw0KGgoAAAANSUhEUgAAAT4AAAIXCAYAAAAFczJTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1..."",
                ""text/plain"": ""<matplotlib.figure.Figure at 0x7f0ce3512320>""
            },
            ""metadata"": {}
        }
        ],
        ""source"": [
        ""1 * 3""
        ]
    }]
}";
            var document          = Jupyter.Parse(jupyterText);
            var codeCell          = document.Cells.FirstOrDefault() as CodeCell;
            var displayDataOutput = codeCell.Outputs.FirstOrDefault() as DisplayDataOutput;

            Assert.AreEqual(OutputType.DisplayData, displayDataOutput.OutputType); //Type
            Assert.AreEqual(
                "iVBORw0KGgoAAAANSUhEUgAAAT4AAAIXCAYAAAAFczJTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1...",
                displayDataOutput.Data.ImageData);                   //Data
            Assert.AreEqual("<matplotlib.figure.Figure at 0x7f0ce3512320>",
                            displayDataOutput.Data.TextPlain.Text);  //Data
            Assert.AreEqual(null, displayDataOutput.MetaData.Image); //MetaData
        }
コード例 #17
0
        public IActionResult DownloadToPdf(JupyterPreviewModel model)
        {
            var jupyterText = model.PreviewJson;

            if (!string.IsNullOrEmpty(jupyterText))
            {
                //convert to document
                var document = Jupyter.Parse(jupyterText);

                //convert to html
                string fileName  = "jupyterDocument.pdf";
                byte[] fileBytes = null;
                using (var memoryStream = new MemoryStream())
                {
                    var htmlRenderer = new PdfRenderer(memoryStream);
                    htmlRenderer.Render(document);
                    fileBytes = memoryStream.ToArray();
                }

                return(File(fileBytes, "application/force-download", fileName));
            }

            return(RedirectToAction("Index"));
        }