def setUp(self): nbdir = self.notebook_dir.name if not os.path.isdir(pjoin(nbdir, 'foo')): os.mkdir(pjoin(nbdir, 'foo')) nb = new_notebook() nb.cells.append(new_markdown_cell(u'Created by test ³')) cc1 = new_code_cell(source=u'print(2*6)') cc1.outputs.append(new_output(output_type="stream", text=u'12')) cc1.outputs.append( new_output( output_type="execute_result", data={'image/png': png_green_pixel}, execution_count=1, )) nb.cells.append(cc1) with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w', encoding='utf-8') as f: write(nb, f, version=4) self.nbconvert_api = NbconvertAPI(self.base_url())
def test_determine_grade_code_grade(): cell = create_grade_cell('print("test")', "code", "foo", 10) cell.outputs = [] assert utils.determine_grade(cell) == (10, 10) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] assert utils.determine_grade(cell) == (0, 10)
def _add_error(self, cell): cell.outputs.append( new_output("error", ename="Error", evalue="oh noes, an error occurred!", traceback=["oh noes, an error occurred!"])) return cell
def test_determine_grade_code_grade_and_solution(self): cell = self._create_grade_and_solution_cell('test', "code", "foo", 10) cell.outputs = [] assert_equal(utils.determine_grade(cell), (10, 10)) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] assert_equal(utils.determine_grade(cell), (0, 10))
def _add_error(self, cell): cell.outputs.append(new_output( "error", ename="Error", evalue="oh noes, an error occurred!", traceback=["oh noes, an error occurred!"] )) return cell
def test_determine_grade_code_grade_and_solution(): cell = create_grade_and_solution_cell('test', "code", "foo", 10) cell.metadata.nbgrader['checksum'] = utils.compute_checksum(cell) cell.outputs = [] assert utils.determine_grade(cell) == (0, 10) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] cell.source = 'test!' assert utils.determine_grade(cell) == (None, 10)
def test_javascript_output(self): nb = v4.new_notebook(cells=[ v4.new_code_cell(outputs=[ v4.new_output( output_type='display_data', data={'application/javascript': "javascript_output();"}) ]) ]) (output, resources) = HTMLExporter( template_file='basic').from_notebook_node(nb) self.assertIn('javascript_output', output)
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [nbformat.new_output(output_type='display_data', data={'image/svg+xml':self.simple_svg}) ] cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)] return nbformat.new_notebook(cells=cells)
def setUp(self): nbdir = self.notebook_dir.name if not os.path.isdir(pjoin(nbdir, "foo")): os.mkdir(pjoin(nbdir, "foo")) nb = new_notebook() nb.cells.append(new_markdown_cell(u"Created by test ³")) cc1 = new_code_cell(source=u"print(2*6)") cc1.outputs.append(new_output(output_type="stream", text=u"12")) cc1.outputs.append( new_output(output_type="execute_result", data={"image/png": png_green_pixel}, execution_count=1) ) nb.cells.append(cc1) with io.open(pjoin(nbdir, "foo", "testnb.ipynb"), "w", encoding="utf-8") as f: write(nb, f, version=4) self.nbconvert_api = NbconvertAPI(self.base_url())
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [nbformat.new_output(output_type="image/svg+xml", output_svg=self.simple_svg)] slide_metadata = {'slideshow' : {'slide_type': 'slide'}} subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}} cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs)] return nbformat.new_notebook(cells=cells)
def test_coalesce_replace_streams(self): """Are \\r characters handled?""" outputs = [ nbformat.new_output(output_type="stream", name="stdout", text="z"), nbformat.new_output(output_type="stream", name="stdout", text="\ra"), nbformat.new_output(output_type="stream", name="stdout", text="\nz\rb"), nbformat.new_output(output_type="stream", name="stdout", text="\nz"), nbformat.new_output(output_type="stream", name="stdout", text="\rc\n"), nbformat.new_output(output_type="stream", name="stdout", text="z\rz\rd") ] cells = [ nbformat.new_code_cell(source="# None", execution_count=1, outputs=outputs) ] nb = nbformat.new_notebook(cells=cells) res = self.build_resources() nb, res = coalesce_streams(nb, res) outputs = nb.cells[0].outputs self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
def setUp(self): nbdir = self.notebook_dir.name if not os.path.isdir(pjoin(nbdir, 'foo')): os.mkdir(pjoin(nbdir, 'foo')) nb = new_notebook() nb.cells.append(new_markdown_cell(u'Created by test ³')) cc1 = new_code_cell(source=u'print(2*6)') cc1.outputs.append(new_output(output_type="stream", text=u'12')) cc1.outputs.append(new_output(output_type="execute_result", data={'image/png': png_green_pixel}, execution_count=1, )) nb.cells.append(cc1) with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w', encoding='utf-8') as f: write(nb, f, version=4) self.nbconvert_api = NbconvertAPI(self.base_url())
def test_save_incorrect_code(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) preprocessors[2].preprocess(nb, resources) assert cell.metadata.nbgrader['score'] == 0 assert cell.metadata.nbgrader['points'] == 1 assert 'comment' not in cell.metadata.nbgrader
def test_save_incorrect_code(self): """Is a failing code cell correctly graded?""" cell = self._create_grade_cell("hello", "code", "foo", 1) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) self.preprocessor1.preprocess(nb, self.resources) self.gb.add_submission("ps0", "bar") self.preprocessor2.preprocess(nb, self.resources) self.preprocessor3.preprocess(nb, self.resources) assert_equal(cell.metadata.nbgrader['score'], 0) assert_equal(cell.metadata.nbgrader['points'], 1) assert 'comment' not in cell.metadata.nbgrader
def test_save_incorrect_code(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) preprocessors[2].preprocess(nb, resources) assert cell.metadata.nbgrader['score'] == 0 assert cell.metadata.nbgrader['points'] == 1 assert 'comment' not in cell.metadata.nbgrader
def test_coalesce_sequenced_streams(self): """Can the coalesce streams preprocessor merge a sequence of streams?""" outputs = [nbformat.new_output(output_type="stream", name="stdout", text="0"), nbformat.new_output(output_type="stream", name="stdout", text="1"), nbformat.new_output(output_type="stream", name="stdout", text="2"), nbformat.new_output(output_type="stream", name="stdout", text="3"), nbformat.new_output(output_type="stream", name="stdout", text="4"), nbformat.new_output(output_type="stream", name="stdout", text="5"), nbformat.new_output(output_type="stream", name="stdout", text="6"), nbformat.new_output(output_type="stream", name="stdout", text="7")] cells=[nbformat.new_code_cell(source="# None", execution_count=1,outputs=outputs)] nb = nbformat.new_notebook(cells=cells) res = self.build_resources() nb, res = coalesce_streams(nb, res) outputs = nb.cells[0].outputs self.assertEqual(outputs[0].text, u'01234567')
def test_javascript_output(self): nb = v4.new_notebook( cells=[ v4.new_code_cell( outputs=[v4.new_output( output_type='display_data', data={ 'application/javascript': "javascript_output();" } )] ) ] ) (output, resources) = HTMLExporter(template_file='basic').from_notebook_node(nb) self.assertIn('javascript_output', output)
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [ nbformat.new_output(output_type='display_data', data={'image/svg+xml': self.simple_svg}) ] cells = [ nbformat.new_code_cell(source="", execution_count=1, outputs=outputs) ] return nbformat.new_notebook(cells=cells)
def test_grade_incorrect_code(self): """Is a failing code cell correctly graded?""" cell = self._create_grade_cell("hello", "code", "foo", 1) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) self.preprocessor1.preprocess(nb, self.resources) self.gb.add_submission("ps0", "bar") self.preprocessor2.preprocess(nb, self.resources) grade_cell = self.gb.find_grade("foo", "test", "ps0", "bar") assert_equal(grade_cell.score, 0) assert_equal(grade_cell.max_score, 1) assert_equal(grade_cell.auto_score, 0) assert_equal(grade_cell.manual_score, None) assert not grade_cell.needs_manual_grade
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [nbformat.new_output(output_type="stream", name="stdout", text="a")] slide_metadata = {'slideshow' : {'slide_type': 'slide'}} subslide_metadata = {'slideshow' : {'slide_type': 'subslide'}} cells=[nbformat.new_code_cell(source="", execution_count=1, outputs=outputs), nbformat.new_markdown_cell(source="", metadata=slide_metadata), nbformat.new_code_cell(source="", execution_count=2, outputs=outputs), nbformat.new_markdown_cell(source="", metadata=slide_metadata), nbformat.new_markdown_cell(source="", metadata=subslide_metadata)] return nbformat.new_notebook(cells=cells)
def test_grade_incorrect_code(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])] nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 0 assert grade_cell.max_score == 1 assert grade_cell.auto_score == 0 assert grade_cell.manual_score == None assert not grade_cell.needs_manual_grade
def build_notebook(self): """Build a notebook in memory for use with preprocessor tests""" outputs = [ nbformat.new_output("stream", name="stdout", text="a"), nbformat.new_output("display_data", data={'text/plain': 'b'}), nbformat.new_output("stream", name="stdout", text="c"), nbformat.new_output("stream", name="stdout", text="d"), nbformat.new_output("stream", name="stderr", text="e"), nbformat.new_output("stream", name="stderr", text="f"), nbformat.new_output("display_data", data={'image/png': 'Zw=='}), # g nbformat.new_output("display_data", data={'application/pdf': 'aA=='}), # h ] cells=[nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs), nbformat.new_markdown_cell(source="$ e $")] return nbformat.new_notebook(cells=cells)
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [ nbformat.new_output(output_type="image/svg+xml", output_svg=self.simple_svg) ] slide_metadata = {'slideshow': {'slide_type': 'slide'}} subslide_metadata = {'slideshow': {'slide_type': 'subslide'}} cells = [ nbformat.new_code_cell(source="", execution_count=1, outputs=outputs) ] return nbformat.new_notebook(cells=cells)
def test_contents_manager(self): "make sure ContentsManager returns right files (ipynb, bin, txt)." nbdir = self.notebook_dir.name base = self.base_url() nb = new_notebook( cells=[ new_markdown_cell(u'Created by test ³'), new_code_cell("print(2*6)", outputs=[ new_output("stream", text="12"), ]) ] ) with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', encoding='utf-8') as f: write(nb, f, version=4) with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f: f.write(b'\xff' + os.urandom(5)) f.close() with io.open(pjoin(nbdir, 'test.txt'), 'w') as f: f.write(u'foobar') f.close() r = requests.get(url_path_join(base, 'files', 'testnb.ipynb')) self.assertEqual(r.status_code, 200) self.assertIn('print(2*6)', r.text) json.loads(r.text) r = requests.get(url_path_join(base, 'files', 'test.bin')) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/octet-stream') self.assertEqual(r.content[:1], b'\xff') self.assertEqual(len(r.content), 6) r = requests.get(url_path_join(base, 'files', 'test.txt')) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'text/plain') self.assertEqual(r.text, 'foobar')
def test_coalesce_replace_streams(self): """Are \\r characters handled?""" outputs = [nbformat.new_output(output_type="stream", name="stdout", text="z"), nbformat.new_output(output_type="stream", name="stdout", text="\ra"), nbformat.new_output(output_type="stream", name="stdout", text="\nz\rb"), nbformat.new_output(output_type="stream", name="stdout", text="\nz"), nbformat.new_output(output_type="stream", name="stdout", text="\rc\n"), nbformat.new_output(output_type="stream", name="stdout", text="z\rz\rd")] cells=[nbformat.new_code_cell(source="# None", execution_count=1,outputs=outputs)] nb = nbformat.new_notebook(cells=cells) res = self.build_resources() nb, res = coalesce_streams(nb, res) outputs = nb.cells[0].outputs self.assertEqual(outputs[0].text, u'a\nb\nc\nd')
def test_determine_grade(self): cell = self._create_code_cell() cell.metadata['nbgrader'] = {} cell.metadata['nbgrader']['grade'] = True cell.metadata['nbgrader']['points'] = 10 cell.outputs = [] assert utils.determine_grade(cell) == (10, 10) cell.outputs = [ new_output('error', ename="NotImplementedError", evalue="", traceback=["error"]) ] assert utils.determine_grade(cell) == (0, 10) cell = self._create_text_cell() cell.metadata['nbgrader'] = {} cell.metadata['nbgrader']['grade'] = True cell.metadata['nbgrader']['points'] = 10 assert utils.determine_grade(cell) == (None, 10)
def test_coalesce_sequenced_streams(self): """Can the coalesce streams preprocessor merge a sequence of streams?""" outputs = [ nbformat.new_output(output_type="stream", name="stdout", text="0"), nbformat.new_output(output_type="stream", name="stdout", text="1"), nbformat.new_output(output_type="stream", name="stdout", text="2"), nbformat.new_output(output_type="stream", name="stdout", text="3"), nbformat.new_output(output_type="stream", name="stdout", text="4"), nbformat.new_output(output_type="stream", name="stdout", text="5"), nbformat.new_output(output_type="stream", name="stdout", text="6"), nbformat.new_output(output_type="stream", name="stdout", text="7") ] cells = [ nbformat.new_code_cell(source="# None", execution_count=1, outputs=outputs) ] nb = nbformat.new_notebook(cells=cells) res = self.build_resources() nb, res = coalesce_streams(nb, res) outputs = nb.cells[0].outputs self.assertEqual(outputs[0].text, u'01234567')
def test_grade_incorrect_code(self, preprocessors, gradebook, resources): """Is a failing code cell correctly graded?""" cell = create_grade_cell("hello", "code", "foo", 1) cell.metadata.nbgrader['checksum'] = compute_checksum(cell) cell.outputs = [ new_output('error', ename="NotImplementedError", evalue="", traceback=["error"]) ] nb = new_notebook() nb.cells.append(cell) preprocessors[0].preprocess(nb, resources) gradebook.add_submission("ps0", "bar") preprocessors[1].preprocess(nb, resources) grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar") assert grade_cell.score == 0 assert grade_cell.max_score == 1 assert grade_cell.auto_score == 0 assert grade_cell.manual_score == None assert not grade_cell.needs_manual_grade
def build_notebook(self): """Build a notebook in memory for use with preprocessor tests""" outputs = [ nbformat.new_output("stream", name="stdout", text="a"), nbformat.new_output("display_data", data={'text/plain': 'b'}), nbformat.new_output("stream", name="stdout", text="c"), nbformat.new_output("stream", name="stdout", text="d"), nbformat.new_output("stream", name="stderr", text="e"), nbformat.new_output("stream", name="stderr", text="f"), nbformat.new_output("display_data", data={'image/png': 'Zw=='}), # g nbformat.new_output("display_data", data={'application/pdf': 'aA=='}), # h ] cells = [ nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs), nbformat.new_markdown_cell(source="$ e $") ] return nbformat.new_notebook(cells=cells)
def build_notebook(self): """Build a reveal slides notebook in memory for use with tests. Overrides base in PreprocessorTestsBase""" outputs = [ nbformat.new_output(output_type="stream", name="stdout", text="a") ] slide_metadata = {'slideshow': {'slide_type': 'slide'}} subslide_metadata = {'slideshow': {'slide_type': 'subslide'}} cells = [ nbformat.new_code_cell(source="", execution_count=1, outputs=outputs), nbformat.new_markdown_cell(source="", metadata=slide_metadata), nbformat.new_code_cell(source="", execution_count=2, outputs=outputs), nbformat.new_markdown_cell(source="", metadata=slide_metadata), nbformat.new_markdown_cell(source="", metadata=subslide_metadata) ] return nbformat.new_notebook(cells=cells)
def add_code_cell(self, nb): output = nbformat.new_output( "display_data", {'application/javascript': "alert('hi');"}) cell = nbformat.new_code_cell("print('hi')", outputs=[output]) nb.cells.append(cell)
def add_code_cell(self, nb): output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"}) cell = nbformat.new_code_cell("print('hi')", outputs=[output]) nb.cells.append(cell)
import base64, os, re, sys import IPython.nbformat.v4 as nbf filename = os.path.splitext(sys.argv[1])[0] try: title, description = open("{}.txt".format(filename), encoding="utf-8").read().split('\n\n', 1) except IOError: title, description = filename, "" description = description.replace("...", "").replace("'''", "**").replace("''", "*") bendpattern = re.compile("^!+", re.MULTILINE) bendcode = '<img src="http://pyx.sourceforge.net/bend.png" align="left">' description = re.sub(bendpattern, lambda m: bendcode*(m.end()-m.start()), description) code = open("{}.py".format(filename), encoding="utf-8").read() code = re.sub('\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)\n.*writeSVGfile\(("[a-z]+")?\)\n', "", code) nb = nbf.new_notebook() cells = [] cells.append(nbf.new_markdown_cell(source="# " + title)) cells.append(nbf.new_code_cell(source=code, execution_count=1, outputs=[nbf.new_output(output_type=u'execute_result', execution_count=1, data={'image/png': base64.encodebytes(open("{}.png".format(filename), "rb").read()).decode("ascii"), 'image/svg+xml': open("{}.svg".format(filename), "r", encoding="utf-8").read()})])) cells.append(nbf.new_markdown_cell(source=description)) nb = nbf.new_notebook(cells=cells, metadata={'language': 'python'}) open("{}.ipynb".format(filename), "w").write(nbf.writes(nb))
description) code = open("{}.py".format(filename), encoding="utf-8").read() code = re.sub( '\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)\n.*writeSVGfile\(("[a-z]+")?\)\n', "", code) nb = nbf.new_notebook() cells = [] cells.append(nbf.new_markdown_cell(source="# " + title)) cells.append( nbf.new_code_cell(source=code, execution_count=1, outputs=[ nbf.new_output( output_type=u'execute_result', execution_count=1, data={ 'image/png': base64.encodebytes( open("{}.png".format(filename), "rb").read()).decode("ascii"), 'image/svg+xml': open("{}.svg".format(filename), "r", encoding="utf-8").read() }) ])) cells.append(nbf.new_markdown_cell(source=description)) nb = nbf.new_notebook(cells=cells, metadata={'language': 'python'}) open("{}.ipynb".format(filename), "w").write(nbf.writes(nb))