def test_kernel(): kernel_name = kernels.get_kernel_name("python") kernel = kernels.get_kernel(kernel_name) kernel.execute("a=1") assert kernel.execute("a")[0]["data"]["text/plain"] == "1" kernel.restart() with pytest.raises(NameError): kernel.execute("a") assert kernel is kernels["python"]
def test_no_kernel(): assert kernels.get_kernel_name("abc") == ""
def test_get_kernel_name(language): assert kernels.get_kernel_name( language) == kernels.kernel_names[language][0]
def test_kernel(): kernel_name = kernels.get_kernel_name("python") kernel = kernels.get_kernel(kernel_name) kernel.execute("a=1") assert kernel.execute("a")[0]["data"]["text/plain"] == "1"
from pheasant.renderers.jupyter.ipython import select_display_data from pheasant.renderers.jupyter.kernel import kernels kernel = kernels.get_kernel(kernels.get_kernel_name("python")) def test_import(): code = ("import holoviews as hv\n" "import matplotlib.pyplot as plt\n" "import pandas as pd\n" "import sympy as sp\n" "from bokeh.plotting import figure\n") assert kernel.execute(code) == [] def test_select_display_data_text(): outputs = kernel.execute("1") select_display_data(outputs) assert outputs == [{ "type": "execute_result", "data": { "text/plain": "1" }, "metadata": {} }] outputs = kernel.execute("print(1)") select_display_data(outputs) assert outputs == [{"type": "stream", "name": "stdout", "text": "1"}] # outputs = kernel.execute("1/0")
def execute_and_render(self, code, context, template) -> str: self.count += 1 cell = Cell(code, context, template) if len(self.cache) >= self.count: cached = self.cache[self.count - 1] if "freeze" in context["option"] or cell == cached: if self.page.path and (self.count - 1) % 5 == 0: relpath = os.path.relpath(self.page.path) self.progress_bar.progress(relpath, count=self.count) return surround(cached.output, "cached") elif self.page.path and self.config["safe"]: self.page.cache.delete() self.progress_bar.finish(done=False) raise CacheMismatchError if not self.config["enabled"]: report = {"count": self.count} return self.render(template, context, outputs=[], report=report) self.language = context.get("language", self.language) kernel_name = kernels.get_kernel_name(self.language) if not kernel_name: report = {"count": self.count} cell.output = self.render(template, context, outputs=[], report=report) self.update_cache(cell) return cell.output kernel = kernels.get_kernel(kernel_name) kernel.start(silent=self.page.path == "") if self.count == 1: self.progress_bar.progress("Start", count=self.count) kwargs = "" if self.language == "python": _, kwargs = split_kwargs_from_option(context["option"]) if kwargs: kernel.execute( "from pheasant.renderers.jupyter.ipython import formatter_kwargs\n" f"formatter_kwargs.update(dict({kwargs}))" ) verbose = self.config["verbose"] def execute(): if verbose >= 2: codes = [self.language + "> " + line for line in code.split("\n")] print("\n".join(codes)) func = kernel.inspect if "inspect" in context["option"] else kernel.execute outputs = func(code, output_hook=output_hook if verbose else None) report = format_report(kernel.report) report["count"] = self.count return outputs, report def format(result): relpath = os.path.relpath(self.page.path) return f"{relpath}({result[1]['total']})" outputs, report = self.progress_bar.progress(execute, format, self.count) if kwargs: kernel.execute( "from pheasant.renderers.jupyter.ipython import formatter_kwargs\n" "formatter_kwargs.clear()" ) cell.extra_module = get_extra_module(outputs) select_display_data(outputs) if "debug" in context["option"]: outputs = [{"type": "execute_result", "data": {"text/plain": outputs}}] elif "display-last" in context["option"]: select_last_display_data(outputs) elif template == "inline_code": select_outputs(outputs) else: latex_display_format(outputs) def not_system_exit(output): return output["type"] != "error" or output["ename"] != "SystemExit" outputs = list(takewhile(not_system_exit, outputs)) option = context["option"].split() code = context["code"].replace("\n\n\n", "\n\n") cell.output = self.render( template, context, code=code, option=option, kernel_name=kernel_name, outputs=outputs, report=report, ) self.update_cache(cell) return cell.output