def _secret(): ...
for i in range(3):
print(i)0
1
2
Directive comments start with #|. They control:
nbdev supports all Quarto directives and adds its own. This page lists all nbdev directives and selected Quarto directives. See Quartoโs directive reference for its full list.
To clarify the origin of directives we use the following emojis:
A directive accepts a value with or without a colon. These spellings are equivalent:
A bare directive means true: #| hide is equivalent to #| hide: true.
You can also put directives in cell metadata under an nbdev key. For example, {"nbdev": {"hide": "true", "eval": "false"}} is equivalent to #| hide and #| eval: false.
Metadata values must be strings. The string "true" represents a bare directive. JSON booleans true and false raise an error. Notebook-level directives such as default_exp can use the same format in notebook metadata. A comment takes precedence over metadata for the same directive.
When rendering docs, nbdev writes directives into cell sources as Quarto options. It uses name: value for values and name: true for bare directives. Quarto applies the options it recognizes and ignores the rest.
The following directives control cell visibility in rendered documentation:
#| hideHide cell input and output.
The following will result in the contents of the cell and itโs output from being hidden:
Note that using #| hide is equivalent to using the Quarto directive #| include: false:
See the quarto docs for more information about #| include.
#| echo: <true|false>Toggle the visibility of code-cell inputs.
#| output: <true|false|asis>Setting this to false hides the output of a cell. Setting this to asis renders the output as raw markdown.
#| hide_lineHide a specific line of code in an input cell.
#| filter_stream <keyword> ...Filter lines containing specific keywords in cell outputs.
#| code-fold: <show|true>#| code-fold makes the code input collapsible. Use true to start collapsed or show to start expanded.
When you set #| code-fold: true, the input cell is collapsed:
this is
output
that takes
lots of vertical space
When you set #| code-fold: show the input cell is shown but still in a collapsible element:
The following directives control how source code is exported from code cells.
#| default_exp <name>Names the module where cells with the #| export directive will be exported to by default.
#| exportExports the items in the cell into the generated module and documentation.
The above cell will get exported to the module specified by #| default_exp. These exports are automatically included in __all__ for the module. To export without including the names in __all__, use #| exporti.
Furthermore, the documentation for this function will automatically be rendered like this:
The docs are generated from this export using show_doc. See these docs for a detailed discussion of show_doc.
#| exportiAn internal export. Not included in __all__ or the docs. Useful for a function that is called by other functions in this module but is not part of the public API.
Equivalently, you can prefix your function or method with _ e.g. def _private(): pass.
#| exportsA source export. Like #| export but in addition to showing docs via showdoc.show_doc, it also shows the source code.
#| exportdInclude the cell in the generated moduleโs docstring. Code cells become fenced examples in the docstring, not executable module code. Markdown cells appear as written, as they do with #| export.
This lets you use tested examples in the docstring without copying them. The docstring starts with the title cellโs > summary, followed by exported Markdown and exportd cells in notebook order.
The following directives allow you to control how cells are executed during docs rendering and testing.
#| exec_docEnsures that a cell is executed each time before generating docs. When a cell does not have this annotation, it is run according to the default rules described here.
#| eval: <true|false>When set to false, the cell is ignored during testing.
skip_exec and skip_showdocTwo frontmatter keys control execution for a whole notebook, rather than a single cell. Set them in the notebookโs frontmatter, e.g. as list items under the title:
skip_exec: true makes nbdev-test skip the notebook and report it as passing. Use it when the notebook needs credentials or live services unavailable in the test environment.skip_showdoc: true makes docs rendering use stored outputs without executing cells.For example, nbdevโs own api/19_diff.ipynb sets skip_exec: true, and tutorials/modular_nbdev.ipynb sets skip_showdoc: true.
When a cell has no directives, cells are run by nbdev according to the behavior described here.