Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force-save to Otter Assign for R notebooks #675

Merged
merged 3 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
**v5.1.0 (unreleased):**

* Removed patch that strips cell IDs from notebooks in Otter Assign per [#677](https://github.com/ucbds-infra/otter-grader/issues/677)
* Added notebook force-save to R notebook assignments per [#474](https://github.com/ucbds-infra/otter-grader/issues/474)
* Updated default version of `ottr` to v1.4.0

**v5.0.2:**

Expand Down
6 changes: 5 additions & 1 deletion otter/assign/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@
self._logger.debug(f"Initializing with config: {user_config}")
super().__init__(user_config, **kwargs)

# convert a boolean to a config object for self.generate if indicated
# convert true values masking subkey contains to those containers
if self.generate is True:
self.generate = AutograderConfig()
if self.export_cell is True:
self.export_cell = type(self).ExportCellValue()

Check warning on line 251 in otter/assign/assignment.py

View check run for this annotation

Codecov / codecov/patch

otter/assign/assignment.py#L251

Added line #L251 was not covered by tests

if self.variables:
warnings.warn(
Expand All @@ -258,6 +260,8 @@
ret = super().update(user_config)
if self.generate is True:
self.generate = AutograderConfig()
if self.export_cell is True:
self.export_cell = type(self).ExportCellValue()

Check warning on line 264 in otter/assign/assignment.py

View check run for this annotation

Codecov / codecov/patch

otter/assign/assignment.py#L264

Added line #L264 was not covered by tests
return ret

@property
Expand Down
4 changes: 0 additions & 4 deletions otter/assign/notebook_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ def transform_notebook(self, nb) -> "TransformedNotebookContainer":
transformed_cells += self.cell_factory.create_check_all_cells()

if self.assignment.export_cell:
export_cell = self.assignment.export_cell
if export_cell is True:
export_cell = {}

transformed_cells += self.cell_factory.create_export_cells()

transformed_nb = copy.deepcopy(nb)
Expand Down
23 changes: 17 additions & 6 deletions otter/assign/r_adapter/cell_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,33 @@
if not self.assignment.export_cell:
return []

force_save = not self.assignment.is_rmd and self.assignment.export_cell.force_save

instructions = nbformat.v4.new_markdown_cell()
instructions.source = "## Submission\n\nMake sure you have run all cells in your " \
"notebook in order before running the cell below, so that all images/graphs appear " \
"in the output. The cell below will generate a zip file for you to submit. **Please " \
"save before exporting!**"
"in the output. The cell below will generate a zip file for you to submit."

if not force_save: instructions.source += " **Please save before exporting!**"

if self.assignment.export_cell.instructions:
instructions.source += '\n\n' + self.assignment.export_cell.instructions

pdf_arg = ""
args = []
if self.assignment.export_cell.pdf:
pdf_arg = ", pdf = TRUE"
args.append("pdf = TRUE")
if force_save:
args.append("force_save = TRUE")

if args:
args = ", " + ", ".join(args)
else:
args = ""

Check warning on line 53 in otter/assign/r_adapter/cell_factory.py

View check run for this annotation

Codecov / codecov/patch

otter/assign/r_adapter/cell_factory.py#L53

Added line #L53 was not covered by tests

export = nbformat.v4.new_code_cell()
source_lines = ["# Save your notebook first, then run this cell to export your submission."]
source_lines.append(f'ottr::export("{self.assignment.notebook_basename}"{pdf_arg})')
source_lines = [] if force_save else \
["# Save your notebook first, then run this cell to export your submission."]
source_lines.append(f'ottr::export("{self.assignment.notebook_basename}"{args})')
export.source = "\n".join(source_lines)

lock(instructions)
Expand Down
2 changes: 1 addition & 1 deletion otter/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

DEFAULT_PYTHON_VERSION = "3.9"
OTTER_ENV_NAME = "otter-env"
OTTR_VERSION = "1.2.0"
OTTR_VERSION = "1.4.0"
TEMPLATE_DIR = pkg_resources.resource_filename(__name__, "templates")
GENERAL_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, "general")

Expand Down
5 changes: 2 additions & 3 deletions test/test_assign/files/r-correct/autograder/r-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
"source": [
"## Submission\n",
"\n",
"Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. **Please save before exporting!**"
"Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit."
]
},
{
Expand All @@ -445,8 +445,7 @@
},
"outputs": [],
"source": [
"# Save your notebook first, then run this cell to export your submission.\n",
"ottr::export(\"r-example.ipynb\")"
"ottr::export(\"r-example.ipynb\", force_save = TRUE)"
]
},
{
Expand Down
5 changes: 2 additions & 3 deletions test/test_assign/files/r-correct/student/r-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
"source": [
"## Submission\n",
"\n",
"Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. **Please save before exporting!**"
"Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit."
]
},
{
Expand All @@ -310,8 +310,7 @@
},
"outputs": [],
"source": [
"# Save your notebook first, then run this cell to export your submission.\n",
"ottr::export(\"r-example.ipynb\")"
"ottr::export(\"r-example.ipynb\", force_save = TRUE)"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions test/test_assign/files/r-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"solutions_pdf: true\n",
"export_cell:\n",
" pdf: false\n",
" force_save: true\n",
"files:\n",
" - data.csv"
]
Expand Down
2 changes: 1 addition & 1 deletion test/test_assign/files/rmd-autograder-correct/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ mamba env create -f /autograder/source/environment.yml
mamba init --all

# install ottr
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.2.0", dependencies=TRUE, repos=NULL)'
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.4.0", dependencies=TRUE, repos=NULL)'
2 changes: 1 addition & 1 deletion test/test_generate/files/autograder-r-correct/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ mamba env create -f /autograder/source/environment.yml
mamba init --all

# install ottr
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.2.0", dependencies=TRUE, repos=NULL)'
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.4.0", dependencies=TRUE, repos=NULL)'
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ mamba run -n otter-env Rscript /autograder/source/requirements.r
mamba init --all

# install ottr
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.2.0", dependencies=TRUE, repos=NULL)'
mamba run -n otter-env Rscript -e 'install.packages("https://cran.r-project.org/package=ottr&version=1.4.0", dependencies=TRUE, repos=NULL)'
Loading