5
$\begingroup$

Imagine I have a notebook divided into multiple groups of code cells which are set as initialization groups. I then have a single cell which evaluates a function program[] that yields an interactive tool (for example via Manipulate) which internal functions are defined in the initialization groups. Something like

enter image description here

If these groups are collapsed, is it possible to lock them so that when I share the notebook the code in these groups remains hidden and private and the only accessible cell is the one to run the program?

$\endgroup$
4
  • $\begingroup$ Have you considered moving the code into a package file (.wl)? Also, what do you mean by "hidden and private"? If the user needs to be able to execute it, they need to have read access to the code in some way. $\endgroup$
    – Lukas Lang
    Commented Jul 22, 2021 at 12:31
  • $\begingroup$ No, I don't know much about packages. The simplest thing is perhaps to imagine that I define my function program in some cell within an initialization group, so that when I run the cell with program[], it first evaluates the group cells, defining program, and then shows me only the output of this last cell. All I'm asking is whether it is possible to lock the group and let initialization cells do the automatic work without needing to see any of the code in it. $\endgroup$
    – sam wolfe
    Commented Jul 22, 2021 at 12:38
  • $\begingroup$ It would be very practical if I could do this, but perhaps moving it into a package is a better option. $\endgroup$
    – sam wolfe
    Commented Jul 22, 2021 at 12:39
  • $\begingroup$ You might be able to use cell tags to mark certain cells as password protected and then use one of the $Pre variables, or CellProlog or something to lock the use of the hide/expand button. I don't know how you'd implement that though. Obviously you still need a way to access the stuff in the cell yourself. $\endgroup$
    – N.J.Evans
    Commented Jul 22, 2021 at 12:49

1 Answer 1

7
$\begingroup$

You can set the status flag of the CellGroupData of your cells to Dynamic[False, None]. This prevents any interactive opening of the cells. If you then also set ShowGroupOpener to False, it seems somewhat workable:

CreatePalette@Row@{
   Button["Lock",
    NotebookWrite[
     InputNotebook[],
     NotebookRead[InputNotebook[]] /.
      CellGroupData[cells_, _] :> CellGroupData[cells, Dynamic[False, None]],
     All
     ];
    SetOptions[#, ShowGroupOpener -> False] & /@ Cells@NotebookSelection[InputNotebook[]]
    ],
   Button["Unlock",
    NotebookWrite[
     InputNotebook[],
     NotebookRead[InputNotebook[]] /.
      CellGroupData[cells_, _] :> CellGroupData[cells, False],
     All
     ];
    SetOptions[#, ShowGroupOpener -> Inherited] & /@ Cells@NotebookSelection[InputNotebook[]]
    ]
   }

enter image description here

$\endgroup$
1
  • $\begingroup$ This is brilliant! Thank you so much $\endgroup$
    – sam wolfe
    Commented Jul 24, 2021 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.