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}'
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, skip_params=('self', 'cls')) 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