---
title: Directives
description: A cheat sheet of directives available in fastship.
order: 2
callout-appearance: simple
toc-depth: 2
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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:

```python
#| 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.

> [!NOTE]
> Example
>
> The following will result in the contents of the cell and it's output from being hidden:
>
> ```python
> #| hide
> print('you will not see this')
> ```
>
> Note that using `#| hide` is equivalent to using the Quarto directive `#| include: false`:
>
> ```python
> #| 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.

> [!NOTE]
> Example
>
> ```python
> #| 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.

> [!NOTE]
> Example
>
> The following cell will not display any output:
>
> ```python
> #| output: false
> 1 + 1
> ```
>
> The following cell with `#| output: asis` will produce the output `hello fastai` rendered as markdown instead of a string:
>
> ```python
> #| 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.

> [!NOTE]
> Example
>
> When you set `#| code-fold: true`, the input cell is collapsed:
>
> <details>
> <summary>Code</summary>
>
> ```python
> print('this is')
> print('output')
> print('that takes')
> print('lots of vertical space')
> ```
>
> </details>
>
> ```output
> 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:
>
> <details open>
> <summary>Code</summary>
>
> ```python
> print('this is')
> print('output')
> print('that takes')
> print('lots of vertical space')
> ```
>
> </details>
>
> ```output
> 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).

> [!NOTE]
> Example
>
> ```python
> #| default_exp baz
>
> # In a new notebook cell:
>
> #| export
> def my_function(): pass
> ```
>
> If our package is named `bitsnbytes`, then you can import:
>
> ```python
> 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.

> [!NOTE]
> Example
>
> ```python
> #| 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__`](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) 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
>
> ```python
> 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`](../api/showdoc.md#show_doc). See [these docs](docs.md#how-show_doc-works) for a detailed discussion of [`show_doc`](../api/showdoc.md#show_doc).

### 📓 `#| exporti`

An `i`nternal export. [Not included in `__all__`](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) 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 `s`ource export. Like `#| export` but in addition to showing docs via `showdoc.show_doc`, it also shows the source code.

> [!NOTE]
> Example
>
> ```python
> #| exports
> def say_hello(to):
>     "Say hello to somebody"
>     return f'Hello {to}!'
> ```
>
> this will produce the following output:
>
> ```python
> def say_hello(to):
>     "Say hello to somebody"
>     return f'Hello {to}!'
> ```
>
> ### say\_hello
>
> ```python
> 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.

> [!NOTE]
> Example
>
> ```python
> #| 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:

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

- `skip_exec: true` makes `ship-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.
- `exec_all: true` makes docs rendering execute every cell, not only exports, imports and [`show_doc`](../api/showdoc.md#show_doc) calls. `exec_all = true` under `[tool.fastship]` does the same for every notebook in the project.

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](docs.md#automatic-cell-execution).
