{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": false }, "outputs": [], "source": [ "#hide\n", "#default_exp showdoc\n", "#default_cls_lvl 3\n", "from nbdev.showdoc import show_doc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from nbdev.imports import *\n", "from nbdev.export import *\n", "from nbdev.sync import *\n", "from nbconvert import HTMLExporter\n", "from fastcore.utils import IN_NOTEBOOK\n", "\n", "if IN_NOTEBOOK:\n", " from IPython.display import Markdown,display\n", " from IPython.core import page" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Show doc\n", "\n", "> Functions to show the doc cells in notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Gather the information" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The inspect module lets us know quickly if an object is a function or a class but it doesn't distinguish classes and enums." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def is_enum(cls):\n", " \"Check if `cls` is an enum or another type of class\"\n", " return type(cls) in (enum.Enum, enum.EnumMeta)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "e = enum.Enum('e', 'a b')\n", "assert is_enum(e)\n", "assert not is_enum(e.__class__)\n", "assert not is_enum(int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Links to documentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def is_lib_module(name):\n", " \"Test if `name` is a library module.\"\n", " if name.startswith('_'): return False\n", " try:\n", " _ = importlib.import_module(f'{Config().lib_name}.{name}')\n", " return True\n", " except: return False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert is_lib_module('export')\n", "assert not is_lib_module('transform')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "re_digits_first = re.compile('^[0-9]+[a-z]*_')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def try_external_doc_link(name, packages):\n", " \"Try to find a doc link for `name` in `packages`\"\n", " for p in packages:\n", " try:\n", " mod = importlib.import_module(f\"{p}._nbdev\")\n", " try_pack = source_nb(name, is_name=True, mod=mod)\n", " if try_pack:\n", " page = re_digits_first.sub('', try_pack).replace('.ipynb', '')\n", " return f'{mod.doc_url}{page}#{name}'\n", " except ModuleNotFoundError: return None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function will only work for other packages built with `nbdev`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(try_external_doc_link('get_name', ['nbdev']), 'https://nbdev.fast.ai/sync#get_name')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def is_doc_name(name):\n", " \"Test if `name` corresponds to a notebook that could be converted to a doc page\"\n", " for f in Config().path(\"nbs_path\").glob(f'*{name}.ipynb'):\n", " if re_digits_first.sub('', f.name) == f'{name}.ipynb': return True\n", " return False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(is_doc_name('flaags'),False)\n", "test_eq(is_doc_name('export'),True)\n", "test_eq(is_doc_name('index'),True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def doc_link(name, include_bt=True):\n", " \"Create link to documentation for `name`.\"\n", " cname = f'`{name}`' if include_bt else name\n", " try:\n", " #Link to modules\n", " if is_lib_module(name) and is_doc_name(name): return f\"[{cname}]({Config().doc_baseurl}{name}.html)\"\n", " #Link to local functions\n", " try_local = source_nb(name, is_name=True)\n", " if try_local:\n", " page = re_digits_first.sub('', try_local).replace('.ipynb', '')\n", " return f'[{cname}]({Config().doc_baseurl}{page}.html#{name})'\n", " ##Custom links\n", " mod = get_nbdev_module()\n", " link = mod.custom_doc_links(name)\n", " return f'[{cname}]({link})' if link is not None else cname\n", " except: return cname" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`.\n", "\n", "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):\n", "``` python\n", "def custom_doc_links(name): \n", " from nbdev.showdoc import try_external_doc_link\n", " return try_external_doc_link(name, ['fastcore', 'nbdev'])\n", "```\n", "\n", "Please note that module links only work if your notebook names \"correspond\" to your module names:\n", "\n", "| Notebook name | Doc name | Module name | Module file | Can doc link? |\n", "|--------------------|----------------|-------------|--------------|---------------|\n", "| export.ipynb | export.html | export | export.py | Yes |\n", "| 00_export.ipynb | export.html | export | export.py | Yes |\n", "| 00a_export.ipynb | export.html | export | export.py | Yes |\n", "| export_1.ipynb | export_1.html | export | export.py | No |\n", "| 03_data.core.ipynb | data.core.html | data.core | data/core.py | Yes |\n", "| 03_data_core.ipynb | data_core.html | data.core | data/core.py | No |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(doc_link('export'), f'[`export`](/export.html)')\n", "test_eq(doc_link('DocsTestClass'), f'[`DocsTestClass`](/export.html#DocsTestClass)')\n", "test_eq(doc_link('DocsTestClass.test'), f'[`DocsTestClass.test`](/export.html#DocsTestClass.test)')\n", "test_eq(doc_link('Tenso'),'`Tenso`')\n", "test_eq(doc_link('_nbdev'), f'`_nbdev`')\n", "test_eq(doc_link('__main__'), f'`__main__`')\n", "test_eq(doc_link('flags'), '`flags`') # we won't have a flags doc page even though we do have a flags module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_re_backticks = re.compile(r\"\"\"\n", "# Catches any link of the form \\[`obj`\\](old_link) or just `obj`,\n", "# to either update old links or add the link to the docs of obj\n", "\\[` # Opening [ and `\n", "([^`]*) # Catching group with anything but a `\n", "`\\] # ` then closing ]\n", "(?: # Beginning of non-catching group\n", "\\( # Opening (\n", "[^)]* # Anything but a closing )\n", "\\) # Closing )\n", ") # End of non-catching group\n", "| # OR\n", "` # Opening `\n", "([^`]*) # Anything but a `\n", "` # Closing `\n", "\"\"\", re.VERBOSE)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def add_doc_links(text, elt=None):\n", " \"Search for doc links for any item between backticks in `text` and insert them\"\n", " def _replace_link(m): \n", " try: \n", " if m.group(2) in inspect.signature(elt).parameters: return f'`{m.group(2)}`'\n", " except: pass\n", " return doc_link(m.group(1) or m.group(2))\n", " return _re_backticks.sub(_replace_link, text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tst = add_doc_links('This is an example of `DocsTestClass`')\n", "test_eq(tst, \"This is an example of [`DocsTestClass`](/export.html#DocsTestClass)\")\n", "tst = add_doc_links('This is an example of [`DocsTestClass`](old_link.html)')\n", "test_eq(tst, \"This is an example of [`DocsTestClass`](/export.html#DocsTestClass)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Names in backticks will not be converted to links if `elt` has a parameter of the same name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def t(a,export):\n", " \"Test func that uses 'export' as a parameter name and has `export` in its doc string\"\n", "assert '[`export`](/export.html)' not in add_doc_links(t.__doc__, t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Names in backticks _used in markdown links_ will be updated like normal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def t(a,export):\n", " \"Test func that uses 'export' as a parameter name and has [`export`]() in its doc string\"\n", "assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "# if the name in backticks is not a param name, links will be added/updated like normal\n", "def t(a,exp):\n", " \"Test func with `export` in its doc string\"\n", "assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)\n", "def t(a,exp):\n", " \"Test func with [`export`]() in its doc string\"\n", "assert '[`export`](/export.html)' in add_doc_links(t.__doc__, t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If `elt` is a class, `add_doc_links` looks at parameter names used in `__init__`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class T:\n", " def __init__(self, add_doc_links): pass\n", "test_eq(add_doc_links('Lets talk about `add_doc_links`'), \n", " 'Lets talk about [`add_doc_links`](/showdoc.html#add_doc_links)')\n", "test_eq(add_doc_links('Lets talk about `add_doc_links`', T), 'Lets talk about `add_doc_links`')\n", "test_eq(add_doc_links('Lets talk about `doc_link`'), \n", " 'Lets talk about [`doc_link`](/showdoc.html#doc_link)')\n", "test_eq(add_doc_links('Lets talk about `doc_link`', T), \n", " 'Lets talk about [`doc_link`](/showdoc.html#doc_link)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Links to source" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _is_type_dispatch(x): return type(x).__name__ == \"TypeDispatch\"\n", "def _unwrapped_type_dispatch_func(x): return x.first() if _is_type_dispatch(x) else x\n", "\n", "def _is_property(x): return type(x)==property\n", "def _has_property_getter(x): return _is_property(x) and hasattr(x, 'fget') and hasattr(x.fget, 'func')\n", "def _property_getter(x): return x.fget.func if _has_property_getter(x) else x\n", "\n", "def _unwrapped_func(x):\n", " x = _unwrapped_type_dispatch_func(x)\n", " x = _property_getter(x)\n", " return x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def get_source_link(func):\n", " \"Return link to `func` in source code\"\n", " func = _unwrapped_func(func)\n", " try: line = inspect.getsourcelines(func)[1]\n", " except Exception: return ''\n", " mod = inspect.getmodule(func)\n", " module = mod.__name__.replace('.', '/') + '.py'\n", " try:\n", " nbdev_mod = importlib.import_module(mod.__package__.split('.')[0] + '._nbdev')\n", " return f\"{nbdev_mod.git_url}{module}#L{line}\"\n", " except: return f\"{module}#L{line}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "assert get_source_link(DocsTestClass.test).startswith(Config().git_url + 'nbdev/export.py')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "#test\n", "from fastcore.foundation import L\n", "assert get_source_link(L).startswith(\"https://github.com/fastai/fastcore/tree/master/fastcore/foundation.py\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As important as the source code, we want to quickly jump to where the function is defined when we are in a development notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_re_header = re.compile(r\"\"\"\n", "# Catches any header in markdown with the title in group 1\n", "^\\s* # Beginning of text followed by any number of whitespace\n", "\\#+ # One # or more\n", "\\s* # Any number of whitespace\n", "(.*) # Catching group with anything\n", "$ # End of text\n", "\"\"\", re.VERBOSE)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def colab_link(path):\n", " \"Get a link to the notebook at `path` on Colab\"\n", " cfg = Config()\n", " res = f'https://colab.research.google.com/github/{cfg.user}/{cfg.lib_name}/blob/{cfg.branch}/{cfg.path(\"nbs_path\").name}/{path}.ipynb'\n", " display(Markdown(f'[Open `{path}` in Colab]({res})'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "[Open `02_showdoc` in Colab](https://colab.research.google.com/github/fastai/nbdev/blob/master/nbs/02_showdoc.ipynb)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "colab_link('02_showdoc')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def get_nb_source_link(func, local=False, is_name=None):\n", " \"Return a link to the notebook where `func` is defined.\"\n", " func = _unwrapped_type_dispatch_func(func)\n", " pref = '' if local else Config().git_url.replace('github.com', 'nbviewer.jupyter.org/github')+ Config().path(\"nbs_path\").name+'/'\n", " is_name = is_name or isinstance(func, str)\n", " src = source_nb(func, is_name=is_name, return_all=True)\n", " if src is None: return '' if is_name else get_source_link(func)\n", " find_name,nb_name = src\n", " nb = read_nb(nb_name)\n", " pat = re.compile(f'^{find_name}\\s+=|^(def|class)\\s+{find_name}\\s*\\(', re.MULTILINE)\n", " if len(find_name.split('.')) == 2:\n", " clas,func = find_name.split('.')\n", " pat2 = re.compile(f'@patch\\s*\\ndef\\s+{func}\\s*\\([^:]*:\\s*{clas}\\s*(?:,|\\))')\n", " else: pat2 = None\n", " for i,cell in enumerate(nb['cells']):\n", " if cell['cell_type'] == 'code':\n", " if re.search(pat, cell['source']): break\n", " if pat2 is not None and re.search(pat2, cell['source']): break\n", " if re.search(pat, cell['source']) is None and (pat2 is not None and re.search(pat2, cell['source']) is None):\n", " return '' if is_name else get_function_source(func)\n", " header_pat = re.compile(r'^\\s*#+\\s*(.*)$')\n", " while i >= 0:\n", " cell = nb['cells'][i]\n", " if cell['cell_type'] == 'markdown' and _re_header.search(cell['source']):\n", " title = _re_header.search(cell['source']).groups()[0]\n", " anchor = '-'.join([s for s in title.split(' ') if len(s) > 0])\n", " return f'{pref}{nb_name}#{anchor}'\n", " i-=1\n", " return f'{pref}{nb_name}'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(get_nb_source_link(DocsTestClass.test), get_nb_source_link(DocsTestClass))\n", "test_eq(get_nb_source_link('DocsTestClass'), get_nb_source_link(DocsTestClass))\n", "\n", "test_eq(get_nb_source_link(check_re, local=True), f'00_export.ipynb#Finding-patterns')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def nb_source_link(func, is_name=None, disp=True, local=True):\n", " \"Show a relative link to the notebook where `func` is defined\"\n", " is_name = is_name or isinstance(func, str)\n", " func_name = func if is_name else qual_name(func)\n", " link = get_nb_source_link(func, local=local, is_name=is_name)\n", " text = func_name if local else f'{func_name} (GitHub)'\n", " if disp: display(Markdown(f'[{text}]({link})'))\n", " else: return link" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "[check_re (GitHub)](https://nbviewer.jupyter.org/github/fastai/nbdev/tree/master/nbs/00_export.ipynb#Finding-patterns)" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "test_eq(nb_source_link(check_re, disp=False), f'00_export.ipynb#Finding-patterns')\n", "test_eq(nb_source_link('check_re', disp=False), f'00_export.ipynb#Finding-patterns')\n", "nb_source_link(check_re, local=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show documentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastcore.script import Param" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def type_repr(t):\n", " \"Representation of type `t` (in a type annotation)\"\n", " if (isinstance(t, Param)): return f'\"{t.help}\"'\n", " if getattr(t, '__args__', None):\n", " args = t.__args__\n", " if len(args)==2 and args[1] == type(None):\n", " return f'`Optional`\\[{type_repr(args[0])}\\]'\n", " reprs = ', '.join([type_repr(o) for o in args])\n", " return f'{doc_link(get_name(t))}\\[{reprs}\\]'\n", " else: return doc_link(get_name(t))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The representation tries to find doc links if possible." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tst = type_repr(Optional[DocsTestClass])\n", "test_eq(tst, '`Optional`\\\\[[`DocsTestClass`](/export.html#DocsTestClass)\\\\]')\n", "tst = type_repr(Union[int, float])\n", "test_eq(tst, '`Union`\\\\[`int`, `float`\\\\]')\n", "test_eq(type_repr(Param(\"description\")), '\"description\"')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_arg_prefixes = {inspect._VAR_POSITIONAL: '\\*', inspect._VAR_KEYWORD:'\\*\\*'}\n", "\n", "def format_param(p):\n", " \"Formats function param to `param:Type=val` with font weights: param=bold, val=italic\"\n", " arg_prefix = _arg_prefixes.get(p.kind, '') # asterisk prefix for *args and **kwargs\n", " res = f\"**{arg_prefix}`{p.name}`**\"\n", " if hasattr(p, 'annotation') and p.annotation != p.empty: res += f':{type_repr(p.annotation)}'\n", " if p.default != p.empty:\n", " default = getattr(p.default, 'func', p.default) #For partials\n", " if hasattr(default,'__name__'): default = getattr(default, '__name__')\n", " else: default = repr(default)\n", " if is_enum(default.__class__): #Enum have a crappy repr\n", " res += f'=*`{default.__class__.__name__}.{default.name}`*'\n", " else: res += f'=*`{default}`*'\n", " return res" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sig = inspect.signature(notebook2script)\n", "params = [format_param(p) for _,p in sig.parameters.items()]\n", "test_eq(params, ['**`fname`**=*`None`*', '**`silent`**=*`False`*', '**`to_dict`**=*`False`*', '**`bare`**=*`False`*', '**`recursive`**=*`False`*'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _format_enum_doc(enum, full_name):\n", " \"Formatted `enum` definition to show in documentation\"\n", " vals = ', '.join(enum.__members__.keys())\n", " return f'{full_name}',f'Enum = [{vals}]'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "tst = _format_enum_doc(e, 'e')\n", "test_eq(tst, ('e', 'Enum = [a, b]'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _escape_chars(s):\n", " return s.replace('_', '\\_')\n", "\n", "def _format_func_doc(func, full_name=None):\n", " \"Formatted `func` definition to show in documentation\"\n", " try:\n", " sig = inspect.signature(func)\n", " fmt_params = [format_param(param) for name,param\n", " in sig.parameters.items() if name not in ('self','cls')]\n", " except: fmt_params = []\n", " name = f'{full_name or func.__name__}'\n", " arg_str = f\"({', '.join(fmt_params)})\"\n", " f_name = f\"class {name}\" if inspect.isclass(func) else name\n", " return f'{f_name}',f'{name}{arg_str}'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "test_eq(_format_func_doc(notebook2script), ('notebook2script', \n", " 'notebook2script(**`fname`**=*`None`*, **`silent`**=*`False`*, **`to_dict`**=*`False`*, **`bare`**=*`False`*, **`recursive`**=*`False`*)'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _format_cls_doc(cls, full_name):\n", " \"Formatted `cls` definition to show in documentation\"\n", " parent_class = inspect.getclasstree([cls])[-1][0][1][0]\n", " name,args = _format_func_doc(cls, full_name)\n", " if parent_class != object: args += f' :: {doc_link(get_name(parent_class))}'\n", " return name,args" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "test_eq(_format_cls_doc(DocsTestClass, 'DocsTestClass'), ('class DocsTestClass', \n", " 'DocsTestClass()'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def show_doc(elt, doc_string=True, name=None, title_level=None, disp=True, default_cls_level=2):\n", " \"Show documentation for element `elt`. Supported types: class, function, and enum.\"\n", " elt = getattr(elt, '__func__', elt)\n", " qname = name or qual_name(elt)\n", " if inspect.isclass(elt):\n", " if is_enum(elt): name,args = _format_enum_doc(elt, qname)\n", " else: name,args = _format_cls_doc (elt, qname)\n", " elif callable(elt): name,args = _format_func_doc(elt, qname)\n", " else: name,args = f\"{qname}\", ''\n", " link = get_source_link(elt)\n", " source_link = f'[source]'\n", " title_level = title_level or (default_cls_level if inspect.isclass(elt) else 4)\n", " doc = f'{name}{source_link}'\n", " doc += f'\\n\\n> {args}\\n\\n' if len(args) > 0 else '\\n\\n'\n", " if doc_string and inspect.getdoc(elt):\n", " s = inspect.getdoc(elt)\n", " # show_doc is used by doc so should not rely on Config\n", " try: monospace = (Config().get('monospace_docstrings') == 'True')\n", " except: monospace = False\n", " # doc links don't work inside markdown pre/code blocks\n", " s = f'```\\n{s}\\n```' if monospace else add_doc_links(s, elt)\n", " doc += s\n", " if disp: display(Markdown(doc))\n", " else: return doc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For instance\n", "```python\n", "show_doc(notebook2script)\n", "```\n", "will display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

notebook2script[source]

\n", "\n", "> notebook2script(**`fname`**=*`None`*, **`silent`**=*`False`*, **`to_dict`**=*`False`*, **`bare`**=*`False`*, **`recursive`**=*`False`*)\n", "\n", "Convert notebooks matching `fname` to modules" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show_doc(notebook2script)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "def t(a,exp):\n", " \"Test func with `export` in its doc string\"\n", "assert '[`export`](/export.html)' in show_doc(t, disp=False)\n", "def t(a,export):\n", " \"Test func that uses 'export' as a parameter name and has `export` in its doc string\"\n", "assert '[`export`](/export.html)' not in show_doc(t, disp=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integration test -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

class DocsTestClass[source]

\n", "\n", "> DocsTestClass()\n", "\n", "for tests only" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#hide\n", "show_doc(DocsTestClass)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

DocsTestClass.test[source]

\n", "\n", "> DocsTestClass.test()\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#hide\n", "show_doc(DocsTestClass.test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

check_re[source]

\n", "\n", "> check_re(**`cell`**, **`pat`**, **`code_only`**=*`True`*)\n", "\n", "Check if `cell` contains a line with regex `pat`" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#hide\n", "show_doc(check_re)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

e[source]

\n", "\n", "> Enum = [a, b]\n", "\n", "An enumeration." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#hide\n", "show_doc(e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "

test_func_with_args_and_links[source]

\n", "\n", "> test_func_with_args_and_links(**`foo`**, **`bar`**)\n", "\n", "Doc link: [`show_doc`](/showdoc.html#show_doc).\n", "Args:\n", " foo: foo\n", " bar: bar\n", "Returns:\n", " None" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "

test_func_with_args_and_links[source]

\n", "\n", "> test_func_with_args_and_links(**`foo`**, **`bar`**)\n", "\n", "```\n", "Doc link: `show_doc`.\n", "Args:\n", " foo: foo\n", " bar: bar\n", "Returns:\n", " None\n", "```" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#hide\n", "def test_func_with_args_and_links(foo, bar):\n", " \"\"\"\n", " Doc link: `show_doc`.\n", " Args:\n", " foo: foo\n", " bar: bar\n", " Returns:\n", " None\n", " \"\"\"\n", " pass\n", "\n", "show_doc(test_func_with_args_and_links)\n", "Config()[\"monospace_docstrings\"] = \"True\"\n", "show_doc(test_func_with_args_and_links)\n", "Config()[\"monospace_docstrings\"] = \"False\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The doc command" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def md2html(md):\n", " \"Convert markdown `md` to HTML code\"\n", " import nbconvert\n", " if nbconvert.__version__ < '5.5.0': return HTMLExporter().markdown2html(md)\n", " else: return HTMLExporter().markdown2html(collections.defaultdict(lambda: collections.defaultdict(dict)), md)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def get_doc_link(func):\n", " mod = inspect.getmodule(func)\n", " module = mod.__name__.replace('.', '/') + '.py'\n", " try:\n", " nbdev_mod = importlib.import_module(mod.__package__.split('.')[0] + '._nbdev')\n", " try_pack = source_nb(func, mod=nbdev_mod)\n", " if try_pack:\n", " page = '.'.join(try_pack.partition('_')[-1:]).replace('.ipynb', '')\n", " return f'{nbdev_mod.doc_url}{page}#{qual_name(func)}'\n", " except: return None" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(get_doc_link(notebook2script), 'https://nbdev.fast.ai/export#notebook2script')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#test\n", "from nbdev.sync import get_name\n", "test_eq(get_doc_link(get_name), 'https://nbdev.fast.ai/sync#get_name')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def doc(elt):\n", " \"Show `show_doc` info in preview window when used in a notebook\"\n", " md = show_doc(elt, disp=False)\n", " doc_link = get_doc_link(elt)\n", " if doc_link is not None:\n", " md += f'\\n\\nShow in docs'\n", " output = md2html(md)\n", " if IN_COLAB: get_ipython().run_cell_magic(u'html', u'', output)\n", " else:\n", " try: page.page({'text/html': output})\n", " except: display(Markdown(md))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converted 00_export.ipynb.\n", "Converted 01_sync.ipynb.\n", "Converted 02_showdoc.ipynb.\n", "Converted 03_export2html.ipynb.\n", "Converted 04_test.ipynb.\n", "Converted 05_merge.ipynb.\n", "Converted 06_cli.ipynb.\n", "Converted 07_clean.ipynb.\n", "Converted 99_search.ipynb.\n", "Converted example.ipynb.\n", "Converted index.ipynb.\n", "Converted tutorial.ipynb.\n" ] } ], "source": [ "#hide\n", "from nbdev.export import notebook2script\n", "notebook2script()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "split_at_heading": true }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }