Skip to content

Commit a0cd836

Browse files
committed
fixes #652
1 parent 0069f0a commit a0cd836

File tree

9 files changed

+50
-93
lines changed

9 files changed

+50
-93
lines changed

nbdev/doclinks.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from .imports import *
1111

1212
from fastcore.script import *
13-
from fastcore.imports import *
14-
from fastcore.basics import *
15-
from fastcore.imports import *
13+
from fastcore.utils import *
1614
from fastcore.meta import delegates
1715

1816
import ast,contextlib
@@ -58,7 +56,6 @@ def _binop_leafs(bo, o):
5856
else: right = [f'{unparse(bo.right).strip()}.{o.name}']
5957
return concat(left + right)
6058

61-
# %% ../nbs/04b_doclinks.ipynb 16
6259
def _all_or_exports(fn):
6360
code = Path(fn).read_text()
6461
trees = L(ast.parse(code).body)
@@ -69,10 +66,7 @@ def _get_patch(o):
6966
if not isinstance(o, (ast.FunctionDef,ast.AsyncFunctionDef)): return
7067
return first([d for d in o.decorator_list if decor_id(d).startswith('patch')])
7168

72-
def _id_or_attr(a):
73-
if hasattr(a, 'id'): return a.id
74-
return a.attr
75-
69+
# %% ../nbs/04b_doclinks.ipynb 16
7670
def get_patch_name(o):
7771
d = _get_patch(o)
7872
if not d: return
@@ -98,20 +92,20 @@ def update_syms(self:DocLinks):
9892
exp = exp.map(f"{self.mod_name}.{{}}")
9993
self.d['syms'][self.mod_name] = exp.map_dict(partial(self.doc_func, self.mod_name))
10094

101-
# %% ../nbs/04b_doclinks.ipynb 23
95+
# %% ../nbs/04b_doclinks.ipynb 24
10296
@patch
10397
def build_index(self:DocLinks):
10498
self.update_syms()
10599
self.d['settings'] = dict(**get_config().d)
106100
self.write_nbdev_idx()
107101

108-
# %% ../nbs/04b_doclinks.ipynb 25
102+
# %% ../nbs/04b_doclinks.ipynb 26
109103
def _doc_link(url, mod, sym=None):
110104
res = urljoin(url, remove_prefix(mod, get_config()['lib_name']+".")) + '.html'
111105
if sym: res += "#" + remove_prefix(sym, mod+".").lower()
112106
return res
113107

114-
# %% ../nbs/04b_doclinks.ipynb 26
108+
# %% ../nbs/04b_doclinks.ipynb 27
115109
def build_modidx():
116110
"Create _modidx.py"
117111
dest = config_key('lib_path')
@@ -125,7 +119,7 @@ def build_modidx():
125119
for file in dest.glob("**/*.py"):
126120
if file.name[0]!='_': DocLinks(file, doc_func, _fn).build_index()
127121

128-
# %% ../nbs/04b_doclinks.ipynb 27
122+
# %% ../nbs/04b_doclinks.ipynb 28
129123
@delegates(globtastic, but=['file_glob', 'skip_folder_re'])
130124
def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', recursive=True, key='nbs_path',
131125
as_path=False, **kwargs):
@@ -135,7 +129,7 @@ def nbglob(path=None, skip_folder_re = '^[_.]', file_glob='*.ipynb', recursive=T
135129
res = globtastic(path, file_glob=file_glob, skip_folder_re=skip_folder_re, **kwargs)
136130
return res.map(Path) if as_path else res
137131

138-
# %% ../nbs/04b_doclinks.ipynb 28
132+
# %% ../nbs/04b_doclinks.ipynb 29
139133
@call_parse
140134
def nbdev_export(
141135
path:str=None, # Path or filename
@@ -154,14 +148,14 @@ def nbdev_export(
154148
add_init(get_config().path('lib_path'))
155149
build_modidx()
156150

157-
# %% ../nbs/04b_doclinks.ipynb 30
151+
# %% ../nbs/04b_doclinks.ipynb 31
158152
def _settings_libs():
159153
try: # settings.ini doesn't exist yet until you call nbdev_new
160154
cfg = get_config()
161155
return cfg.get('strip_libs', cfg.get('lib_path')).split()
162156
except FileNotFoundError: return 'nbdev'
163157

164-
# %% ../nbs/04b_doclinks.ipynb 31
158+
# %% ../nbs/04b_doclinks.ipynb 32
165159
class NbdevLookup:
166160
"Mapping from symbol names to URLs with docs"
167161
def __init__(self, strip_libs=None, incl_libs=None, skip_mods=None):
@@ -184,7 +178,7 @@ def __init__(self, strip_libs=None, incl_libs=None, skip_mods=None):
184178

185179
def __getitem__(self, s): return self.syms.get(s, None)
186180

187-
# %% ../nbs/04b_doclinks.ipynb 39
181+
# %% ../nbs/04b_doclinks.ipynb 40
188182
@patch
189183
def _link_sym(self:NbdevLookup, m):
190184
l = m.group(1)

nbdev/export.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def _export_(self, nbp, cell, exp_to=None):
3030
def black_format(cell, # A cell node
3131
force=False): # Turn black formatting on regardless of settings.ini
3232
"Format code with `black`"
33-
cfg = get_config()
33+
try: cfg = get_config()
34+
except FileNotFoundError: return
3435
if (str(cfg.get('black_formatting')).lower() != 'true' and not force) or cell.cell_type != 'code': return
3536
try: import black
3637
except: raise ImportError("You must install black: `pip install black` if you wish to use black formatting with nbdev")
@@ -53,9 +54,9 @@ def create_modules(path, dest, procs=None, debug=False, mod_maker=ModuleMaker):
5354
"See https://nbdev.fast.ai/getting_started.html for more information.")
5455
return
5556
mm = mod_maker(dest=dest, name=name, nb_path=path, is_new=mod=='#')
56-
mm.make(cells, all_cells)
57+
mm.make(cells, all_cells, lib_path=dest)
5758

58-
# %% ../nbs/04a_export.ipynb 16
59+
# %% ../nbs/04a_export.ipynb 17
5960
def nb_export(nbname, lib_path=None):
6061
if lib_path is None: lib_path = get_config().path('lib_path')
6162
create_modules(nbname, lib_path, procs=[black_format])

nbdev/maker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ def _import2relative(cells, lib_name=None):
178178

179179
# %% ../nbs/02_maker.ipynb 32
180180
@patch
181-
def make(self:ModuleMaker, cells, all_cells=None, lib_name=None):
181+
def make(self:ModuleMaker, cells, all_cells=None, lib_path=None):
182182
"Write module containing `cells` with `__all__` generated from `all_cells`"
183183
if all_cells is None: all_cells = cells
184184
if self.parse:
185-
libnm = get_config().path('lib_path')
186-
mod_dir = os.path.relpath(self.fname.parent, libnm.parent)
185+
if not lib_path: lib_path = get_config().path('lib_path')
186+
mod_dir = os.path.relpath(self.fname.parent, Path(lib_path).parent)
187187
_import2relative(all_cells, mod_dir)
188188
if not self.is_new: return self._make_exists(cells, all_cells)
189189

nbdev/read.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def nbdev_create_config(
4848
doc_path:str='_docs', # Folder name containing docs
4949
tst_flags:str='', # Test flags
5050
version:str='0.0.1', # Version number
51+
doc_host:str='example.org', # Hostname for docs
52+
doc_base_url:str='/', # Base URL for docs
5153
keywords='python', # Keywords for PyPI
5254
license='apache2', # License for PyPI
5355
copyright='', # Copyright for PyPI, defaults to author from current year

nbs/01_read.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
" doc_path:str='_docs', # Folder name containing docs\n",
115115
" tst_flags:str='', # Test flags\n",
116116
" version:str='0.0.1', # Version number\n",
117+
" doc_host:str='example.org', # Hostname for docs\n",
118+
" doc_base_url:str='/', # Base URL for docs\n",
117119
" keywords='python', # Keywords for PyPI\n",
118120
" license='apache2', # License for PyPI\n",
119121
" copyright='', # Copyright for PyPI, defaults to author from current year\n",

nbs/02_maker.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@
464464
"source": [
465465
"#|export\n",
466466
"@patch\n",
467-
"def make(self:ModuleMaker, cells, all_cells=None, lib_name=None):\n",
467+
"def make(self:ModuleMaker, cells, all_cells=None, lib_path=None):\n",
468468
" \"Write module containing `cells` with `__all__` generated from `all_cells`\"\n",
469469
" if all_cells is None: all_cells = cells\n",
470470
" if self.parse: \n",
471-
" libnm = get_config().path('lib_path')\n",
472-
" mod_dir = os.path.relpath(self.fname.parent, libnm.parent)\n",
471+
" if not lib_path: lib_path = get_config().path('lib_path')\n",
472+
" mod_dir = os.path.relpath(self.fname.parent, Path(lib_path).parent)\n",
473473
" _import2relative(all_cells, mod_dir)\n",
474474
" if not self.is_new: return self._make_exists(cells, all_cells)\n",
475475
"\n",

nbs/04a_export.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"def black_format(cell, # A cell node \n",
105105
" force=False): # Turn black formatting on regardless of settings.ini\n",
106106
" \"Format code with `black`\"\n",
107-
" cfg = get_config()\n",
107+
" try: cfg = get_config()\n",
108+
" except FileNotFoundError: return\n",
108109
" if (str(cfg.get('black_formatting')).lower() != 'true' and not force) or cell.cell_type != 'code': return\n",
109110
" try: import black\n",
110111
" except: raise ImportError(\"You must install black: `pip install black` if you wish to use black formatting with nbdev\")\n",
@@ -145,7 +146,7 @@
145146
" \"See https://nbdev.fast.ai/getting_started.html for more information.\")\n",
146147
" return\n",
147148
" mm = mod_maker(dest=dest, name=name, nb_path=path, is_new=mod=='#')\n",
148-
" mm.make(cells, all_cells)"
149+
" mm.make(cells, all_cells, lib_path=dest)"
149150
]
150151
},
151152
{

nbs/04b_doclinks.ipynb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
"from nbdev.imports import *\n",
3131
"\n",
3232
"from fastcore.script import *\n",
33-
"from fastcore.imports import *\n",
34-
"from fastcore.basics import *\n",
35-
"from fastcore.imports import *\n",
33+
"from fastcore.utils import *\n",
3634
"from fastcore.meta import delegates\n",
3735
"\n",
3836
"import ast,contextlib\n",
@@ -209,7 +207,17 @@
209207
" else: left = [f'{unparse(bo.left).strip()}.{o.name}']\n",
210208
" if isinstance(bo.right, ast.BinOp): right = _binop_leafs(bo.right, o)\n",
211209
" else: right = [f'{unparse(bo.right).strip()}.{o.name}']\n",
212-
" return concat(left + right)"
210+
" return concat(left + right)\n",
211+
"\n",
212+
"def _all_or_exports(fn):\n",
213+
" code = Path(fn).read_text()\n",
214+
" trees = L(ast.parse(code).body)\n",
215+
" res = read_var(code, '__all__')\n",
216+
" return L(retr_exports(trees) if res is None else res),trees\n",
217+
"\n",
218+
"def _get_patch(o):\n",
219+
" if not isinstance(o, (ast.FunctionDef,ast.AsyncFunctionDef)): return\n",
220+
" return first([d for d in o.decorator_list if decor_id(d).startswith('patch')])"
213221
]
214222
},
215223
{
@@ -219,20 +227,6 @@
219227
"outputs": [],
220228
"source": [
221229
"#|export\n",
222-
"def _all_or_exports(fn):\n",
223-
" code = Path(fn).read_text()\n",
224-
" trees = L(ast.parse(code).body)\n",
225-
" res = read_var(code, '__all__')\n",
226-
" return L(retr_exports(trees) if res is None else res),trees\n",
227-
"\n",
228-
"def _get_patch(o):\n",
229-
" if not isinstance(o, (ast.FunctionDef,ast.AsyncFunctionDef)): return\n",
230-
" return first([d for d in o.decorator_list if decor_id(d).startswith('patch')])\n",
231-
"\n",
232-
"def _id_or_attr(a):\n",
233-
" if hasattr(a, 'id'): return a.id\n",
234-
" return a.attr\n",
235-
"\n",
236230
"def get_patch_name(o):\n",
237231
" d = _get_patch(o)\n",
238232
" if not d: return\n",
@@ -303,6 +297,13 @@
303297
" self.d['syms'][self.mod_name] = exp.map_dict(partial(self.doc_func, self.mod_name))"
304298
]
305299
},
300+
{
301+
"cell_type": "markdown",
302+
"metadata": {},
303+
"source": [
304+
"We have a test notebook we can export to create a test python module:"
305+
]
306+
},
306307
{
307308
"cell_type": "code",
308309
"execution_count": null,

nbs/08_showdoc.ipynb

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
"| **Returns** | **int** | | |"
228228
],
229229
"text/plain": [
230-
"<__main__.DocmentTbl at 0x7fc8d5d35e20>"
230+
"<__main__.DocmentTbl at 0x120766a90>"
231231
]
232232
},
233233
"execution_count": null,
@@ -289,7 +289,7 @@
289289
"| c | param c |"
290290
],
291291
"text/plain": [
292-
"<__main__.DocmentTbl at 0x7fc8d5d355b0>"
292+
"<__main__.DocmentTbl at 0x120766850>"
293293
]
294294
},
295295
"execution_count": null,
@@ -371,7 +371,7 @@
371371
"| c | str | None | |"
372372
],
373373
"text/plain": [
374-
"<__main__.DocmentTbl at 0x7fc8d5d35820>"
374+
"<__main__.DocmentTbl at 0x1207303d0>"
375375
]
376376
},
377377
"execution_count": null,
@@ -406,7 +406,7 @@
406406
"| d | bool | True | description of param d |"
407407
],
408408
"text/plain": [
409-
"<__main__.DocmentTbl at 0x7fc8d5d35070>"
409+
"<__main__.DocmentTbl at 0x120730f10>"
410410
]
411411
},
412412
"execution_count": null,
@@ -558,50 +558,6 @@
558558
"You can use `show_doc` to document apis of functions, classes or methods:"
559559
]
560560
},
561-
{
562-
"cell_type": "code",
563-
"execution_count": null,
564-
"id": "53a1bc00",
565-
"metadata": {},
566-
"outputs": [
567-
{
568-
"data": {
569-
"text/markdown": [
570-
"---\n",
571-
"\n",
572-
"#### f\n",
573-
"\n",
574-
"> f (x:int=1)\n",
575-
"\n",
576-
"func docstring"
577-
],
578-
"text/plain": [
579-
"<__main__.BasicMarkdownRenderer at 0x7fc8d5c50220>"
580-
]
581-
},
582-
"execution_count": null,
583-
"metadata": {},
584-
"output_type": "execute_result"
585-
}
586-
],
587-
"source": [
588-
"def f(x:int=1):\n",
589-
" \"func docstring\"\n",
590-
" ...\n",
591-
"\n",
592-
"show_doc(f)"
593-
]
594-
},
595-
{
596-
"cell_type": "markdown",
597-
"id": "66a8dfcd-dc2b-41ce-9d35-5c35e040f25d",
598-
"metadata": {},
599-
"source": [
600-
":::{.callout-warning}\n",
601-
"If you are using a version of python that is older than 3.10, type hints might be rendered as strings when running `show_doc`. We recommend upgrading to python 3.10 locally if possible so you can preview docs without this artifact. We have set the version of python to be 3.10 `.github/workflows/deploy.yaml` for this reason as well.\n",
602-
":::"
603-
]
604-
},
605561
{
606562
"cell_type": "code",
607563
"execution_count": null,

0 commit comments

Comments
 (0)