Functions to show the doc cells in notebooks

All the automatic documentation of functions and classes are generated with the show_doc function. It displays the name, arguments, docstring along with a link to the source code on GitHub.

Gather the information

The inspect module lets us know quickly if an object is a function or a class but it doesn't distinguish classes and enums.



Check if cls is an enum or another type of class

e = enum.Enum('e', 'a b')
assert is_enum(e)
assert not is_enum(int)





Test if name is a library module.

assert is_lib_module('export')
assert not is_lib_module('transform')

try_external_doc_link(name, packages)

Try to find a doc link for name in packages

This function will only work for other packages built with nbdev.

test_eq(try_external_doc_link('TfmdDL', ['fastai2']), '')
test_eq(try_external_doc_link('Learner', ['fastai2']), '')
#Only works for packages built with nbdev right now
assert try_external_doc_link('Tensor', ['torch']) is None

doc_link(name, include_bt=True)

Create link to documentation for name.

This function will generate link for a module (pointing to the html conversion of the notebook that created it) and functions (pointing to the hmtl conversion of the notebook they were defined, with the first anchor found before). If the function/module is not part of the library you are writing, it will call the function custom_doc_links generated in _nbdev (you can customize it to your needs) and just return the name between backticks if that function returns None.

For instance, fastai2 has the following custom_doc_links that tries to find a doc link for name in fastcore then nbdev (in this order):

def custom_doc_links(name): 
    from nbdev.showdoc import try_external_doc_link
    return try_external_doc_link(name, ['fastcore', 'nbdev'])
test_eq(doc_link('export'), f'[`export`](/export)')
test_eq(doc_link('DocsTestClass'), f'[`DocsTestClass`](/export#DocsTestClass)')
test_eq(doc_link('DocsTestClass.test'), f'[`DocsTestClass.test`](/export#DocsTestClass.test)')
test_eq(doc_link('_nbdev'), f'`_nbdev`')
test_eq(doc_link('__main__'), f'`__main__`')


Search for doc links for any item between backticks in text and isnter them

This function not only add links to backtick keywords, it also update the links that are already in the text (in case they have changed).

tst = add_doc_links('This is an example of `DocsTestClass`')
test_eq(tst, "This is an example of [`DocsTestClass`](/export#DocsTestClass)")
tst = add_doc_links('This is an example of [`DocsTestClass`](old_link.html)')
test_eq(tst, "This is an example of [`DocsTestClass`](/export#DocsTestClass)")


Return link to func in source code

Be sure to properly set the git_url in setting.ini (derived from lib_name and branch on top of the prefix you will need to adapt) so that those links are correct.

As important as the source code, we want to quickly jump to where the function is defined when we are in a development notebook.

get_nb_source_link(func, local=False, is_name=None)

Return a link to the notebook where func is defined.

test_eq(get_nb_source_link(DocsTestClass.test), get_nb_source_link(DocsTestClass))
test_eq(get_nb_source_link('DocsTestClass'), get_nb_source_link(DocsTestClass))

NB_SOURCE_URL = Config().git_url.replace('', '')+ Config()'/'
test_eq(get_nb_source_link(check_re), f'{NB_SOURCE_URL}00_export.ipynb#Finding-patterns')
test_eq(get_nb_source_link(check_re, local=True), f'00_export.ipynb#Finding-patterns')

You can either pass an object or its name (by default is_name will look if func is a string or not to decide if it's True or False, but you can override if there is some inconsistent behavior). local will return a local link, otherwise it will point to a the notebook on github wrapped in nbviewer.

nb_source_link(func, is_name=None, disp=True)

Show a relative link to the notebook where func is defined

This function assumes you are in one notebook in the development folder, otherwise you can use disp=False to get the relative link. You can either pass an object or its name (by default is_name will look if func is a string or not to decide if it's True or False, but you can override if there is some inconsistent behavior).

test_eq(nb_source_link(check_re, disp=False), f'00_export.ipynb#Finding-patterns')
test_eq(nb_source_link('check_re', disp=False), f'00_export.ipynb#Finding-patterns')

Show documentation



Representation of type t (in a type annotation)

The representation tries to find doc links if possible.

tst = type_repr(Optional[DocsTestClass])
test_eq(tst, '`Optional`\\[[`DocsTestClass`](/export#DocsTestClass)\\]')
tst = type_repr(Union[int, float])
test_eq(tst, '`Union`\\[`int`, `float`\\]')
test_eq(type_repr(Param("description")), '"description"')



Formats function param to param:Type=val with font weights: param=bold, val=italic

sig = inspect.signature(notebook2script)
params = [format_param(p) for _,p in sig.parameters.items()]
test_eq(params, ['**`fname`**=*`None`*', '**`silent`**=*`False`*', '**`to_dict`**=*`False`*'])


show_doc(elt, doc_string=True, name=None, title_level=None, disp=True, default_cls_level=2)

Show documentation for element elt. Supported types: class, function, and enum.

doc_string determines if we show the docstring of the function or not. name can be used to provide an alternative to the name automatically found. title_level determines the level of the anchor (default 3 for classes and 4 for functions). If disp is False, the function returns the markdown code instead of displaying it. If doc_string is True and monospace_docstrings is set to True in settings.ini, the docstring of the function is formatted in a code block to preserve whitespace.

For instance


will display


notebook2script(fname=None, silent=False, to_pkl=False)

Convert fname or all the notebook satisfying all_fs.

The doc command



Convert markdown md to HTML code


test_eq(get_doc_link(notebook2script), '')
from import TfmdDL
test_eq(get_doc_link(TfmdDL), '')



Show show_doc info in preview window when used in a notebook