コード例 #1
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        self.blob = os.urandom(100)
        self.b64_blob = base64.encodestring(self.blob).decode('ascii')

        for d in (self.dirs + self.hidden_dirs):
            d.replace('/', os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace('/', os.sep)
            # create a notebook
            with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w',
                         encoding='utf-8') as f:
                nb = new_notebook()
                write(nb, f, version=4)

            # create a text file
            with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w',
                         encoding='utf-8') as f:
                f.write(self._txt_for_name(name))

            # create a binary file
            with io.open(pjoin(nbdir, d, '%s.blob' % name), 'wb') as f:
                f.write(self._blob_for_name(name))

        self.api = API(self.base_url())
コード例 #2
0
    def test_grade_existing_manual_grade(self, preprocessors, gradebook,
                                         resources):
        """Is a failing code cell correctly graded?"""
        cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        cell.source = "hello!"
        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 == None
        assert grade_cell.manual_score == None
        assert grade_cell.needs_manual_grade

        grade_cell.manual_score = 1
        grade_cell.needs_manual_grade = False
        gradebook.db.commit()

        preprocessors[1].preprocess(nb, resources)

        grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar")
        assert grade_cell.score == 1
        assert grade_cell.max_score == 1
        assert grade_cell.auto_score == None
        assert grade_cell.manual_score == 1
        assert grade_cell.needs_manual_grade
コード例 #3
0
    def test_modify_cell_with_submissions(self, preprocessor, resources):
        nb = new_notebook()
        nb.cells.append(
            create_grade_and_solution_cell("hello", "markdown", "foo", 2))
        nb, resources = preprocessor.preprocess(nb, resources)

        gb = preprocessor.gradebook
        notebook = gb.find_notebook("test", "ps0")
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        solution_cell = gb.find_solution_cell("foo", "test", "ps0")
        source_cell = gb.find_source_cell("foo", "test", "ps0")
        assert grade_cell.max_score == 2
        assert source_cell.source == "hello"

        gb.add_student("hacker123")
        submission = gb.add_submission("ps0", "hacker123").notebooks[0]
        assert len(notebook.submissions) == 1

        nb.cells[-1] = create_grade_and_solution_cell("goodbye", "markdown",
                                                      "foo", 1)
        nb, resources = preprocessor.preprocess(nb, resources)

        gb.db.refresh(notebook)
        gb.db.refresh(submission)
        gb.db.refresh(grade_cell)
        gb.db.refresh(solution_cell)
        gb.db.refresh(source_cell)
        assert len(notebook.submissions) == 1
        assert grade_cell.max_score == 1
        assert source_cell.source == "goodbye"
コード例 #4
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        self.blob = os.urandom(100)
        self.b64_blob = base64.encodestring(self.blob).decode("ascii")

        for d in self.dirs + self.hidden_dirs:
            d.replace("/", os.sep)
            if not os.path.isdir(pjoin(nbdir, d)):
                os.mkdir(pjoin(nbdir, d))

        for d, name in self.dirs_nbs:
            d = d.replace("/", os.sep)
            # create a notebook
            with io.open(pjoin(nbdir, d, "%s.ipynb" % name), "w", encoding="utf-8") as f:
                nb = new_notebook()
                write(nb, f, version=4)

            # create a text file
            with io.open(pjoin(nbdir, d, "%s.txt" % name), "w", encoding="utf-8") as f:
                f.write(self._txt_for_name(name))

            # create a binary file
            with io.open(pjoin(nbdir, d, "%s.blob" % name), "wb") as f:
                f.write(self._blob_for_name(name))

        self.api = API(self.base_url())
