Skip to content
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

Added show_input and no_input settings #87

Merged
merged 2 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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