Jupyter notebooks are fantastic, but they’re really geared at developers. I had to lock one down so it could be used by non-developers (without damaging it). It took quite a lot of googling!
I figure that a lot of people must need this. If I were a university instructor, I’d like to send students to a server, let them play, but prevent them from breaking my example.
Locking Things Down
Here are the different things I did to mitigate damage:
- You can actually make the entire notebook read-only by setting the file permissions on command line or in properties (works for Windows and Linux). Jupyter will detect this (after you reload the page) and show that you can’t save anything.
- You can make individual cells un-deletable and un-editable (so they don’t mess up the top cells that the lower down cells they’re working with depend on):
- Run a cell.
- Click “Edit Metadata” in its banner.
- Add:
- “deletable”: false,
- “edittable”: false,
- You can actually hide the code for a range of cells like this code which hides the first four (which is very useful if you’re using iPython UI widgets and just want to show widgets and now how they were made) – disclaimer – I got this off stack overflow but am having trouble finding it to reference it currently:
from IPython.display import HTML HTML(''' code_show=true; function code_toggle_and_hide_move_buttons() { if (code_show){ $('div.input').slice(0,4).hide(); $('#move_up_down').hide() } else { $('div.input').slice(0,4).show(); $('#move_up_down').show() } code_show = !code_show } $(document).ready(code_toggle_and_hide_move_buttons); ''')
Further Recommendations
I would also suggest making sure you disable the terminal in your Jupyter config file and that you set a known location for your notebooks to be loaded from so that you can add the read-only attribute.
Also, you can disable various hotkeys in the UI, and you can use a CSS selector similar to the one in my “hide code” example above to hide the move-up/move-down cell buttons to help prevent errors cropping in that way.