コード例 #5
0
    def test_grade_existing_manual_grade(self, preprocessors, gradebook, resources):
        """Is a failing code cell correctly graded?"""
        cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        cell.source = "hello!"
        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 == None
        assert grade_cell.manual_score == None
        assert grade_cell.needs_manual_grade

        grade_cell.manual_score = 1
        grade_cell.needs_manual_grade = False
        gradebook.db.commit()

        preprocessors[1].preprocess(nb, resources)

        grade_cell = gradebook.find_grade("foo", "test", "ps0", "bar")
        assert grade_cell.score == 1
        assert grade_cell.max_score == 1
        assert grade_cell.auto_score == None
        assert grade_cell.manual_score == 1
        assert grade_cell.needs_manual_grade
コード例 #6
0
ファイル: utils.py プロジェクト: pyucenter/py_ml_utils
 def create_notebook(self, cells, filename, start_notebook):
     nb = nbf.new_notebook()
     nb.cells = cells
     with open(filename, 'w') as f:
         f.write(nbf.writes(nb))
     call(['ipython', 'trust', filename])
     if start_notebook: call(['ipython', 'notebook', filename])
コード例 #7
0
    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')
コード例 #8
0
ファイル: manager.py プロジェクト: habi/ipython
 def new(self, model=None, path=''):
     """Create a new file or directory and return its model with no content.
     
     To create a new untitled entity in a directory, use `new_untitled`.
     """
     path = path.strip('/')
     if model is None:
         model = {}
     
     if path.endswith('.ipynb'):
         model.setdefault('type', 'notebook')
     else:
         model.setdefault('type', 'file')
     
     # no content, not a directory, so fill out new-file model
     if 'content' not in model and model['type'] != 'directory':
         if model['type'] == 'notebook':
             model['content'] = new_notebook()
             model['format'] = 'json'
         else:
             model['content'] = ''
             model['type'] = 'file'
             model['format'] = 'text'
     
     model = self.save(model, path)
     return model
コード例 #9
0
ファイル: manager.py プロジェクト: arvindchari88/newGitTest
    def new(self, model=None, path=""):
        """Create a new file or directory and return its model with no content.
        
        To create a new untitled entity in a directory, use `new_untitled`.
        """
        path = path.strip("/")
        if model is None:
            model = {}

        if path.endswith(".ipynb"):
            model.setdefault("type", "notebook")
        else:
            model.setdefault("type", "file")

        # no content, not a directory, so fill out new-file model
        if "content" not in model and model["type"] != "directory":
            if model["type"] == "notebook":
                model["content"] = new_notebook()
                model["format"] = "json"
            else:
                model["content"] = ""
                model["type"] = "file"
                model["format"] = "text"

        model = self.save(model, path)
        return model
コード例 #10
0
 def _empty_notebook(self, path):
     nb = new_notebook()
     full_dest = os.path.join(os.getcwd(), path)
     if not os.path.exists(os.path.dirname(full_dest)):
         os.makedirs(os.path.dirname(full_dest))
     with open(full_dest, 'w') as f:
         write_nb(nb, f, 4)
コード例 #11
0
ファイル: describe.py プロジェクト: eugeneyan/py_ml_utils
  def _create_notebook(self, cells, start_notebook):
    nb = nbf.new_notebook()
    nb.cells = cells
    with open('dataset_description.ipynb', 'w') as f: 
      f.write(nbf.writes(nb)) 

    if start_notebook:call(['ipython', 'notebook', 'dataset_description.ipynb'])
コード例 #12
0
    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())
コード例 #13
0
ファイル: test_savecells.py プロジェクト: c0ns0le/nbgrader
    def test_modify_cell_with_submissions(self, preprocessor, resources):
        nb = new_notebook()
        nb.cells.append(create_grade_and_solution_cell("hello", "markdown", "foo", 2))
        nb, resources = preprocessor.preprocess(nb, resources)

        gb = preprocessor.gradebook
        notebook = gb.find_notebook("test", "ps0")
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        solution_cell = gb.find_solution_cell("foo", "test", "ps0")
        source_cell = gb.find_source_cell("foo", "test", "ps0")
        assert grade_cell.max_score == 2
        assert source_cell.source == "hello"

        gb.add_student("hacker123")
        submission = gb.add_submission("ps0", "hacker123").notebooks[0]
        assert len(notebook.submissions) == 1

        nb.cells[-1] = create_grade_and_solution_cell("goodbye", "markdown", "foo", 1)
        nb, resources = preprocessor.preprocess(nb, resources)

        gb.db.refresh(notebook)
        gb.db.refresh(submission)
        gb.db.refresh(grade_cell)
        gb.db.refresh(solution_cell)
        gb.db.refresh(source_cell)
        assert len(notebook.submissions) == 1
        assert grade_cell.max_score == 1
        assert source_cell.source == "goodbye"
