greenape / mknotebooks

A plugin for mkdocs to help you include Jupyter notebooks in your projects
MIT License
136 stars 20 forks source link

Cell output removal broken? #1117

Open AdrianoKF opened 9 months ago

AdrianoKF commented 9 months ago

Hi,

I'm having an issue with the removal of cell outputs through the tag_remove_configs config settings. Despite attaching the configured cell tags, they still show out in the rendered output page. Curiously enough, the tags that seems to be working fine are Remove_cell and Remove_input, but not the ones that remove the outputs.

Here's my mkdocs.yml for a minimum reproducible example (the settings come straight out of the official mknotebooks examples):

site_name: My Docs
theme: readthedocs

plugins:
  - mknotebooks:
      execute: true
      # https://github.com/greenape/mknotebooks/blob/master/examples/cell_tag_remove/mkdocs.yml
      # See the following for how to add the tags to cells: https://jupyterbook.org/en/stable/content/metadata.html#jupyter-cell-tags
      tag_remove_configs:
        remove_cell_tags:
          - Remove_cell
        remove_all_outputs_tags:
          - Remove_all_output
        remove_single_output_tags:
          - Remove_single_output
        remove_input_tags:
          - Remove_input

This is the JSON source of the notebook (containing a single cell with the Remove_all_output tag):

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Remove_all_output"
    ]
   },
   "outputs": [],
   "source": [
    "print(\"Hello\")\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

Looking at the generated HTML page, I see that the cell has a class attached to it that corresponds to the cell tag (celltag_Remove_all_output), but I was under the impression from the nbconvert docs that the TagRemovePreprocessor would completely remove the node from the generated HTML (if it is working as intended).

As a workaround, I have been hiding the outputs manually using CSS, but I'd like to understand if I'm doing something wrong, or if the behavior I'm seeing is indeed unintended.

Thanks for your support!

RobinL commented 1 week ago

I also had this problem a while back. struggling to remeber exactly the issue but I was able to solve with. Apologies this code is a bit mixed in with some other project-specific stuff

@event_priority(-100)
def on_config(config: MkDocsConfig) -> MkDocsConfig:
    # convert ipynb to md rather than html directly
    # this ensures we render symbols such as '<' correctly
    # in codeblocks, instead of '%lt;'

    t = TagRemovePreprocessor()
    mknotebooks_config = config.get("plugins", {}).get("mknotebooks", {})
    tag_remove_configs = mknotebooks_config.config.get("tag_remove_configs", {})
    for option, setting in tag_remove_configs.items():
        setattr(t, option, set(setting))

    md_exporter = MarkdownExporter(config=config)
    md_exporter.register_preprocessor(t, enabled=True)

    # md_exporter.config["TagRemovePreprocessor"]["remove_input_tags"] = ("hideme",)
    # overwrite mknotebooks config option
    config["notebook_exporter"] = md_exporter
    return config

in docs/hooks/__init__.py