Hiding "spoilers" (answers) cells

Hi, I’m working on some Jupyter based training material that it’s supposed to be used as self guided support for some classes. It has some exaplanatory markdown, some code examples in code cells that students can use, and some practice exercises. I want to provide answers for the exercises , which I’m doing with a detail tags. To give you a small (artificial example):

[Markdown Cell] Write some code that asks the user to enter a sentence, and print back the last word in that sentence.

[Code cell]

# Enter your code here
pass

[Markdown cell]

<details>

<summary>Answers</summary>

You can use split() to separate words, and negative indexing to get the last value:
```python
sentence = input("Enter a sentence:")
print(sentence.split()[-1])
```
</details>

Using the <details> tag like this allows the students to avoid the “spoiler” until they have tried solving it. However the snippet of Python code that I enter is not runnable, if they want to try it they need to copy and paste it into their own cell.

If I put the answer in a code cell, it’s automatically visible. I’ve seen some tips about how to hide all input cells, but I only want to do this selectively (some code cells are examples and should be visible by default, but code cells shouldn’t be). Is there a way to do this?

1 Like

I use the collapsible headings extension as a simple way of hiding multiple cells, code and markdown, beneath a collapsed header. Quick and easy, though it does rely on the student user have the extension installed and enabled.

1 Like

I’ll take a look, that might work. Thanks!

I have used the magic command %load for this purpose. It loads the content of a text file into a code cell. What I do is to create a folder with solutions for each exercise and load them where necessary.

It looks like the following.

# Uncomment the following line to see the solution
# %load ./snippets/solution_exercise.py
3 Likes

That worked really well, thanks!

1 Like