コード例 #14
0
    def test_very_long_cells(self):
        """
        Torture test that long cells do not cause issues
        """
        lorem_ipsum_text = textwrap.dedent("""\
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
          dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
          nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
          suscipit interdum at, ultrices sit amet orci. Mauris facilisis
          imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
          consequat malesuada neque sit amet pulvinar. Curabitur pretium
          ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
          volutpat, eu adipiscing purus pulvinar. Maecenas consequat
          luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
          erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
          at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
          Quisque at sapien non nibh feugiat tempus ac ultricies purus.
           """)
        lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n"
        large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)

        notebook_name = "lorem_ipsum_long.ipynb"
        nb = v4.new_notebook(
            cells=[
                    v4.new_markdown_cell(source=large_lorem_ipsum_text)
            ]
        )

        with TemporaryDirectory() as td:
            nbfile = os.path.join(td, notebook_name)
            with open(nbfile, 'w') as f:
                write(nb, f, 4)

            (output, resources) = LatexExporter(template_file='article').from_filename(nbfile)
            assert len(output) > 0
コード例 #15
0
ファイル: describe.py プロジェクト: LamDang/py_ml_utils
  def show(self):    
    nb = nbf.new_notebook()
    nb.cells = self.get_cells()
    with open('dataset_description.ipynb', 'w') as f: 
      f.write(nbf.writes(nb)) 

    call(['ipython', 'notebook', 'dataset_description.ipynb'])
コード例 #16
0
ファイル: base.py プロジェクト: gustavoasoares/nbgrader
 def _empty_notebook(self, path):
     nb = new_notebook()
     full_dest = os.path.join(os.getcwd(), path)
     if not os.path.exists(os.path.dirname(full_dest)):
         os.makedirs(os.path.dirname(full_dest))
     with open(full_dest, "w") as f:
         write_nb(nb, f, 4)
コード例 #17
0
ファイル: describe.py プロジェクト: LamDang/py_ml_utils
    def show(self):
        nb = nbf.new_notebook()
        nb.cells = self.get_cells()
        with open('dataset_description.ipynb', 'w') as f:
            f.write(nbf.writes(nb))

        call(['ipython', 'notebook', 'dataset_description.ipynb'])
コード例 #18
0
    def test_very_long_cells(self):
        """
        Torture test that long cells do not cause issues
        """
        lorem_ipsum_text = textwrap.dedent("""\
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
          dignissim, ipsum non facilisis tempus, dui felis tincidunt metus,
          nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus
          suscipit interdum at, ultrices sit amet orci. Mauris facilisis
          imperdiet elit, vitae scelerisque ipsum dignissim non. Integer
          consequat malesuada neque sit amet pulvinar. Curabitur pretium
          ut turpis eget aliquet. Maecenas sagittis lacus sed lectus
          volutpat, eu adipiscing purus pulvinar. Maecenas consequat
          luctus urna, eget cursus quam mollis a. Aliquam vitae ornare
          erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra
          at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat.
          Quisque at sapien non nibh feugiat tempus ac ultricies purus.
           """)
        lorem_ipsum_text = lorem_ipsum_text.replace("\n", " ") + "\n\n"
        large_lorem_ipsum_text = "".join([lorem_ipsum_text] * 3000)

        notebook_name = "lorem_ipsum_long.ipynb"
        nb = v4.new_notebook(
            cells=[v4.new_markdown_cell(source=large_lorem_ipsum_text)])

        with TemporaryDirectory() as td:
            nbfile = os.path.join(td, notebook_name)
            with open(nbfile, 'w') as f:
                write(nb, f, 4)

            (output, resources) = LatexExporter(
                template_file='article').from_filename(nbfile)
            assert len(output) > 0
