Skip to content

Commit

Permalink
Add tag to student response cells in otter assign student notebook (#914
Browse files Browse the repository at this point in the history
)

* leave solution cell tags in student notebooks

* fixing tests
  • Loading branch information
chrispyles authored Jan 24, 2025
1 parent 6e07025 commit 0acbe43
Show file tree
Hide file tree
Showing 25 changed files with 453 additions and 129 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fixed assignment summary in Otter Assign so that manual questions are included per [#886](https://github.com/ucbds-infra/otter-grader/issues/886)
* Wrap `ModuleNotFoundError` for `otter` in grading image to include debugging instructions per [#907](https://github.com/ucbds-infra/otter-grader/issues/907)
* Updated Otter Assign to disallow metadata tests for Colab notebooks per [#901](https://github.com/ucbds-infra/otter-grader/issues/901)
* Updated Otter Assign to leave solution cell tags in student notebooks per [#893](https://github.com/ucbds-infra/otter-grader/issues/893)

**v6.0.5:**

Expand Down
5 changes: 4 additions & 1 deletion otter/assign/cell_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .assignment import Assignment
from .feature_toggle import FeatureToggle
from .question_config import QuestionConfig
from .solutions import ANSWER_CELL_TAG
from .utils import lock


Expand Down Expand Up @@ -174,4 +175,6 @@ def create_markdown_response_cell() -> nbformat.NotebookNode:
Returns:
``nbformat.NotebookNode``: the response cell
"""
return nbformat.v4.new_markdown_cell("_Type your answer here, replacing this text._")
return nbformat.v4.new_markdown_cell(
"_Type your answer here, replacing this text._", metadata={"tags": [ANSWER_CELL_TAG]}
)
4 changes: 4 additions & 0 deletions otter/assign/notebook_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .r_adapter import rmarkdown_converter
from .r_adapter.cell_factory import RCellFactory
from .solutions import (
ANSWER_CELL_TAG,
has_seed,
overwrite_seed_vars,
SOLUTION_CELL_TAG,
Expand Down Expand Up @@ -319,6 +320,9 @@ def _get_transformed_cells(self, cells: list[nbf.NotebookNode]) -> list[nbf.Note
self.tests_mgr.read_test(cell, question)
continue

elif curr_block[-1] == BlockType.PROMPT:
cell = add_tag(cell, ANSWER_CELL_TAG)

elif curr_block[-1] == BlockType.SOLUTION:
cell = add_tag(cell, SOLUTION_CELL_TAG)

Expand Down
11 changes: 11 additions & 0 deletions otter/assign/r_adapter/rmarkdown_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from copy import deepcopy

from ..solutions import ANSWER_CELL_TAG, SOLUTION_CELL_TAG
from ..utils import remove_tag
from ...utils import get_source, NBFORMAT_VERSION, NOTEBOOK_METADATA_KEY


Expand Down Expand Up @@ -113,6 +115,15 @@ def write_as_rmd(nb: nbf.NotebookNode, rmd_path: str, has_solutions: bool):
# notebook (resolves whitespace issues caused by the use of prompts for written questions)
if not has_solutions:
for i, cell in enumerate(nb["cells"]):
# remove tags from all cells since they aren't needed for Rmd files
if "tags" in cell["metadata"]:
tags = cell["metadata"]["tags"]
if SOLUTION_CELL_TAG in tags:
tags.remove(SOLUTION_CELL_TAG)
if ANSWER_CELL_TAG in tags:
tags.remove(ANSWER_CELL_TAG)
if not tags:
cell["metadata"].pop("tags")
if (
i < len(nb["cells"]) - 1
and cell["cell_type"] == "markdown"
Expand Down
9 changes: 4 additions & 5 deletions otter/assign/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

from .assignment import Assignment
from .r_adapter import solutions as r_solutions
from .utils import has_tag, is_cell_type, remove_output, remove_tag
from .utils import add_tag, has_tag, is_cell_type, remove_output, remove_tag
from ..utils import get_source


ANSWER_CELL_TAG = "otter_answer_cell"
BLOCK_PROMPT = "..."
OTTER_INCLUDE_TAG = "otter_include"
SOLUTION_CELL_TAG = "otter_assign_solution_cell"


Expand Down Expand Up @@ -205,9 +207,6 @@ def strip_ignored_lines(nb: nbf.NotebookNode) -> nbf.NotebookNode:
return nb


OTTER_INCLUDE_TAG = "otter_include"


def strip_solutions_and_output(assignment: Assignment, nb: nbf.NotebookNode) -> nbf.NotebookNode:
"""
Create a copy of a notebook with solutions and outputs stripped.
Expand All @@ -231,7 +230,7 @@ def strip_solutions_and_output(assignment: Assignment, nb: nbf.NotebookNode) ->
cell = remove_tag(cell, OTTER_INCLUDE_TAG)
else:
del_md_solutions.append(i)
nb["cells"][i] = remove_tag(cell, SOLUTION_CELL_TAG)
nb["cells"][i] = add_tag(remove_tag(cell, SOLUTION_CELL_TAG), ANSWER_CELL_TAG)

del_md_solutions.reverse()
for i in del_md_solutions:
Expand Down
20 changes: 16 additions & 4 deletions test/test_assign/files/example-correct/autograder/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand Down Expand Up @@ -310,7 +314,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Write your thing here._"
]
Expand All @@ -333,7 +341,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand Down Expand Up @@ -549,9 +561,9 @@
"assignment_name": "hw01",
"tests": {
"q1": {
"all_or_nothing": true,
"name": "q1",
"points": null,
"all_or_nothing": true,
"suites": [
{
"cases": [
Expand Down
52 changes: 40 additions & 12 deletions test/test_assign/files/example-correct/student/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -81,7 +83,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -126,7 +130,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -165,7 +171,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand Down Expand Up @@ -208,7 +218,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -249,7 +261,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Write your thing here._"
]
Expand All @@ -272,7 +288,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand All @@ -295,7 +315,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -331,7 +353,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand All @@ -342,7 +366,9 @@
{
"cell_type": "markdown",
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"source": [
"Then call your function below to add the numbers 1 and 2 (should not be removed)"
Expand All @@ -352,7 +378,9 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
"tags": [
"otter_answer_cell"
]
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -440,9 +468,9 @@
"assignment_name": "hw01",
"tests": {
"q1": {
"all_or_nothing": true,
"name": "q1",
"points": null,
"all_or_nothing": true,
"suites": [
{
"cases": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand Down Expand Up @@ -298,7 +302,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Write your thing here._"
]
Expand All @@ -319,7 +327,11 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"tags": [
"otter_answer_cell"
]
},
"source": [
"_Type your answer here, replacing this text._"
]
Expand Down
Loading

0 comments on commit 0acbe43

Please sign in to comment.