Having two version of a notebook

40 views
Skip to first unread message

vincent leclere

unread,
Oct 25, 2019, 3:20:34 AM10/25/19
to Teaching with Jupyter Notebooks
Hello everybody,

I am using Jupyter notebook (in Julia, but irrelevant) for practical work. I do not have nor want
to install a JupyterHub for the moment. For each notebook I have two versions: the student one and
the one with solution. 

Currently I work in the following way : write the solution and then edit it to produce the student version. 
My problem is that when I make a modification (like typos or adding explanation in a question) in the
solution version I have to edit again by hand.

So here is my question: is there an extension that allows to maintain multiple version of the same notebook ?
I would like to be able to mark some cells in the master document and then suppress them when exporting 
the "student" version.

It can be scripted but I wonder if a light extension exists for that.

Nicolas M. Thiery

unread,
Oct 25, 2019, 4:33:53 AM10/25/19
to vincent leclere, jupyter-...@googlegroups.com
Hi,
Quick answer: I am using nbgrader for that purpose. nbgrader does much
more (submission handling, semi-automated grading, UI, ) but you can
convince it to do just this functionality from the command line. It
takes a bit of tweaking though. Some quick links for now:

https://gitlab.u-psud.fr/Info111/Info111/blob/master/Makefile#L211
https://gitlab.u-psud.fr/Info111/Info111/blob/master/nbgrader_config.py

I can elaborate a bit, but not in the next two weeks ...

One point where it may not match your needs: it's more about removing
some chunks in the cells than whole cells.

Cheers,
Nicolas
--
Nicolas M. Thiéry "Isil" <nth...@users.sf.net>
http://Nicolas.Thiery.name/

Wes Turner

unread,
Oct 25, 2019, 5:00:10 AM10/25/19
to vincent leclere, Teaching with Jupyter Notebooks
You can add cell-level metadata in JupyterLab:

> Open a notebook by double clicking it in the file browser.
> Select Cell tools in the left sidebar.
> Select the slide type (Slide, Subslide, Fragment, Skip, Notes).

https://jupyterlab.readthedocs.io/en/stable/user/export.html#reveal-js-slides

IDK if there's already an extension for generating an ipynb that excludes Skip or Notes cells?

Nbgrader or nbgitpuller may handle your use case?

https://nbgrader.readthedocs.io/en/stable/

https://nbgrader.readthedocs.io/en/stable/configuration/student_version.html#autograded-answer-cells :

> By default, nbgrader generate_assignment will replace regions beginning with BEGIN SOLUTION and END SOLUTION comment delimeters with: [...]

https://nbgrader.readthedocs.io/en/stable/configuration/student_version.html#autograder-tests-cells-with-hidden-tests :

> By default, nbgrader generate_assignment will remove tests wrapped within the BEGIN HIDDEN TESTS and END HIDDEN TESTS comment delimeters, for example: [...]
> --
> You received this message because you are subscribed to the Google Groups "Teaching with Jupyter Notebooks" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter-educat...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter-education/f7e9e181-7ec7-49db-9d7a-a782a9b7be2f%40googlegroups.com.
>

vincent leclere

unread,
Oct 25, 2019, 9:06:32 AM10/25/19
to Teaching with Jupyter Notebooks
Thanks, actually the cell-tag idea lead me to find https://github.com/jupyterlab/jupyterlab-celltags 

From Jupyterlab I can select and delete all tagged cell easily. 

nbgrader should be able to do the trick but the first time I looked at it, it seemed quite an overkill
for my usecase. I will try to install it anyway.
--
Vincent Leclère

Chris Holdgraf

unread,
Oct 25, 2019, 11:36:30 AM10/25/19
to vincent leclere, Teaching with Jupyter Notebooks
I had the same problem, and ended up writing a little lightweight python module to quickly generate student notebooks from my teacher copies. Maybe you'll find it useful!


Chris 

Jesse Oldroyd

unread,
Oct 25, 2019, 5:23:22 PM10/25/19
to Teaching with Jupyter Notebooks
I use the following method (stolen from StackExchange) for hiding cells in HTML output, which you may be interested in. I mark cells I want to remove with a remove_cell tag using View - Cell Toolbar - Tags, and then place the following code into the first code cell in my notebook:

% convert to HTML and remove cells with remove_cell tag
!jupyter nbconvert user_defined_functions.ipynb --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags="['remove_cell']" --to html

Each time I need to update the HTML output I just re-run the cell.

Wes Turner

unread,
Oct 25, 2019, 7:24:44 PM10/25/19
to Chris Holdgraf, vincent leclere, Teaching with Jupyter Notebooks
nbclean looks like a good solution if nbgrader is overkill.

Is there a good way to auto-save a copy with the skip/note/remove_cells removed?
Jupytext has a "paired notebook" feature:
https://github.com/mwouts/jupytext/blob/master/README.md#paired-notebooks-in-the-jupyter-server :

> Paired notebooks in the Jupyter Server
>
> Jupytext can write a given notebook to multiple files. In addition to the original notebook file, Jupytext can save the input cells to a text file — either a script or a Markdown document. Put the text file under version control for a clear commit history. Or refactor the paired script, and reimport the updated input cells by simply refreshing the notebook in Jupyter.
>
> Configuring notebooks to use Jupytext
>
> Select the pairing for a given notebook using either the Jupytext menu in Jupyter Notebook, or the Jupytext commands in JupyterLab.
>
> Alternatively, the pairing information for one or multiple notebooks can be set on the command line:
>
>  jupytext --set-formats ipynb,py notebook.ipynb

If you're going to present the solutions together in class (e.g. with reveal.js or RISE) anyway, would e.g. 'solution' be a better tag than skip/notes/remove_cell?
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter-education/CAD7kTDG-2GWYHYsN5LOJbZJhEUiRzouCAseCWieZHF2wKK2wpg%40mail.gmail.com.
>

Chris Holdgraf

unread,
Oct 26, 2019, 8:48:41 PM10/26/19
to Wes Turner, vincent leclere, Teaching with Jupyter Notebooks
Totally do-able, I think...just a matter to hours in the day to implement :-)
Reply all
Reply to author
Forward
0 new messages