コード例 #19
0
    def new(self, model=None, path=''):
        """Create a new file or directory and return its model with no content.
        
        To create a new untitled entity in a directory, use `new_untitled`.
        """
        path = path.strip('/')
        if model is None:
            model = {}

        if path.endswith('.ipynb'):
            model.setdefault('type', 'notebook')
        else:
            model.setdefault('type', 'file')

        # no content, not a directory, so fill out new-file model
        if 'content' not in model and model['type'] != 'directory':
            if model['type'] == 'notebook':
                model['content'] = new_notebook()
                model['format'] = 'json'
            else:
                model['content'] = ''
                model['type'] = 'file'
                model['format'] = 'text'

        model = self.save(model, path)
        return model
コード例 #20
0
ファイル: loader.py プロジェクト: parente/contentmanagement
 def create_rich_help_func(self):
     '''
     Build a help renderer with an attached __richdoc__ notebook.
     '''
     nb = nb_v4.new_notebook()
     f = rich_help()
     f.__richdoc__ = nb
     return f
コード例 #21
0
    def _create_notebook(self, cells, start_notebook):
        nb = nbf.new_notebook()
        nb.cells = cells
        with open('dataset_description.ipynb', 'w') as f:
            f.write(nbf.writes(nb))

        if start_notebook:
            call(['ipython', 'notebook', 'dataset_description.ipynb'])
コード例 #22
0
    def test_dont_overwrite_grade_and_solution_source(self):
        """Is the source not overwritten for grade+solution cells?"""
        cell = self._create_grade_and_solution_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)
コード例 #23
0
def lines_to_notebook(lines, name=None):
    """
    Convert the lines of an m file into an IPython notebook

    Parameters
    ----------
    lines : list
        A list of strings. Each element is a line in the m file

    Returns
    -------
    notebook : an IPython NotebookNode class instance, containing the
    information required to create a file


    """
    source = []
    md = np.empty(len(lines), dtype=object)
    new_cell = np.empty(len(lines), dtype=object)
    for idx, l in enumerate(lines):
        new_cell[idx], md[idx], this_source = format_line(l)
        # Transitions between markdown and code and vice-versa merit a new
        # cell, even if no newline, or "%%" is found. Make sure not to do this
        # check for the very first line!
        if idx > 1 and not new_cell[idx]:
            if md[idx] != md[idx - 1]:
                new_cell[idx] = True

        source.append(this_source)
    # This defines the breaking points between cells:
    new_cell_idx = np.hstack([np.where(new_cell)[0], -1])

    # Listify the sources:
    cell_source = [
        source[new_cell_idx[i]:new_cell_idx[i + 1]]
        for i in range(len(new_cell_idx) - 1)
    ]
    cell_md = [md[new_cell_idx[i]] for i in range(len(new_cell_idx) - 1)]
    cells = []

    # Append the notebook with loading matlab magic extension
    notebook_head = "import pymatbridge as pymat\n" + "ip = get_ipython()\n" \
                    + "pymat.load_ipython_extension(ip)"
    cells.append(nbformat.new_code_cell(notebook_head))  #, language='python'))

    for cell_idx, cell_s in enumerate(cell_source):
        if cell_md[cell_idx]:
            cells.append(nbformat.new_markdown_cell(cell_s))
        else:
            cell_s.insert(0, '%%matlab\n')
            cells.append(
                nbformat.new_code_cell(cell_s))  #, language='matlab'))

    #ws = nbformat.new_worksheet(cells=cells)
    notebook = nbformat.new_notebook(cells=cells)
    return notebook
