jupyter-book icon indicating copy to clipboard operation
jupyter-book copied to clipboard

Allow setting all inputs as hidden by default

Open akhmerov opened this issue 5 years ago • 19 comments

Is your feature request related to a problem? Please describe.

For educational materials where programming is not primary focus, exposing source code is an uncommon approach, and it may distract the learners. In such circumstances it would be useful to hide or at least collapse all source code by default. Right now the only option that I see is to make every single input hidden.

Describe the solution you'd like

Enable a global switch that would allow making all inputs hidden by default.

akhmerov avatar May 31 '20 00:05 akhmerov

I'd like to second that. In a company setting, the code is sometimes considered distracting from the core analytical message and an option to remove/hide all input could speed up some resulting polishing of the notebook(s).

However, I don't think it needs to be the default but an optional flag like the --no-input flag of jupyter nbconvert.

As a sidenote to that, the html builder can work with "hide-input" and "remove-input" tags. The pdfhtml Builder seemed to have some issues to cope with "hide-input".

Raccooneer avatar May 31 '20 19:05 Raccooneer

Sorry, by "default" I mean that "if this global flag is set, all inputs are hidden unless explicitly configured to be shown on a per-cell basis".

akhmerov avatar May 31 '20 19:05 akhmerov

As @Racooneer says in a company setting being able to hide or remove inputs is crucial. Hope to see this feature in.

ofajardo avatar Jun 01 '20 14:06 ofajardo

After thinking about this a little bit more, I feel like it would be straightforward to do something like:

With a flag in the notebook, hide all inputs / outputs / whatever

However, it would be more-complex to then allow them to be turned back on one by one. It'd mean we need to support another tag for each option for the special-case of "cell elements are hidden by default, and now I want to turn one on instead of off".

Another option could be some improvements in a tool like nbclean. It could be used to, say, quickly add hide_input tags to all cells of a notebook. Would that be an acceptable solution? e.g., running:

nbclean ./*.ipynb --tags hide_input --cells code

And it'd add hide_input tags to all code cells in notebooks that match *.ipynb? What do folks think about something like that?

choldgraf avatar Jun 01 '20 15:06 choldgraf

hey, nbclean looks very interesting!

I was hoping to something simpler like the --no-input --no-promt flags from nbconvert. Having said that, in nbconvert I have to use those flags and in addition use a custom jinja template to remove stderr and certain outputs, like plotnine printing the class repr of the plot etc. So, at the end not that simple. From that point of view I would be happy using nbclean. I just wonder how could I make it easier for non experienced colleagues, maybe making a wrapper that runs nbclean with our defaults and immediately after jupyter-book build so that they need to remember only one command.

Another possibility could be having both a --no-input etc. flag, and let people who need more fine granular control use nbclean.

BTW, would what I just described work with the current version on nbclean and jupyter-book? (adding tags for removing input, ouput-prompt, stderr ...) If yes I give it a try tomorrow.

ofajardo avatar Jun 01 '20 16:06 ofajardo

Another option could be some improvements in a tool like nbclean. It could be used to, say, quickly add hide_input tags to all cells of a notebook. Would that be an acceptable solution? e.g., running:

nbclean ./*.ipynb --tags hide_input --cells code

And it'd add hide_input tags to all code cells in notebooks that match *.ipynb? What do folks think about something like that?

I like the idea in general. But I see the point of @ofajardo, that a wrapper completely changes access and usability of the functionality to some users (this would probably include me).

However, I think we are talking two use cases here: @choldgraf, your nbclean option changes the notebook / book permanently. Or at least I understand it that way. But I think for some cases just changing the output file would be sufficient without changing the tags of the input file. Maybe there could be a higher level condition that specifies a list of tags to ignore in the event of an --no-input flag.

I'm kind of afraid, that this would tangle with the idea of using a cache for larger books, you guys are working on. I couldn't tell...

Raccooneer avatar Jun 01 '20 17:06 Raccooneer

Well I think that it should be straightforward to support something like --no-inputs either at the command-line, or in page-level metadata. I think the question is whether to also support turning on some inputs if that flag is given (since there's currently no tag to support this)

choldgraf avatar Jun 01 '20 17:06 choldgraf

For that particular case nbclean would be an option: set all tags to hide snd then remove manually those you would like to not be hidden

ofajardo avatar Jun 01 '20 17:06 ofajardo

@ofajardo yeah exactly. I'm wondering if it is enough to tell users "if you want to hide all inouts/outputs/etc then use page metadata. If you only want to hide some, even if some is" all but one", use tags"

choldgraf avatar Jun 01 '20 17:06 choldgraf

I have the feeling that the common cases are either show everything or hide all, hiding many except few looks like more infrequent to me (Maybe I am wrong) and therefore is seems ok ask for some extra effort? I have read that hiding code is considered a bad practice in certain circles, but for us is everyday business, and it is supported out of the box in Rmarkdown for instance where many, many people use it.

ofajardo avatar Jun 01 '20 18:06 ofajardo

@choldgraf makes sense to me. This still leaves the choice of page-wide vs book-wide setting of hiding inputs. Furthermore the question of how much they should be hidden also deserves some thought: e.g. the current implementation via toggle button, I think, may be too distracting for a book where code is an aside.

akhmerov avatar Jun 01 '20 22:06 akhmerov

In the interim, here's the outline of my solution.

In _config.yml:

  local_extensions:
    extensions: ./sphinxext/

In spinxext/extensions.py:

import sphinx


class RemoveAllInputs(sphinx.transforms.Transform):
    default_priority = 210

    def apply(self):
        for node in self.document.traverse(myst_nb.nodes.CellInputNode):
            node.parent.remove(node)


def setup(app):
    app.connect("source-read", replace_refs)
    app.add_transform(RemoveAllInputs)

    return {
        'version': '0.0.1',
        'parallel_read_safe': True,
        'parallel_write_safe': True,
    }

akhmerov avatar Aug 12 '21 17:08 akhmerov

At least myst_nb 1.0.0 has a global option, just use this in _config.yml:

sphinx:
  config:
    nb_remove_code_source: true

mikegerber avatar Feb 20 '24 19:02 mikegerber