Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added show_input and no_input settings (#87)
  • Loading branch information
ggbaro committed Sep 23, 2022
1 parent d751a76 commit 9861720
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -141,6 +141,25 @@ plugins:
kernel_name: python3
```

#### Ingore Code Input

By default the plugin will show full code and regular cell output details.
You can hide cell code input for all the notebooks:

```yaml
plugins:
- mkdocs-jupyter:
show_input: False
```

You can also decide to hide the `Out[#]` output notation and other cell metadata for all the notebooks:

```yaml
plugins:
- mkdocs-jupyter:
no_input: True
```

### Jupyter themes

You can configure the different Jupyter themes.
Expand Down
35 changes: 31 additions & 4 deletions mkdocs_jupyter/nbconvert2.py
Expand Up @@ -43,6 +43,8 @@ def nb2html(
start=0,
end=None,
allow_errors=True,
show_input: bool = True,
no_input: bool = False,
):
"""
Convert a notebook to HTML
Expand All @@ -63,7 +65,10 @@ def nb2html(
End cell number
allow_errors
Render even if notebook execution fails
show_input: bool
Shows code input (default: True)
no_input: bool
Render notebook without code or output (default: False)
Returns
-------
HTML content
Expand All @@ -79,6 +84,8 @@ def nb2html(
start=start,
end=end,
allow_errors=allow_errors,
show_input=show_input,
no_input=no_input,
)

# Use the templates included in this package
Expand Down Expand Up @@ -157,13 +164,32 @@ def get_nbconvert_app(
start=0,
end=None,
allow_errors=True,
show_input: bool = True,
no_input: bool = False,
) -> NbConvertApp:
"""Create"""

# Load the user's nbconvert configuration
app = NbConvertApp()
app.load_config_file()

template_exported_conf = {
"TemplateExporter": {
"exclude_input": not show_input,
}
}

if no_input:
template_exported_conf.update(
{
"TemplateExporter": {
"exclude_output_prompt": True,
"exclude_input": True,
"exclude_input_prompt": True,
}
}
)

app.config.update(
{
# This Preprocessor changes the pygments css prefixes
Expand All @@ -179,6 +205,7 @@ def get_nbconvert_app(
"kernel_name": kernel_name,
"allow_errors": allow_errors,
},
**template_exported_conf,
}
)

Expand Down Expand Up @@ -213,9 +240,9 @@ def custom_markdown2html(source):
This is done so it's the same HTML structure that for regular non-language
sections.
"""
return MarkdownWithMath(
renderer=CustomMarkdownRendered(escape=False)
).render(source)
return MarkdownWithMath(renderer=CustomMarkdownRendered(escape=False)).render(
source
)


class CustomMarkdownRendered(IPythonRenderer):
Expand Down
6 changes: 6 additions & 0 deletions mkdocs_jupyter/plugin.py
Expand Up @@ -44,6 +44,8 @@ class Plugin(mkdocs.plugins.BasePlugin):
("include_source", config_options.Type(bool, default=False)),
("ignore_h1_titles", config_options.Type(bool, default=False)),
("allow_errors", config_options.Type(bool, default=True)),
("show_input", config_options.Type(bool, default=True)),
("no_input", config_options.Type(bool, default=False)),
)
_supported_extensions = [".ipynb", ".py"]

Expand Down Expand Up @@ -81,6 +83,8 @@ def on_pre_page(self, page, config, files):

exec_nb = self.config["execute"]
allow_errors = self.config["allow_errors"]
show_input = self.config["show_input"]
no_input = self.config["no_input"]

if self.config["execute_ignore"]:
ignore_pattern = self.config["execute_ignore"]
Expand All @@ -99,6 +103,8 @@ def new_render(self, config, files):
kernel_name=kernel_name,
theme=theme,
allow_errors=allow_errors,
show_input=show_input,
no_input=no_input,
)
self.content = body
toc, title = get_nb_toc(page.file.abs_src_path)
Expand Down

0 comments on commit 9861720

Please sign in to comment.