コード例 #24
0
    def test_duplicate_locked_cell(self, preprocessor):
        cell1 = create_locked_cell("hello", "code", "foo")
        cell2 = create_locked_cell("goodbye", "code", "foo")
        nb = new_notebook()
        nb.cells.append(cell1)
        nb.cells.append(cell2)

        nb, resources = preprocessor.preprocess(nb, {})

        assert nb.cells[0].metadata.nbgrader == {}
        assert nb.cells[1].metadata.nbgrader != {}
コード例 #25
0
ファイル: test_html.py プロジェクト: arvindchari88/newGitTest
 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)
コード例 #26
0
    def test_duplicate_locked_cell(self, preprocessor):
        cell1 = create_locked_cell("hello", "code", "foo")
        cell2 = create_locked_cell("goodbye", "code", "foo")
        nb = new_notebook()
        nb.cells.append(cell1)
        nb.cells.append(cell2)

        nb, resources = preprocessor.preprocess(nb, {})

        assert nb.cells[0].metadata.nbgrader == {}
        assert nb.cells[1].metadata.nbgrader != {}
コード例 #27
0
    def test_comment_unchanged_code(self):
        """Is an unchanged code cell given the correct comment?"""
        cell = self._create_solution_cell("hello", "code")
        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)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, "No response.")
コード例 #28
0
    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)
コード例 #29
0
    def test_overwrite_points(self):
        """Are points overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.metadata.nbgrader["points"] = 2
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.metadata.nbgrader["points"], 1)
コード例 #30
0
    def test_overwrite_grade_cell_type(self):
        """Is the cell type overwritten for solution cells?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.cell_type = "markdown"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.cell_type, "code")
コード例 #31
0
    def test_overwrite_grade_checksum(self):
        """Is the checksum overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.metadata.nbgrader["checksum"] = "1234"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.metadata.nbgrader["checksum"], compute_checksum(cell))
コード例 #32
0
    def test_dont_overwrite_solution_source(self):
        """Is the source not overwritten for solution cells?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.source, "hello!")
