Directives

A cheat sheet of directives available in fastship.

Directive comments start with #|. They control:

  1. Cell visibility in rendered documentation
  2. How source code is generated from notebook cells
  3. Execution of cells for tests and docs

This page lists the directives supported by fastship.

A directive accepts a value with or without a colon. These spellings are equivalent:

#| default_exp: core
#| default_exp:core
#| default_exp core

A bare directive means true: #| hide is equivalent to #| hide: true.

You can also store directives in cell metadata under the nbdev key (preserved for notebook compatibility). 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.

Cell Visibility

The following directives control cell visibility in rendered documentation:

📓 #| hide

Hide cell input and output.

Example

The following will result in the contents of the cell and it's output from being hidden:

#| hide
print('you will not see this')

Note that using #| hide is equivalent to using the Quarto directive #| include: false:

#| include: false
print('you will not see this')

See the quarto docs for more information about #| include.

🔵 #| echo: <true|false>

Toggle the visibility of code-cell inputs.

Example
#| echo: false
print('you can see the output but not the code!')

which results in:

you can see the output but not the code!

🔵 #| output: <true|false|asis>

Setting this to false hides the output of a cell. Setting this to asis renders the output as raw markdown.

Example

The following cell will not display any output:

#| output: false
1 + 1

The following cell with #| output: asis will produce the output hello fastai rendered as markdown instead of a string:

#| output: asis
print("`hello fastai`")

🔵 #| code-fold: <show|true>

#| code-fold makes the code input collapsible. Use true to start collapsed or show to start expanded.

Example

When you set #| code-fold: true, the input cell is collapsed:

Code
print('this is')
print('output')
print('that takes')
print('lots of vertical space')
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:

Code
print('this is')
print('output')
print('that takes')
print('lots of vertical space')
this is
output
that takes
lots of vertical space

Generating Source Code

The following directives control how source code is exported from code cells.

📓 #| default_exp <name>

Names the module owned by this notebook where cells with #| export are written. Each generated module has one owning notebook (cross-notebook append is not supported).

Example
#| default_exp baz

# In a new notebook cell:

#| export
def my_function(): pass

If our package is named bitsnbytes, then you can import:

from bitsnbytes.baz import my_function

The package name is defined by name in [project] in pyproject.toml.

📓 #| export

Exports the items in the cell into the generated module and documentation.

Example
#| export
def say_hello(to:str # name of person to say hello to
             ):
    "Say hello to somebody"
    return f'Hello {to}!'

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:

say_hello

def say_hello(
    to:str, # name of person to say hello to
):

Say hello to somebody

The docs are generated from this export using show_doc. See these docs for a detailed discussion of show_doc.

📓 #| exporti

An 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.

📓 #| exports

A source export. Like #| export but in addition to showing docs via showdoc.show_doc, it also shows the source code.

Example
#| exports
def say_hello(to):
    "Say hello to somebody"
    return f'Hello {to}!'

this will produce the following output:

def say_hello(to):
    "Say hello to somebody"
    return f'Hello {to}!'

say_hello

def say_hello(
    to
):

Say hello to somebody

📓 #| exportd

Include 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.

Cell Execution

The following directives allow you to control how cells are executed during docs rendering and testing.

🔵 #| eval: <true|false>

When set to false, the cell is ignored during testing.

Example
#| eval: false
raise Exception("I'm not raised because I'm not run")

📓 Notebook frontmatter: skip_exec and skip_showdoc

Two 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:

# My notebook
> Needs a running database
- skip_exec: true

For example, fastship's own api/19_diff.ipynb sets skip_exec: true, and tutorials/modular_nbdev.ipynb sets skip_showdoc: true.

Cell execution when there is no directive

When a cell has no directives, cells are run by fastship according to the behavior described here.