-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathshowdoc.py
More file actions
67 lines (56 loc) · 2.59 KB
/
showdoc.py
File metadata and controls
67 lines (56 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Display symbol documentation in notebook and website"""
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/08_showdoc.ipynb.
# %% ../nbs/api/08_showdoc.ipynb #7f371f15
from __future__ import annotations
from .doclinks import *
from .config import get_config
from fastcore.docments import *
from fastcore.utils import *
from importlib import import_module
import inspect, sys
from collections import OrderedDict
from textwrap import fill
from types import FunctionType
# %% auto #0
__all__ = ['BasicMarkdownRenderer', 'show_doc', 'doc', 'showdoc_nm', 'colab_link']
# %% ../nbs/api/08_showdoc.ipynb #abe98f5c
def _ext_link(url, txt, xtra=""): return f'[{txt}]({url}){{target="_blank" {xtra}}}'
class BasicMarkdownRenderer(MarkdownRenderer):
"Markdown renderer for `show_doc`"
def _repr_markdown_(self):
doc = '---\n\n'
src = NbdevLookup().code(self.fn)
if src: doc += _ext_link(src, 'source', 'style="float:right; font-size:smaller"') + '\n\n'
h = '#'*self.title_level
doc += f'{h} {self.nm}\n\n'
return doc+super()._repr_markdown_()
# %% ../nbs/api/08_showdoc.ipynb #1256ef79
def show_doc(sym, # Symbol to document
renderer=None, # Optional renderer (defaults to markdown)
name:str|None=None, # Optionally override displayed name of `sym`
title_level:int=3): # Heading level to use for symbol name
"Show signature and docstring for `sym`"
if renderer is None: renderer = get_config().get('renderer', None)
if renderer is None: renderer=BasicMarkdownRenderer
elif isinstance(renderer,str):
p,m = renderer.rsplit('.', 1)
renderer = getattr(import_module(p), m)
if isinstance_str(sym, "Function"): pass
elif isinstance_str(sym, "TypeDispatch"): pass # use _str as TypeDispatch will be removed from fastcore
else:return renderer(sym or show_doc, name=name, title_level=title_level)
# %% ../nbs/api/08_showdoc.ipynb #eb1f0530
def doc(elt):
"Show `show_doc` info along with link to docs"
return BasicMarkdownRenderer(elt)
# %% ../nbs/api/08_showdoc.ipynb #35043aa7-6b60-4ddf-8949-39a78577f23d
def showdoc_nm(tree):
"Get the fully qualified name for showdoc."
return ifnone(patch_name(tree), tree.name)
# %% ../nbs/api/08_showdoc.ipynb #e947414d
def colab_link(path):
"Get a link to the notebook at `path` on Colab"
from IPython.display import Markdown
cfg = get_config()
pre = 'https://colab.research.google.com/github/'
res = f'{pre}{cfg.user}/{cfg.repo}/blob/{cfg.branch}/{cfg.nbs_path.name}/{path}.ipynb'
display(Markdown(f'[Open `{path}` in Colab]({res})'))