#hide
#default_exp showdoc
#default_cls_lvl 3
from nbdev.showdoc import show_doc
#export
from nbdev.imports import *
from nbdev.export import *
from nbdev.sync import *
from nbconvert import HTMLExporter
from fastcore.docments import docments, isclass, _clean_comment, _tokens, _param_locs, _get_comment
from fastcore.utils import IN_NOTEBOOK
from fastcore.xtras import get_source_link, _unwrapped_type_dispatch_func
import string
from tokenize import COMMENT
if IN_NOTEBOOK:
from IPython.display import Markdown,display
from IPython.core import page
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.
The inspect module lets us know quickly if an object is a function or a class but it doesn't distinguish classes and enums.
#export
def is_enum(cls):
"Check if `cls` is an enum or another type of class"
return type(cls) in (enum.Enum, enum.EnumMeta)
e = enum.Enum('e', 'a b')
assert is_enum(e)
assert not is_enum(e.__class__)
assert not is_enum(int)
#export
def is_lib_module(name):
"Test if `name` is a library module."
if name.startswith('_'): return False
try:
_ = importlib.import_module(f'{get_config().lib_name}.{name}')
return True
except: return False
assert is_lib_module('export')
assert not is_lib_module('transform')
#export
re_digits_first = re.compile('^[0-9]+[a-z]*_')
#export
def try_external_doc_link(name, packages):
"Try to find a doc link for `name` in `packages`"
for p in packages:
try:
mod = importlib.import_module(f"{p}._nbdev")
try_pack = source_nb(name, is_name=True, mod=mod)
if try_pack:
page = re_digits_first.sub('', try_pack).replace('.ipynb', '')
return f'{mod.doc_url}{page}#{name}'
except ModuleNotFoundError: return None
This function will only work for other packages built with nbdev
.
test_eq(try_external_doc_link('get_name', ['nbdev']), 'https://nbdev.fast.ai/sync#get_name')
#export
def is_doc_name(name):
"Test if `name` corresponds to a notebook that could be converted to a doc page"
for f in get_config().path("nbs_path").glob(f'*{name}.ipynb'):
if re_digits_first.sub('', f.name) == f'{name}.ipynb': return True
return False
test_eq(is_doc_name('flaags'),False)
test_eq(is_doc_name('export'),True)
test_eq(is_doc_name('index'),True)
#export
def doc_link(name, include_bt=True):
"Create link to documentation for `name`."
cname = f'`{name}`' if include_bt else name
try:
#Link to modules
if is_lib_module(name) and is_doc_name(name): return f"[{cname}]({get_config().doc_baseurl}{name}.html)"
#Link to local functions
try_local = source_nb(name, is_name=True)
if try_local:
page = re_digits_first.sub('', try_local).replace('.ipynb', '')
return f'[{cname}]({get_config().doc_baseurl}{page}.html#{name})'
##Custom links
mod = get_nbdev_module()
link = mod.custom_doc_links(name)
return f'[{cname}]({link})' if link is not None else cname
except: return cname
This function will generate links for modules (pointing to the html conversion of the corresponding notebook) and functions (pointing to the html conversion of the notebook where 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, fastai 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'])
Please note that module links only work if your notebook names "correspond" to your module names:
Notebook name | Doc name | Module name | Module file | Can doc link? |
---|---|---|---|---|
export.ipynb | export.html | export | export.py | Yes |
00_export.ipynb | export.html | export | export.py | Yes |
00a_export.ipynb | export.html | export | export.py | Yes |
export_1.ipynb | export_1.html | export | export.py | No |
03_data.core.ipynb | data.core.html | data.core | data/core.py | Yes |
03_data_core.ipynb | data_core.html | data.core | data/core.py | No |
test_eq(doc_link('export'), f'[`export`](/export.html)')
test_eq(doc_link('DocsTestClass'), f'[`DocsTestClass`](/export.html#DocsTestClass)')
test_eq(doc_link('DocsTestClass.test'), f'[`DocsTestClass.test`](/export.html#DocsTestClass.test)')
test_eq(doc_link('Tenso'),'`Tenso`')
test_eq(doc_link('_nbdev'), f'`_nbdev`')
test_eq(doc_link('__main__'), f'`__main__`')
test_eq(doc_link('flags'), '`flags`') # we won't have a flags doc page even though we do have a flags module
#export
_re_backticks = re.compile(r"""
# Catches any link of the form \[`obj`\](old_link) or just `obj`,
# to either update old links or add the link to the docs of obj
\[` # Opening [ and `
([^`]*) # Catching group with anything but a `
`\] # ` then closing ]
(?: # Beginning of non-catching group
\( # Opening (
[^)]* # Anything but a closing )
\) # Closing )
) # End of non-catching group
| # OR
` # Opening `
([^`]*) # Anything but a `
` # Closing `
""", re.VERBOSE)
#export
def add_doc_links(text, elt=None):
"Search for doc links for any item between backticks in `text` and insert them"
def _replace_link(m):
try:
if m.group(2) in inspect.signature(elt).parameters: return f'`{m.group(2)}`'
except: pass
return doc_link(m.group(1) or m.group(2))
return _re_backticks.sub(_replace_link, text)
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.html#DocsTestClass)")
tst = add_doc_links('This is an example of [`DocsTestClass`](old_link.html)')
test_eq(tst, "This is an example of [`DocsTestClass`](/export.html#DocsTestClass)")
Names in backticks will not be converted to links if elt
has a parameter of the same name
def t(a,export):
"Test func that uses 'export' as a parameter name and has `export` in its doc string"
assert '[`export`](/export.html)' not in add_doc_links(t.__doc__, t)
Names in backticks used in markdown links will be updated like normal
def t(a,export):
"Test func that uses 'export' as a parameter name and has [`export`]() in its doc string"
assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)
#hide
# if the name in backticks is not a param name, links will be added/updated like normal
def t(a,exp):
"Test func with `export` in its doc string"
assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)
def t(a,exp):
"Test func with [`export`]() in its doc string"
assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)
If elt
is a class, add_doc_links
looks at parameter names used in __init__
class T:
def __init__(self, add_doc_links): pass
test_eq(add_doc_links('Lets talk about `add_doc_links`'),
'Lets talk about [`add_doc_links`](/showdoc.html#add_doc_links)')
test_eq(add_doc_links('Lets talk about `add_doc_links`', T), 'Lets talk about `add_doc_links`')
test_eq(add_doc_links('Lets talk about `doc_link`'),
'Lets talk about [`doc_link`](/showdoc.html#doc_link)')
test_eq(add_doc_links('Lets talk about `doc_link`', T),
'Lets talk about [`doc_link`](/showdoc.html#doc_link)')
As important as the source code, we want to quickly jump to where the function is defined when we are in a development notebook.
#export
_re_header = re.compile(r"""
# Catches any header in markdown with the title in group 1
^\s* # Beginning of text followed by any number of whitespace
\#+ # One # or more
\s* # Any number of whitespace
(.*) # Catching group with anything
$ # End of text
""", re.VERBOSE)
#export
def colab_link(path):
"Get a link to the notebook at `path` on Colab"
cfg = get_config()
res = f'https://colab.research.google.com/github/{cfg.user}/{cfg.lib_name}/blob/{cfg.branch}/{cfg.path("nbs_path").name}/{path}.ipynb'
display(Markdown(f'[Open `{path}` in Colab]({res})'))
colab_link('02_showdoc')
#export
def get_nb_source_link(func, local=False, is_name=None):
"Return a link to the notebook where `func` is defined."
func = _unwrapped_type_dispatch_func(func)
pref = '' if local else get_config().git_url.replace('github.com', 'nbviewer.jupyter.org/github')+ get_config().path("nbs_path").name+'/'
is_name = is_name or isinstance(func, str)
src = source_nb(func, is_name=is_name, return_all=True)
if src is None: return '' if is_name else get_source_link(func)
find_name,nb_name = src
nb = read_nb(nb_name)
pat = re.compile(f'^{find_name}\s+=|^(def|class)\s+{find_name}\s*\(', re.MULTILINE)
if len(find_name.split('.')) == 2:
clas,func = find_name.split('.')
pat2 = re.compile(f'@patch\s*\ndef\s+{func}\s*\([^:]*:\s*{clas}\s*(?:,|\))')
else: pat2 = None
for i,cell in enumerate(nb['cells']):
if cell['cell_type'] == 'code':
if re.search(pat, cell['source']): break
if pat2 is not None and re.search(pat2, cell['source']): break
if re.search(pat, cell['source']) is None and (pat2 is not None and re.search(pat2, cell['source']) is None):
return '' if is_name else get_function_source(func)
header_pat = re.compile(r'^\s*#+\s*(.*)$')
while i >= 0:
cell = nb['cells'][i]
if cell['cell_type'] == 'markdown' and _re_header.search(cell['source']):
title = _re_header.search(cell['source']).groups()[0]
anchor = '-'.join([s for s in title.split(' ') if len(s) > 0])
return f'{pref}{nb_name}#{anchor}'
i-=1
return f'{pref}{nb_name}'
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))
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 Google Colab.
#export
def nb_source_link(func, is_name=None, disp=True, local=True):
"Show a relative link to the notebook where `func` is defined"
is_name = is_name or isinstance(func, str)
func_name = func if is_name else qual_name(func)
link = get_nb_source_link(func, local=local, is_name=is_name)
text = func_name if local else f'{func_name} (GitHub)'
if disp: display(Markdown(f'[{text}]({link})'))
else: return link
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')
nb_source_link(check_re, local=False)
#export
from fastcore.script import Param
#export
def _format_annos(anno, highlight=False):
"Returns a clean string representation of `anno` from either the `__qualname__` if it is a base class, or `str()` if not"
annos = listify(anno)
if len(annos) == 0: return "None" # If anno is none, listify has a length of 0
new_anno = "(" if len(annos) > 1 else ""
def _inner(o): return getattr(o, '__qualname__', str(o)) if '<' in str(o) else str(o)
for i, anno in enumerate(annos):
new_anno += _inner(anno) if not highlight else f'{doc_link(_inner(anno))}'
# if "." in new_anno: new_anno = new_anno.split('.')[-1]
if len(annos) > 1 and i < len(annos) - 1:
new_anno += ', '
return f'{new_anno})' if len(annos) > 1 else new_anno
#hide
from typing import Union, Tuple, List
test_eq(_format_annos(Union[int,float]), 'typing.Union[int, float]')
test_eq(_format_annos(Tuple[int,float]), 'typing.Tuple[int, float]')
test_ne(_format_annos(Tuple[int,float]), 'typing.Tuple[int,float]')
test_eq(_format_annos((int,float)), '(int, float)')
test_eq(_format_annos(int), 'int')
test_eq(_format_annos(L), 'L')
test_eq(_format_annos(L, highlight=True), '`L`')
test_eq(_format_annos((L,list), highlight=True), '(`L`, `list`)')
test_eq(_format_annos(None), "None")
test_eq(_format_annos(Union[List[str],str]), 'typing.Union[typing.List[str], str]')
#export
def type_repr(t):
"Representation of type `t` (in a type annotation)"
if (isinstance(t, Param)): return f'"{t.help}"'
if getattr(t, '__args__', None):
args = t.__args__
if len(args)==2 and args[1] == type(None):
return f'`Optional`\[{type_repr(args[0])}\]'
reprs = ', '.join([_format_annos(o, highlight=True) for o in args])
return f'{doc_link(get_name(t))}\[{reprs}\]'
else: return doc_link(_format_annos(t))
The representation tries to find doc links if possible.
tst = type_repr(Optional[DocsTestClass])
test_eq(tst, '`Optional`\\[[`DocsTestClass`](/export.html#DocsTestClass)\\]')
tst = type_repr(Union[int, float])
test_eq(tst, '`Union`\\[`int`, `float`\\]')
test_eq(type_repr(Param("description")), '"description"')
#export
_arg_prefixes = {inspect._VAR_POSITIONAL: '\*', inspect._VAR_KEYWORD:'\*\*'}
def format_param(p):
"Formats function param to `param:Type=val` with font weights: param=bold, val=italic"
arg_prefix = _arg_prefixes.get(p.kind, '') # asterisk prefix for *args and **kwargs
res = f"**{arg_prefix}`{p.name}`**"
if hasattr(p, 'annotation') and p.annotation != p.empty: res += f':{type_repr(p.annotation)}'
if p.default != p.empty:
default = getattr(p.default, 'func', p.default) #For partials
if hasattr(default,'__name__'): default = getattr(default, '__name__')
else: default = repr(default)
if is_enum(default.__class__): #Enum have a crappy repr
res += f'=*`{default.__class__.__name__}.{default.name}`*'
else: res += f'=*`{default}`*'
return res
sig = inspect.signature(notebook2script)
params = [format_param(p) for _,p in sig.parameters.items()]
test_eq(params, ['**`fname`**=*`None`*', '**`silent`**=*`False`*', '**`to_dict`**=*`False`*', '**`bare`**=*`False`*'])
def o(a:(list, int)): return a
sig = inspect.signature(o)
params = [format_param(p) for _,p in sig.parameters.items()]
test_eq(params, ['**`a`**:`(list, int)`'])
#export
def _format_enum_doc(enum, full_name):
"Formatted `enum` definition to show in documentation"
vals = ', '.join(enum.__members__.keys())
return f'<code>{full_name}</code>',f'<code>Enum</code> = [{vals}]'
#hide
tst = _format_enum_doc(e, 'e')
test_eq(tst, ('<code>e</code>', '<code>Enum</code> = [a, b]'))
#export
def _escape_chars(s):
return s.replace('_', '\_')
def _format_func_doc(func, full_name=None):
"Formatted `func` definition to show in documentation"
try:
sig = inspect.signature(func)
fmt_params = [format_param(param) for name,param
in sig.parameters.items() if name not in ('self','cls')]
except: fmt_params = []
name = f'<code>{full_name or func.__name__}</code>'
arg_str = f"({', '.join(fmt_params)})"
f_name = f"<code>class</code> {name}" if inspect.isclass(func) else name
return f'{f_name}',f'{name}{arg_str}'
#hide
test_eq(_format_func_doc(notebook2script), ('<code>notebook2script</code>',
'<code>notebook2script</code>(**`fname`**=*`None`*, **`silent`**=*`False`*, **`to_dict`**=*`False`*, **`bare`**=*`False`*)'))
#export
def _format_cls_doc(cls, full_name):
"Formatted `cls` definition to show in documentation"
parent_class = inspect.getclasstree([cls])[-1][0][1][0]
name,args = _format_func_doc(cls, full_name)
if parent_class != object: args += f' :: {doc_link(get_name(parent_class))}'
return name,args
#hide
test_eq(_format_cls_doc(DocsTestClass, 'DocsTestClass'), ('<code>class</code> <code>DocsTestClass</code>',
'<code>DocsTestClass</code>()'))
#export
def _has_docment(elt):
comments = {o.start[0]:_clean_comment(o.string) for o in _tokens(elt) if o.type==COMMENT}
params = _param_locs(elt, returns=True)
comments = [_get_comment(line,arg,comments,params) for line,arg in params.items()]
return any(c is not None for c in comments)
#hide
def _test_a(
a:int, # has two docments
b:int, # Test
):
"A test func"
return a+b
test_eq(_has_docment(_test_a), True)
#hide
def _test_b(
a:int, # has one docment
b:int,
):
"A test func"
return a+b
test_eq(_has_docment(_test_b), True)
#hide
def _test_c(a:int,b:int):
"A test func"
return a+b
test_eq(_has_docment(_test_c), False)
#export
def _generate_arg_string(argument_dict, has_docment=False, monospace=False):
"Turns a dictionary of argument information into a useful docstring"
arg_string = '||Type|Default|'
border_string = '|---|---|---|'
if has_docment:
arg_string += 'Details|'
border_string += '---|'
arg_string+= f'\n{border_string}\n'
for key, item in argument_dict.items():
is_required=True
if key == 'return': continue
if item['default'] != inspect._empty:
if item['default'] == '':
item['default'] = '""'
is_required = False
arg_string += f"|**`{key}`**|"
details_string = ""
if item['anno'] == None: item['anno'] = NoneType
if (item["default"] == None and item['anno'] == NoneType) or item['anno'] == inspect._empty:
details_string += "|"
else:
details_string += f"`{_format_annos(item['anno']).replace('|', 'or')}`|"
details_string += "|" if is_required else f"`{_format_annos(item['default'])}`|"
if has_docment:
if item['docment']:
item['docment'] = item['docment'].replace('\n', '<br />')
details_string += f"{item['docment']}|" if item['docment'] is not None else "*No Content*|"
arg_string += add_doc_links(details_string)
arg_string += '\n'
return arg_string
#hide
args = {
"full": {
"docment":"Test me!",
"anno":int,
"default":0
},
"partial": {
"docment": "Test x2",
"anno": float,
"default": inspect._empty
},
"none": {
"docment": None,
"anno": inspect._empty,
"default": inspect._empty
},
"multitype": {
"docment":"Testing pipe!",
"anno": 'int | float',
"default": inspect._empty
},
"multitype-basetypes" : {
"docment":"Testing typing!",
"anno": Union[int,float],
"default": inspect._empty
},
"multiline-docstring": {
"docment": "This is my first\nThis is my second",
"anno": int,
"default": 2
},
"multi-none": {
"docment": "Blah",
"anno": NoneType,
"default": None
},
"default-none": {
"docment": "Blah blah",
"anno": int,
"default": None
}
}
#hide
_str = "||Type|Default|Details|\n|---|---|---|---|\n|**`full`**|`int`|`0`|Test me!|\n"
test_eq(_generate_arg_string({"full":args["full"]}, has_docment=True), _str)
#hide
_str = "||Type|Default|Details|\n|---|---|---|---|\n|**`partial`**|`float`||Test x2|\n"
test_eq(_generate_arg_string({"partial":args["partial"]}, has_docment=True), _str)
#hide
_str = '||Type|Default|\n|---|---|---|\n|**`none`**|||\n'
test_eq(_generate_arg_string({"none":args["none"]}, has_docment=False), _str)
#hide
_str = '||Type|Default|Details|\n|---|---|---|---|\n|**`multitype`**|`int or float`||Testing pipe!|\n'
test_eq(_generate_arg_string({"multitype":args["multitype"]}, has_docment=True), _str)
#hide
_str = '||Type|Default|Details|\n|---|---|---|---|\n|**`multitype-basetypes`**|`typing.Union[int, float]`||Testing typing!|\n'
test_eq(_generate_arg_string({"multitype-basetypes":args["multitype-basetypes"]}, has_docment=True), _str)
#hide
_str = '||Type|Default|Details|\n|---|---|---|---|\n|**`multiline-docstring`**|`int`|`2`|This is my first<br />This is my second|\n'
test_eq(_generate_arg_string({"multiline-docstring":args["multiline-docstring"]}, has_docment=True), _str)
#hide
_str = '||Type|Default|Details|\n|---|---|---|---|\n|**`default-none`**|`int`|`None`|Blah blah|\n'
test_eq(_generate_arg_string({"default-none":args["default-none"]}, has_docment=True), _str)
#hide
_str = '||Type|Default|Details|\n|---|---|---|---|\n|**`multi-none`**||`None`|Blah|\n'
test_eq(_generate_arg_string({"multi-none":args["multi-none"]}, has_docment=True), _str)
#export
def _generate_return_string(return_dict:dict, has_docment=False):
"Turns a dictionary of return information into a useful docstring"
if return_dict['anno'] is None:
if not return_dict['docment']: return ''
else: return_dict['anno'] = NoneType
anno = _format_annos(return_dict['anno']).replace('|', 'or')
return_string = f"|**Returns**|`{anno}`||"
if has_docment:
if return_dict['docment']:
return_dict['docment'] = return_dict['docment'].replace('\n', '<br />')
else: return_dict['docment'] = ''
return return_string if not has_docment else f"{return_string}{return_dict['docment']}|"
#hide
_return_dict = {
"full":{
"docment": "Sum of parts",
"anno":float,
"default":inspect._empty
},
# We can't have a situation of a `docment` without
# annotations on a return, so we only test an annotation
"partial":{
"docment": None,
"anno":float,
"default":inspect._empty
},
"none":{
"docment": None,
"anno":inspect._empty,
"default":inspect._empty
},
"typing":{
"docment": "Sum of parts",
"anno":Union[int,float],
"default":inspect._empty
},
"anno-none": {
"docment": None,
"anno": None,
"default": inspect._empty
},
"none-with-docment": {
"docment": "We return nothing",
"anno": None,
"default": inspect._empty
}
}
#hide
_str = "|**Returns**|`float`||Sum of parts|"
test_eq(_generate_return_string(_return_dict["full"], has_docment=True), _str)
#hide
_str = "|**Returns**|`float`||"
test_eq(_generate_return_string(_return_dict["partial"], has_docment=False), _str)
#hide
_str = "|**Returns**|`_empty`||"
test_eq(_generate_return_string(_return_dict["none"], has_docment=False), _str)
#hide
_str = "|**Returns**|`typing.Union[int, float]`||Sum of parts|"
test_eq(_generate_return_string(_return_dict["typing"], has_docment=True), _str)
#hide
test_eq(_generate_return_string(_return_dict["anno-none"]), '')
#hide
_str = '|**Returns**|`NoneType`||We return nothing|'
test_eq(_generate_return_string(_return_dict["none-with-docment"], has_docment=True), _str)
#export
def _is_static(func):
"Checks whether `func` is a static method in a class"
name = qual_name(func)
if len(name.split(".")) == 2:
cls, nm = name.split('.')
cls = getattr(sys.modules[func.__module__], cls)
method_type = inspect.getattr_static(cls, nm)
return isinstance(method_type, staticmethod)
return False
#hide
class _A:
def __init__(self,
a, # First number
b # Second number
):
self.a = a
self.b = b
@classmethod
def from_self(cls, *args):
return cls(*args)
@staticmethod
def _add(
a, # First val
b # Second val
): return a+b
def add(self # Should be ignored
): return self._add(self.a, self.b)
test_eq(_is_static(_A), False)
test_eq(_is_static(_A.__init__), False)
test_eq(_is_static(_A.add), False)
test_eq(_is_static(_A._add), True)
test_eq(_is_static(_A.from_self), False)
#export
def _format_args(elt, ment_dict:dict = None, kwargs = [], monospace=False, is_class=False):
"Generates a formatted argument string, potentially from an existing `ment_dict`"
if ment_dict is None:
ment_dict = docments(elt, full=True)
arg_string = ""
return_string = ""
if not _is_static(elt) and is_class:
ment_dict.pop("self", {})
ment_dict.pop("cls", {})
ret = ment_dict.pop("return", None)
has_docment = _has_docment(elt)
if len(ment_dict.keys()) > 0:
if len(kwargs) > 0:
kwarg_dict = filter_keys(ment_dict, lambda x: x in kwargs)
ment_dict = filter_keys(ment_dict, lambda x: x not in kwargs)
arg_string = _generate_arg_string(ment_dict, has_docment)
arg_string += "|||**Valid Keyword Arguments**||\n"
arg_string += _generate_arg_string(kwarg_dict, has_docment, monospace=monospace).replace("||Type|Default|Details|\n|---|---|---|---|\n", "")
else:
arg_string = _generate_arg_string(ment_dict, has_docment, monospace=monospace)
if not ret["anno"] == inspect._empty:
return_string = _generate_return_string(ret, has_docment)
return arg_string + return_string
#hide
def addition(
a:int, # The first number to add
b:float, # The second number to add
) -> (int,float): # The sum of `a` and `b`
"Adds two numbers together"
return a+b
test_eq(_format_args(addition), '||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|`int`||The first number to add|\n|**`b`**|`float`||The second number to add|\n|**Returns**|`(int, float)`||The sum of `a` and `b`|')
#hide
def addition(
# The first number to add
# The starting value
a:int,
# The second number to add
# The addend
b:float = 2
) -> (int,float): # The sum of `a` and `b`
"Adds two numbers together"
return a+b
test_eq(_format_args(addition), '||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|`int`||The first number to add<br />The starting value|\n|**`b`**|`float`|`2`|The second number to add<br />The addend|\n|**Returns**|`(int, float)`||The sum of `a` and `b`|')
#hide
def addition(
# The first number to add
# The starting value
a:int,
# The second number to add
# The addend
b:float = 2
# The sum of `a` and `b`
# Our return
) -> (int,float):
"Adds two numbers together"
return a+b
test_eq(_format_args(addition), '||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|`int`||The first number to add<br />The starting value|\n|**`b`**|`float`|`2`|The second number to add<br />The addend|\n|**Returns**|`(int, float)`||The sum of `a` and `b`<br />Our return|')
#hide
test_eq(_format_args(_A._add, is_class=True), '||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|||First val|\n|**`b`**|||Second val|\n')
test_eq(_format_args(_A.__init__, is_class=True), '||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|||First number|\n|**`b`**|||Second number|\n')
test_eq(_format_args(_A.add, is_class=True), '')
#export
def is_source_available(
elt, # A python object
):
"Checks if it is possible to return the source code of `elt` mimicking `inspect.getfile`"
if inspect.ismodule(elt):
return True if getattr(object, '__file__', None) else False
elif isclass(elt):
if hasattr(elt, '__module__'):
module = sys.modules.get(elt.__module__)
return True if getattr(module, '__file__', None) else False
elif getattr(elt, '__name__', None) == "<lambda>":
return False
elif inspect.ismethod(elt) or inspect.isfunction(elt) or inspect.istraceback(elt) or inspect.isframe(elt) or inspect.iscode(elt):
return True
elif is_enum(elt):
return False
return False
#hide
from fastcore.dispatch import typedispatch
@typedispatch
def _typ_test(
a:int, # An integer
b:int # A second integer
) -> float:
"Perform op"
return a+b
class _TypTest:
def __init__(a,b):
self.a = a
self.b = b
class abc(object):
@staticmethod
def static_method():
print("Hi,this tutorial is about static class in python")
@classmethod
def class_method(cls):
print("Hi,this tutorial is about static class in python")
@patch
def myTestFunc(self:_TypTest):
print("This is a patched func")
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def noofsides(self):
pass
#hide
test_eq(is_source_available(lambda x: x+1), False) # lambda
test_eq(is_source_available(int), False) # builtin type
test_eq(is_source_available(addition), True) # func
test_eq(is_source_available(getattr), False) # builtin func
test_eq(is_source_available(_typ_test), False) # typedispatch
test_eq(is_source_available(e), False) # enum
test_eq(is_source_available(_TypTest), False) # class
test_eq(is_source_available(_TypTest.__init__), True) # class
test_eq(is_source_available(_TypTest.myTestFunc), True) # patch
test_eq(is_source_available(abc.static_method), True) # static
test_eq(is_source_available(abc), False) # static object
test_eq(is_source_available(abc.class_method), True) # class method
test_eq(is_source_available(AbstractClass), False) # abstract class
test_eq(is_source_available(AbstractClass.noofsides), True) # abstract method
#export
def _handle_delegates(elt):
"Generates a `docment` dict handling `@delegates` and returns names of the kwargs in `elt`"
kwargs = []
arg_dict = docments(elt, full=True)
delwrap_dict = docments(elt.__delwrap__, full=True)
drop = arg_dict.keys()
for k,v in arg_dict.items():
if k in delwrap_dict.keys() and v["docment"] is None and k != "return":
kwargs.append(k)
if delwrap_dict[k]["docment"] is not None:
v["docment"] = delwrap_dict[k]["docment"] + f" passed to `{qual_name(elt.__delwrap__)}`"
else:
v['docment'] = f"Argument passed to `{qual_name(elt.__delwrap__)}`"
return arg_dict, kwargs
#hide
from fastcore.meta import delegates
def _a(
a:int=2, # First
):
return a
@delegates(_a)
def _b(
b:str, # Second
**kwargs
):
return b, (_a(**kwargs))
_docment = ({"b":AttrDict({"docment":"Second","anno":str,"default":inspect._empty}),
"a":AttrDict({"docment":"First passed to `_a`","anno":int,"default":2}),
"return": AttrDict({"docment":None,"anno":inspect._empty,"default":inspect._empty})},
["a"])
test_eq(_handle_delegates(_b),_docment)
#export
def _get_docments(elt, with_return=False, ment_dict=None, kwargs=[], monospace=False, is_class=False):
"Grabs docments for `elt` and formats with a potential `ment_dict` and valid kwarg names"
s = f"\n\n{_format_args(elt, ment_dict=ment_dict, kwargs=kwargs, monospace=monospace, is_class=is_class)}"
if not with_return: s = s.split("|**Returns**|")[0]
return s
#hide
_s = '\n\n||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|`int`|`2`|First|\n'
test_eq(_get_docments(_a), _s)
_s = '\n\n||Type|Default|Details|\n|---|---|---|---|\n|**`b`**|`str`||Second|\n|**`a`**|`int`|`2`|*No Content*|\n'
test_eq(_get_docments(_b), _s)
#export
def show_doc(elt, doc_string:bool=True, name=None, title_level=None, disp=True, default_cls_level=2, show_all_docments=False, verbose=False):
"Show documentation for element `elt` with potential input documentation. Supported types: class, function, and enum."
elt = getattr(elt, '__func__', elt)
qname = name or qual_name(elt)
is_class = '.' in qname or inspect.isclass
if inspect.isclass(elt):
if is_enum(elt): name,args = _format_enum_doc(elt, qname)
else: name,args = _format_cls_doc (elt, qname)
elif callable(elt): name,args = _format_func_doc(elt, qname)
else: name,args = f"<code>{qname}</code>", ''
link = get_source_link(elt)
source_link = f'<a href="{link}" class="source_link" style="float:right">[source]</a>'
title_level = title_level or (default_cls_level if inspect.isclass(elt) else 4)
doc = f'<h{title_level} id="{qname}" class="doc_header">{name}{source_link}</h{title_level}>'
doc += f'\n\n> {args}\n\n' if len(args) > 0 else '\n\n'
s = ''
try:
monospace = get_config().d.getboolean('monospace_docstrings', False)
except FileNotFoundError:
monospace = False
if doc_string and inspect.getdoc(elt):
s = inspect.getdoc(elt)
# doc links don't work inside markdown pre/code blocks
s = f'```\n{s}\n```' if monospace else add_doc_links(s, elt)
doc += s
if len(args) > 0:
if hasattr(elt, '__init__') and isclass(elt):
elt = elt.__init__
if is_source_available(elt):
if show_all_docments or _has_docment(elt):
if hasattr(elt, "__delwrap__"):
arg_dict, kwargs = _handle_delegates(elt)
doc += _get_docments(elt, ment_dict=arg_dict, with_return=True, kwargs=kwargs, monospace=monospace, is_class=is_class)
else:
doc += _get_docments(elt, monospace=monospace, is_class=is_class)
elif verbose:
print(f'Warning: `docments` annotations will not work for built-in modules, classes, functions, and `enums` and are unavailable for {qual_name(elt)}. They will not be shown')
if disp: display(Markdown(doc))
else: return doc
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
show_doc(notebook2script)
will display
show_doc(notebook2script)
notebook2script
[source]
notebook2script
(fname
=None
,silent
=False
,to_dict
=False
,bare
=False
)
Convert notebooks matching fname
to modules
#hide
from fastcore.meta import delegates
def _a(
a:int=2, # First
):
return a
@delegates(_a)
def _b(
b:str, # Second
**kwargs
):
return b, (_a(**kwargs))
def _c(
b:str, # Second
a:int=2, # Blah
):
return b, a
@delegates(_c)
def _d(
c:int, # First
b:str,
**kwargs
):
return c, _c(b, **kwargs)
_str = '<h4 id="_b" class="doc_header"><code>_b</code><a href="__main__.py#L8" class="source_link" style="float:right">[source]</a></h4>\n\n> <code>_b</code>(**`b`**:`str`, **`a`**:`int`=*`2`*)\n\n\n\n||Type|Default|Details|\n|---|---|---|---|\n|**`b`**|`str`||Second|\n|||**Valid Keyword Arguments**||\n|**`a`**|`int`|`2`|First passed to `_a`|\n'
test_eq(show_doc(_b, disp=False), _str)
_str = '<h4 id="_d" class="doc_header"><code>_d</code><a href="__main__.py#L20" class="source_link" style="float:right">[source]</a></h4>\n\n> <code>_d</code>(**`c`**:`int`, **`b`**:`str`, **`a`**:`int`=*`2`*)\n\n\n\n||Type|Default|Details|\n|---|---|---|---|\n|**`c`**|`int`||First|\n|||**Valid Keyword Arguments**||\n|**`b`**|`str`||Second passed to `_c`|\n|**`a`**|`int`|`2`|Blah passed to `_c`|\n'
test_eq(show_doc(_d, disp=False), _str)
_str = '<h4 id="_A._add" class="doc_header"><code>_A._add</code><a href="__main__.py#L14" class="source_link" style="float:right">[source]</a></h4>\n\n> <code>_A._add</code>(**`a`**, **`b`**)\n\n\n\n||Type|Default|Details|\n|---|---|---|---|\n|**`a`**|||First val|\n|**`b`**|||Second val|\n'
test_eq(show_doc(_A._add, disp=False), _str)
#hide
def _test_linked_param(
show_doc:callable # The function `show_doc`
):
return show_doc
assert show_doc(_test_linked_param, disp=False).count("`show_doc`") == 3
assert show_doc(_test_linked_param, disp=False).count("**`show_doc`**") == 2 # In arg string and docment default
assert show_doc(_test_linked_param, disp=False).count("[`show_doc`]") == 1 # In docment itself
#hide
def t(a,exp):
"Test func with `export` in its doc string"
assert '[`export`](/export.html)' in show_doc(t, disp=False)
def t(a,export):
"Test func that uses 'export' as a parameter name and has `export` in its doc string"
assert '[`export`](/export.html)' not in show_doc(t, disp=False)
#hide
show_doc(DocsTestClass)
#hide
show_doc(DocsTestClass.test)
DocsTestClass.test
[source]
DocsTestClass.test
()
#hide
show_doc(check_re)
#hide
show_doc(e)
#hide
def test_func_with_args_and_links(foo, bar):
"""
Doc link: `show_doc`.
Args:
foo: foo
bar: bar
Returns:
None
"""
pass
show_doc(test_func_with_args_and_links)
get_config()["monospace_docstrings"] = "True"
show_doc(test_func_with_args_and_links)
get_config()["monospace_docstrings"] = "False"
#export
def md2html(md):
"Convert markdown `md` to HTML code"
import nbconvert
if nbconvert.__version__ < '5.5.0': return HTMLExporter().markdown2html(md)
else: return HTMLExporter().markdown2html(collections.defaultdict(lambda: collections.defaultdict(dict)), md)
#export
def get_doc_link(func):
mod = inspect.getmodule(func)
module = mod.__name__.replace('.', '/') + '.py'
try:
nbdev_mod = importlib.import_module(mod.__package__.split('.')[0] + '._nbdev')
try_pack = source_nb(func, mod=nbdev_mod)
if try_pack:
page = '.'.join(try_pack.partition('_')[-1:]).replace('.ipynb', '')
return f'{nbdev_mod.doc_url}{page}#{qual_name(func)}'
except: return None
test_eq(get_doc_link(notebook2script), 'https://nbdev.fast.ai/export#notebook2script')
#test
from nbdev.sync import get_name
test_eq(get_doc_link(get_name), 'https://nbdev.fast.ai/sync#get_name')
#export
# Fancy CSS needed to make raw Jupyter rendering look nice
_TABLE_CSS = """<style>
table { border-collapse: collapse; border:thin solid #dddddd; margin: 25px 0px; ; }
table tr:first-child { background-color: #FFF}
table thead th { background-color: #eee; color: #000; text-align: center;}
tr, th, td { border: 1px solid #ccc; border-width: 1px 0 0 1px; border-collapse: collapse;
padding: 5px; }
tr:nth-child(even) {background: #eee;}</style>"""
#export
def doc(elt:int, show_all_docments:bool=True):
"Show `show_doc` info in preview window when used in a notebook"
md = show_doc(elt, disp=False, show_all_docments=show_all_docments)
doc_link = get_doc_link(elt)
if doc_link is not None:
md += f'\n\n<a href="{doc_link}" target="_blank" rel="noreferrer noopener">Show in docs</a>'
output = md2html(md)
if IN_COLAB: get_ipython().run_cell_magic(u'html', u'', output + _TABLE_CSS)
else:
try: page.page({'text/html': output + _TABLE_CSS})
except: display(Markdown(md))
#hide
from nbdev.export import notebook2script
notebook2script()
Converted 00_export.ipynb. Converted 01_sync.ipynb. Converted 02_showdoc.ipynb. Converted 03_export2html.ipynb. Converted 04_test.ipynb. Converted 05_merge.ipynb. Converted 06_cli.ipynb. Converted 07_clean.ipynb. Converted 99_search.ipynb. Converted example.ipynb. Converted index.ipynb. Converted nbdev_comments.ipynb. Converted tutorial.ipynb. Converted tutorial_colab.ipynb.