コード例 #33
0
    def test_overwrite_grade_source(self):
        """Is the source overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.source, "hello")
コード例 #34
0
    def test_comment_unchanged_markdown(self, preprocessors, gradebook, resources):
        """Is an unchanged markdown cell given the correct comment?"""
        cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        preprocessors[1].preprocess(nb, resources)

        comment = gradebook.find_comment(0, "test", "ps0", "bar")
        assert comment.comment == "No response."
コード例 #35
0
def lines_to_notebook(lines, name=None):
    """
    Convert the lines of an m file into an IPython notebook

    Parameters
    ----------
    lines : list
        A list of strings. Each element is a line in the m file

    Returns
    -------
    notebook : an IPython NotebookNode class instance, containing the
    information required to create a file


    """
    source = []
    md = np.empty(len(lines), dtype=object)
    new_cell = np.empty(len(lines), dtype=object)
    for idx, l in enumerate(lines):
        new_cell[idx], md[idx], this_source = format_line(l)
        # Transitions between markdown and code and vice-versa merit a new
        # cell, even if no newline, or "%%" is found. Make sure not to do this
        # check for the very first line!
        if idx>1 and not new_cell[idx]:
            if md[idx] != md[idx-1]:
                new_cell[idx] = True

        source.append(this_source)
    # This defines the breaking points between cells:
    new_cell_idx = np.hstack([np.where(new_cell)[0], -1])

    # Listify the sources:
    cell_source = [source[new_cell_idx[i]:new_cell_idx[i+1]]
                   for i in range(len(new_cell_idx)-1)]
    cell_md = [md[new_cell_idx[i]] for i in range(len(new_cell_idx)-1)]
    cells = []

    # Append the notebook with loading matlab magic extension
    notebook_head = "import pymatbridge as pymat\n" + "ip = get_ipython()\n" \
                    + "pymat.load_ipython_extension(ip)"
    cells.append(nbformat.new_code_cell(notebook_head))#, language='python'))

    for cell_idx, cell_s in enumerate(cell_source):
        if cell_md[cell_idx]:
            cells.append(nbformat.new_markdown_cell(cell_s))
        else:
            cell_s.insert(0, '%%matlab\n')
            cells.append(nbformat.new_code_cell(cell_s))#, language='matlab'))

    #ws = nbformat.new_worksheet(cells=cells)
    notebook = nbformat.new_notebook(cells=cells)
    return notebook
コード例 #36
0
    def test_comment_changed_markdown(self):
        """Is a changed markdown cell given the correct comment?"""
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        cell.source = "hello!"
        self.preprocessor2.preprocess(nb, self.resources)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, None)
コード例 #37
0
    def test_overwrite_locked_checksum(self, preprocessors, resources):
        """Is the checksum overwritten for locked cells?"""
        cell = create_locked_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.metadata.nbgrader["checksum"] = "1234"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.metadata.nbgrader["checksum"] == compute_checksum(cell)
コード例 #38
0
    def test_overwrite_solution_cell_type(self, preprocessors, resources):
        """Is the cell type overwritten for solution cells?"""
        cell = create_solution_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.cell_type = "markdown"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.cell_type == "code"
コード例 #39
0
    def test_save_unchanged_code(self, preprocessors, gradebook, resources):
        """Is an unchanged code cell given the correct comment?"""
        cell = create_solution_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        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['comment'] == "No response."
コード例 #40
0
    def test_comment_changed_code(self, preprocessors, gradebook, resources):
        """Is a changed code cell given the correct comment?"""
        cell = create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        cell.source = "hello!"
        preprocessors[1].preprocess(nb, resources)

        comment = gradebook.find_comment(0, "test", "ps0", "bar")
        assert comment.comment == None
コード例 #41
0
    def test_overwrite_locked_source_markdown(self, preprocessors, resources):
        """Is the source overwritten for locked markdown cells?"""
        cell = create_locked_cell("hello", "markdown", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.source = "hello!"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.source == "hello"
コード例 #42
0
    def test_overwrite_points(self, preprocessors, resources):
        """Are points overwritten for grade cells?"""
        cell = create_grade_cell("hello", "code", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.metadata.nbgrader["points"] = 2
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.metadata.nbgrader["points"] == 1
コード例 #43
0
    def test_overwrite_locked_checksum(self, preprocessors, resources):
        """Is the checksum overwritten for locked cells?"""
        cell = create_locked_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.metadata.nbgrader["checksum"] = "1234"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.metadata.nbgrader["checksum"] == compute_checksum(cell)
コード例 #44
0
    def test_comment_unchanged_code(self, preprocessors, gradebook, resources):
        """Is an unchanged code cell given the correct comment?"""
        cell = create_solution_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        preprocessors[1].preprocess(nb, resources)

        comment = gradebook.find_comment("foo", "test", "ps0", "bar")
        assert comment.auto_comment == "No response."
コード例 #45
0
    def test_overwrite_locked_source_markdown(self, preprocessors, resources):
        """Is the source overwritten for locked markdown cells?"""
        cell = create_locked_cell("hello", "markdown", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.source = "hello!"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.source == "hello"
コード例 #46
0
    def test_overwrite_grade_source(self, preprocessors, resources):
        """Is the source overwritten for grade cells?"""
        cell = create_grade_cell("hello", "code", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.source = "hello!"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.source == "hello"
コード例 #47
0
    def test_dont_overwrite_grade_and_solution_source(self, preprocessors, resources):
        """Is the source not overwritten for grade+solution cells?"""
        cell = create_grade_and_solution_cell("hello", "code", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.source = "hello!"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.source == "hello!"
コード例 #48
0
    def test_overwrite_solution_cell_type(self, preprocessors, resources):
        """Is the cell type overwritten for solution cells?"""
        cell = create_solution_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = preprocessors[0].preprocess(nb, resources)

        cell.cell_type = "markdown"
        nb, resources = preprocessors[1].preprocess(nb, resources)

        assert cell.cell_type == "code"
コード例 #49
0
    def test_comment_unchanged_markdown(self, preprocessors, gradebook,
                                        resources):
        """Is an unchanged markdown cell given the correct comment?"""
        cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        preprocessors[1].preprocess(nb, resources)

        comment = gradebook.find_comment("foo", "test", "ps0", "bar")
        assert comment.auto_comment == "No response."
コード例 #50
0
    def test_comment_changed_code(self, preprocessors, gradebook, resources):
        """Is a changed code cell given the correct comment?"""
        cell = create_solution_cell("hello", "code", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)
        preprocessors[0].preprocess(nb, resources)
        gradebook.add_submission("ps0", "bar")
        cell.source = "hello!"
        preprocessors[1].preprocess(nb, resources)

        comment = gradebook.find_comment("foo", "test", "ps0", "bar")
        assert comment.auto_comment is None
コード例 #51
0
    def test_run_nb(self):
        """Test %run notebook.ipynb"""
        from IPython.nbformat import v4, writes
        nb = v4.new_notebook(cells=[
            v4.new_markdown_cell("The Ultimate Question of Everything"),
            v4.new_code_cell("answer=42")
        ])
        src = writes(nb, version=4)
        self.mktmp(src, ext='.ipynb')

        _ip.magic("run %s" % self.fname)

        nt.assert_equal(_ip.user_ns['answer'], 42)
コード例 #52
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        d = os.path.join(nbdir, 'foo')
        os.mkdir(d)

        with io.open(os.path.join(d, 'bar.ipynb'), 'w', encoding='utf-8') as f:
            nb = new_notebook()
            write(nb, f, version=4)

        with io.open(os.path.join(d, 'baz.txt'), 'w', encoding='utf-8') as f:
            f.write(u'flamingo')

        self.base_url()
コード例 #53
0
    def test_save_correct_code(self, preprocessors, gradebook, resources):
        """Is a passing code cell correctly graded?"""
        cell = create_grade_cell("hello", "code", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        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'] == 1
        assert cell.metadata.nbgrader['points'] == 1
        assert 'comment' not in cell.metadata.nbgrader
コード例 #54
0
    def test_save_locked_markdown_cell(self, preprocessor, resources):
        cell = create_locked_cell("hello", "markdown", "foo")
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = preprocessor.preprocess(nb, resources)

        gb = preprocessor.gradebook
        source_cell = gb.find_source_cell("foo", "test", "ps0")
        assert source_cell.source == "hello"
        assert source_cell.checksum == cell.metadata.nbgrader["checksum"]
        assert source_cell.cell_type == "markdown"
        assert source_cell.locked
コード例 #55
0
    def test_save_unchanged_markdown(self, preprocessors, gradebook, resources):
        """Is an unchanged markdown cell correctly graded?"""
        cell = create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        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 cell.metadata.nbgrader['comment'] == "No response."
コード例 #56
0
ファイル: test_html.py プロジェクト: hack1nt0/CM
 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)
コード例 #57
0
    def setUp(self):
        nbdir = self.notebook_dir.name
        try:
            os.mkdir(pjoin(nbdir, 'foo'))
        except OSError as e:
            # Deleting the folder in an earlier test may have failed
            if e.errno != errno.EEXIST:
                raise

        with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w',
                     encoding='utf-8') as f:
            nb = new_notebook()
            write(nb, f, version=4)

        self.sess_api = SessionAPI(self.base_url())
コード例 #58
0
    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
コード例 #59
0
    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)
コード例 #60
0
    def test_grade_correct_code(self, preprocessors, gradebook, resources):
        """Is a passing code cell correctly graded?"""
        cell = create_grade_cell("hello", "code", "foo", 1)
        cell.metadata.nbgrader['checksum'] = compute_checksum(cell)
        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 == 1
        assert grade_cell.max_score == 1
        assert grade_cell.auto_score == 1
        assert grade_cell.manual_score == None
        assert not grade_cell.needs_manual_grade