Skip to content

Commit

Permalink
Merge pull request #643 from jhamrick/metadata
Browse files Browse the repository at this point in the history
Add command to update metadata
  • Loading branch information
jhamrick committed Jan 14, 2017
2 parents 4f25791 + 4aa5e4b commit 88c3b2c
Show file tree
Hide file tree
Showing 50 changed files with 1,537 additions and 1,393 deletions.
2 changes: 2 additions & 0 deletions nbgrader/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DbApp, DbStudentApp, DbAssignmentApp,
DbStudentAddApp, DbStudentRemoveApp, DbStudentImportApp, DbStudentListApp,
DbAssignmentAddApp, DbAssignmentRemoveApp, DbAssignmentImportApp, DbAssignmentListApp)
from .updateapp import UpdateApp
from .nbgraderapp import NbGraderApp


Expand Down Expand Up @@ -48,4 +49,5 @@
'DbAssignmentImportApp',
'DbAssignmentRemoveApp',
'DbAssignmentListApp',
'UpdateApp'
]
11 changes: 10 additions & 1 deletion nbgrader/apps/nbgraderapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
ExtensionApp,
QuickStartApp,
ExportApp,
DbApp
DbApp,
UpdateApp
)

aliases = {}
Expand Down Expand Up @@ -211,6 +212,14 @@ class NbGraderApp(NbGrader):
removing, importing, and listing assignments or students.
"""
).strip()
),
update=(
UpdateApp,
dedent(
"""
Update nbgrader cell metadata to the most recent version.
"""
).strip()
)
)

Expand Down
82 changes: 82 additions & 0 deletions nbgrader/apps/updateapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os
import traceback

from nbformat import current_nbformat, read as orig_read, write as orig_write
from traitlets import Bool

from .baseapp import NbGrader
from ..nbformat import Validator, write, ValidationError
from ..utils import find_all_notebooks

aliases = {
'log-level': 'Application.log_level',
}
flags = {}


class UpdateApp(NbGrader):

name = u'nbgrader-update'
description = u'Update nbgrader notebook metadata'

aliases = aliases
flags = flags

validate = Bool(True, help="whether to validate metadata after updating it").tag(config=True)

examples = """
nbgrader stores metadata in the JSON source of the notebooks. Previously,
we did not do a good job of validating whether this metadata was
correctly formatted or not. Starting in version 0.4.0 of nbgrader, we
are explicitly validating this metadata. This will require that you
update the metadata in your old nbgrader notebooks to be consistent
with what nbgrader expects.
The `nbgrader update` command performs this metadata update for you
easily. All you need to do is point it at a directory, and it will
find all notebooks in that directory and update them to have the
correct metadata:
# update notebooks rooted in the current directory
nbgrader update .
# update notebooks rooted in the `class_files` directory
nbgrader update class_files/
Alternately, you can open all your notebooks with the "Create Assignment"
toolbar and re-save them from the notebook interface. But, it will be
more efficient to run the `nbgrader update` command to get them all in
one fell swoop.
"""

def start(self):
super(UpdateApp, self).start()

if len(self.extra_args) < 1:
self.fail("No notebooks given")

notebooks = set()
for name in self.extra_args:
if not os.path.exists(name):
self.fail("No such file or directory: {}".format(name))
elif os.path.isdir(name):
notebooks.update([os.path.join(name, x) for x in find_all_notebooks(name)])
elif not name.endswith(".ipynb"):
self.log.warn("{} is not a notebook, ignoring".format(name))
else:
notebooks.add(name)

notebooks = sorted(list(notebooks))
for notebook in notebooks:
self.log.info("Updating metadata for notebook: {}".format(notebook))
nb = orig_read(notebook, current_nbformat)
nb = Validator().upgrade_notebook_metadata(nb)
if self.validate:
try:
write(nb, notebook)
except ValidationError:
self.log.error(traceback.format_exc())
self.fail("Notebook '{}' failed to validate, metadata is corrupted".format(notebook))
else:
orig_write(nb, notebook)

9 changes: 9 additions & 0 deletions nbgrader/apps/validateapp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import traceback

from traitlets import Unicode, List, Bool, default
from nbconvert.nbconvertapp import NbConvertApp, DottedOrNone
from ..preprocessors import DisplayAutoGrades, Execute, ClearOutput, CheckCellMetadata
Expand Down Expand Up @@ -73,3 +75,10 @@ def init_single_notebook_resources(self, notebook_filename):

def write_single_notebook(self, output, resources):
return

def convert_single_notebook(self, notebook_filename):
try:
super(ValidateApp, self).convert_single_notebook(notebook_filename)
except Exception:
self.log.error(traceback.format_exc())
self.fail("nbgrader encountered a fatal error while trying to validate '{}'".format(notebook_filename))
1 change: 1 addition & 0 deletions nbgrader/docs/source/build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def autogen_command_line(root):
'QuickStartApp',
'ReleaseApp',
'SubmitApp',
'UpdateApp',
'ValidateApp'
]

Expand Down
1 change: 1 addition & 0 deletions nbgrader/docs/source/command_line_tools/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Instructor commands
nbgrader-quickstart
nbgrader-assign
nbgrader-validate
nbgrader-update
nbgrader-autograde
nbgrader-formgrade
nbgrader-feedback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n",
"\n",
Expand All @@ -20,13 +13,7 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -36,14 +23,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"---"
]
Expand All @@ -68,14 +48,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"---\n",
"## Part A (2 points)\n",
Expand Down Expand Up @@ -115,14 +88,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"Your function should print `[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]` for $n=10$. Check that it does:"
]
Expand All @@ -131,13 +97,7 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
"collapsed": false
},
"outputs": [
{
Expand Down Expand Up @@ -228,14 +188,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"---\n",
"\n",
Expand Down Expand Up @@ -272,14 +225,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:"
]
Expand All @@ -288,13 +234,7 @@
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
"collapsed": false
},
"outputs": [
{
Expand Down Expand Up @@ -382,14 +322,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"---\n",
"## Part C (1 point)\n",
Expand Down Expand Up @@ -417,14 +350,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"schema_version": 1,
"solution": false
}
},
"metadata": {},
"source": [
"---\n",
"## Part D (2 points)\n",
Expand Down
Loading

0 comments on commit 88c3b2c

Please sign in to comment.