-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hiding code input but not output? #15
Comments
I've heard and read about this request (with regards to I thought about implementing this in I'm not really a fan of removing code completely, but I think the code-folding option would be great. I have no clue how to do such a thing in HTML/CSS, can you provide an example for that? Do you have an idea how this could be realized in LaTeX? As a quick hack to remove all input cells, you can search for "block input" in {% extends 'nbsphinx-rst.tpl' %}
{% block input %}
{% endblock input %} This only looses a little bit of CSS, namely Completely removing the input on a per-notebook and per-cell basis would be simple, I'd just have to extend the supported notebook/cell metadata. |
Thanks for the reply and sorry for my slow response. I agree that it would make sense to wait for the 'official cell metadata conventions. The metadata tagging that one can now apply for creating Slideshows (enabled via the \View \Cell Toolbar \Slideshow in Jupyter) includes a 'Skip' tag. When that is chosen for a cell, that cell is not displayed in Slideshow mode. But I suspect you are talking about a different set of metadata. Code folding would be a very nice option. Hiding all output does come in handy. I plan to write up economics lecture notes that involve many plots and optimization, but most of the students will not have had prior python exposure. I do want them to learn python but it would be nice if students, at least on a first pass, they had the option to read the rendered notebook without the code. Code for some plots in particular can get long and distracting. I want them to understand the Economics content first, and then turn on the code so that they start understanding how to use python. I'm afraid I can't be much use with HTML/CSS, as most of this is beyond my capabilities (I'm more of an end user), but this Stackoverflow thread has HTML/javascript to add a javascript button to hide/display all code which works nicely in the notebook or nbviewer. I did briefly try using a template file as you suggested, but it didn't seem to do anything (I put the _templates folder. I haven't had time to explore further. Thanks again for your detailed discussion of the issue. I can live without code hiding for now but am glad to hear it is something you are considering. |
I think the slideshow "skip" tag should be orthogonal to "nbsphinx: hidden", both are very different uses but you might want both in the same notebook. As I said, I like code folding, but I'll not try to implement this on my own. If somebody submits a PR, I'm willing to consider it. Sorry that I misled you about the template customization. I mixed up I'm still not sure if your use case is acually the best use of a Jupyter notebook ... |
I agree on 'skip' and 'hidden', and also agree it's best to wait for an agreed standard. I'm not sure if I completely understand your last suggestion. Is the idea that I'd always have paired notebooks. One with the code and savefig and another that just keeps the markdown but pulls in the images? Something like that could be made to work but would seem to require keeping two notebook/scripts for each project. For me the beauty of jupyter notebooks -- and nbsphinx is becoming a crucial element in the workflow -- is its promise of convergence: one single document can be used as an interactive notebook, displayed as a slideshow, or rendered beautifully by Sphinx on a website or as a PDF document. If the we had the option of setting a tag to enable code-hiding/folding on some pages/cells, that would be icing on the cake. But it is not a very high priority --- my audience can learn to read or skip python code in the meantime. I will try the edits to nbsphinx that you suggested as soon as I have time and report back when I can. Thanks again |
I don't mean a strict one-to-one pairing, probably it makes sense to have a Jupyter notebook (or I also like the possibility to keep explanatory text, equations, images, code and resulting plots in one document, but that doesn't mean everything has to be in one single huge notebook. At some point this will also get hard to manage. I still think that code folding would be a nice feature to use occasionally, but it has a large potential for being over-used. |
I will explore some of those ideas, thanks and I am closing this issue since it does seem that any good solution involves waiting to see whatever notebook/cell metadata conventions the jupyter project settles on for hiding cells. |
With the release of [jupyter] [ANN] nbconvert 5.2.1 release it tested if its possible to hide all input. My test showed that this is indeed possible when you add an extra conf option in the init methode of class Exporter (see 'TemplateExporter'):
Remaining issue: in my example above it appears that the first output prompt is not hidden. The next output prompts are hidden. Question: if it's possible to work with (an) extra argument(s) in the source rst file in addition to the notebook name you could indicate for which notebook you want to use such settings. PS with the release [ANN] nbconvert 5.3.0 — now with tag-based element filtering! you can do tag based filtering but that was not my use case. |
@keluc Thanks for checking this out!
I don't know why that happens. Can you please provide an example notebook file and exact instructions how to reproduce this?
Do you mean as part of the |
Ok, I'll provide an example next week. Which can be used to tailor the config settings:
|
@keluc |
Would it not be possible to rely on a notebook cell metadata information
This would allow the customization to be done on a per-cell level inside a notebook. |
Sure, it would be possible. There have been previous discussions about this in #15, #17, #65, #86 and #185. Feel free to add suggestions to these issues/PRs, or create a new PR! I think the most reasonable course of action is to limit this feature to HTML output and implement a "toggle" button similar to the one used in https://jupyter.org/jupyter-book/features/hiding. I'm open for a PR is somebody wants to implement this. |
I note that I also note (
although I'm still not clear how / when they apply and at what versions of various things (nbconvert, notebook JupyterLab they started to apply, if at all. Tag based filtering in
|
Hi! I was really struggling to configure nbsphinx for my needs.
I leave this snippet for those who:
# ./ext/nbsphinx.py
# extensions = [..., "ext.nbsphinx", ...]
"""Patching latest nbsphinx
"""
from nbsphinx import RST_TEMPLATE
from nbsphinx import setup
import nbsphinx
import re
BLOCK_REGEX = r'(({{% block {block} -%}}\n)(.*?)({{% endblock {block} %}}\n))'
PATCH_TEMPLATE = r'{{% block {block} -%}}\n{patch}{{% endblock {block} %}}\n'
def search(block, template):
pattern = BLOCK_REGEX.format(block=block)
m = re.search(pattern, template, re.DOTALL)
assert m is not None, f"Block {block} is not found"
return m.group(3)
def patch(block, template, patch):
pattern = BLOCK_REGEX.format(block=block)
sub = PATCH_TEMPLATE.format(block=block, patch=patch)
return re.sub(pattern, sub, template, flags=re.DOTALL)
def remove_block_on_tag(block, tags, template):
content = search(block, RST_TEMPLATE)
conditions = [f"{t!r} in cell.metadata.tags" for t in tags]
content1 = f"""\
{{%- if {" or ".join(conditions)} -%}}
{{%- else -%}}
{content}
{{%- endif -%}}
"""
return patch(block, template, content1)
RST_TEMPLATE = remove_block_on_tag("input", ["remove_cell", "remove_input"], RST_TEMPLATE)
RST_TEMPLATE = remove_block_on_tag("nboutput", ["remove_cell", "remove_output"], RST_TEMPLATE)
nbsphinx.RST_TEMPLATE = RST_TEMPLATE
__all__ = ["setup"] |
Just came here because I'm looking for a way to remove the source of a cell when using nbsphinx. Just to be clear: "metadata": {
"jupyter": {
"source_hidden": true
}
}, |
Correct, this is not implemented yet. I have mentioned multiple issues and open PRs here: #15 (comment) Feel free to help out! |
The documentation has a nice discussion of how to hide code cells and their output entirely. It is however also frequently useful to hide just the code and not the output for some or all code-cells in a notebook (e.g. to make an HTML only report). I've seen some rendered html that seems to go further by offering code-folding buttons on the rendered html.
Is there a way to hide just the code input but not the output with nbsphinx? For reference, with jupyter's nbconvert one can use templates and a command as such:
where the template is like the one adapted from this SO answer
The text was updated successfully, but these errors were encountered: