Skip to content

Commit

Permalink
fixes #269
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 25, 2020
1 parent 4916fdf commit 11cade3
Show file tree
Hide file tree
Showing 23 changed files with 897 additions and 2,230 deletions.
3 changes: 0 additions & 3 deletions docs/_data/sidebars/home_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ entries:
- output: web,pdf
title: Magic Flags
url: magic_flags.html
- output: web,pdf
title: Callbacks
url: nbdev_callbacks.html
output: web
title: nbdev
output: web
Expand Down
1 change: 0 additions & 1 deletion docs/cli.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<li><a href="/cli.html#nbdev_test_nbs"><code>nbdev_test_nbs</code></a> runs tests in notebooks</li>
<li><a href="/cli.html#nbdev_trust_nbs"><code>nbdev_trust_nbs</code></a> trusts all notebooks (so that the HTML content is shown)</li>
<li><a href="/cli.html#nbdev_update_lib"><code>nbdev_update_lib</code></a> propagates any change in the library back to the notebooks</li>
<li><code>nbdev_upgrade</code> updates an existing nbdev project to use the latest features</li>
</ul>

</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ <h4 id="compose" class="doc_header"><code>compose</code><a href="https://github.


<div class="output_markdown rendered_html output_subarea ">
<h4 id="parallel" class="doc_header"><code>parallel</code><a href="https://github.com/fastai/fastcore/tree/master/fastcore/utils.py#L702" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>parallel</code>(<strong><code>f</code></strong>, <strong><code>items</code></strong>, <strong>*<code>args</code></strong>, <strong><code>n_workers</code></strong>=<em><code>64</code></em>, <strong><code>total</code></strong>=<em><code>None</code></em>, <strong><code>progress</code></strong>=<em><code>None</code></em>, <strong><code>pause</code></strong>=<em><code>0</code></em>, <strong><code>timeout</code></strong>=<em><code>None</code></em>, <strong><code>chunksize</code></strong>=<em><code>1</code></em>, <strong>**<code>kwargs</code></strong>)</p>
<h4 id="parallel" class="doc_header"><code>parallel</code><a href="https://github.com/fastai/fastcore/tree/master/fastcore/utils.py#L701" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>parallel</code>(<strong><code>f</code></strong>, <strong><code>items</code></strong>, <strong>*<code>args</code></strong>, <strong><code>n_workers</code></strong>=<em><code>64</code></em>, <strong><code>total</code></strong>=<em><code>None</code></em>, <strong><code>progress</code></strong>=<em><code>None</code></em>, <strong><code>pause</code></strong>=<em><code>0</code></em>, <strong><code>timeout</code></strong>=<em><code>None</code></em>, <strong><code>chunksize</code></strong>=<em><code>1</code></em>, <strong>**<code>kwargs</code></strong>)</p>
</blockquote>
<p>Applies <code>func</code> in parallel to <code>items</code>, using <code>n_workers</code></p>

Expand Down
10 changes: 5 additions & 5 deletions docs/export2html.html
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ <h4 id="convert_nb" class="doc_header"><code>convert_nb</code><a href="https://g


<div class="output_markdown rendered_html output_subarea ">
<h4 id="notebook2html" class="doc_header"><code>notebook2html</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L565" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>notebook2html</code>(<strong><code>fname</code></strong>=<em><code>None</code></em>, <strong><code>force_all</code></strong>=<em><code>False</code></em>, <strong><code>n_workers</code></strong>=<em><code>None</code></em>, <strong><code>template_file</code></strong>=<em><code>None</code></em>, <strong><code>exporter</code></strong>=<em><code>None</code></em>, <strong><code>dest</code></strong>=<em><code>None</code></em>, <strong><code>pause</code></strong>=<em><code>0</code></em>, <strong><code>execute</code></strong>=<em><code>True</code></em>)</p>
<h4 id="notebook2html" class="doc_header"><code>notebook2html</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L562" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>notebook2html</code>(<strong><code>fname</code></strong>=<em><code>None</code></em>, <strong><code>force_all</code></strong>=<em><code>False</code></em>, <strong><code>n_workers</code></strong>=<em><code>None</code></em>, <strong><code>template_file</code></strong>=<em><code>None</code></em>, <strong><code>exporter</code></strong>=<em><code>None</code></em>, <strong><code>dest</code></strong>=<em><code>None</code></em>, <strong><code>pause</code></strong>=<em><code>0</code></em>, <strong><code>execute</code></strong>=<em><code>True</code></em>)</p>
</blockquote>
<p>Convert all notebooks matching <code>fname</code> to html files</p>

Expand Down Expand Up @@ -1885,7 +1885,7 @@ <h4 id="notebook2html" class="doc_header"><code>notebook2html</code><a href="htt


<div class="output_markdown rendered_html output_subarea ">
<h4 id="convert_md" class="doc_header"><code>convert_md</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L593" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>convert_md</code>(<strong><code>fname</code></strong>, <strong><code>dest_path</code></strong>, <strong><code>img_path</code></strong>=<em><code>'docs/images/'</code></em>, <strong><code>jekyll</code></strong>=<em><code>True</code></em>)</p>
<h4 id="convert_md" class="doc_header"><code>convert_md</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L590" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>convert_md</code>(<strong><code>fname</code></strong>, <strong><code>dest_path</code></strong>, <strong><code>img_path</code></strong>=<em><code>'docs/images/'</code></em>, <strong><code>jekyll</code></strong>=<em><code>True</code></em>)</p>
</blockquote>
<p>Convert a notebook <code>fname</code> to a markdown file in <code>dest_path</code>.</p>

Expand Down Expand Up @@ -1973,7 +1973,7 @@ <h4 id="convert_md" class="doc_header"><code>convert_md</code><a href="https://g


<div class="output_markdown rendered_html output_subarea ">
<h4 id="nb_detach_cells" class="doc_header"><code>nb_detach_cells</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L654" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>nb_detach_cells</code>(<strong><code>path_nb</code></strong>, <strong><code>dest</code></strong>=<em><code>None</code></em>, <strong><code>replace</code></strong>=<em><code>True</code></em>, <strong><code>use_img</code></strong>=<em><code>False</code></em>)</p>
<h4 id="nb_detach_cells" class="doc_header"><code>nb_detach_cells</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L648" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>nb_detach_cells</code>(<strong><code>path_nb</code></strong>, <strong><code>dest</code></strong>=<em><code>None</code></em>, <strong><code>replace</code></strong>=<em><code>True</code></em>, <strong><code>use_img</code></strong>=<em><code>False</code></em>)</p>
</blockquote>
<p>Export cell attachments to <code>dest</code> and update references</p>

Expand Down Expand Up @@ -2053,7 +2053,7 @@ <h2 id="Sidebar">Sidebar<a class="anchor-link" href="#Sidebar"> </a></h2>


<div class="output_markdown rendered_html output_subarea ">
<h4 id="create_default_sidebar" class="doc_header"><code>create_default_sidebar</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L708" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>create_default_sidebar</code>()</p>
<h4 id="create_default_sidebar" class="doc_header"><code>create_default_sidebar</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L702" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>create_default_sidebar</code>()</p>
</blockquote>
<p>Create the default sidebar for the docs website</p>

Expand Down Expand Up @@ -2092,7 +2092,7 @@ <h4 id="create_default_sidebar" class="doc_header"><code>create_default_sidebar<


<div class="output_markdown rendered_html output_subarea ">
<h4 id="make_sidebar" class="doc_header"><code>make_sidebar</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L714" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>make_sidebar</code>()</p>
<h4 id="make_sidebar" class="doc_header"><code>make_sidebar</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/export2html.py#L708" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>make_sidebar</code>()</p>
</blockquote>
<p>Making sidebar for the doc website form the content of <code>doc_folder/sidebar.json</code></p>

Expand Down
Binary file modified docs/images/export_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion nbdev/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.19"
__version__ = "1.1.0"

from fastcore.imports import IN_IPYTHON
from .imports import *
Expand Down
5 changes: 1 addition & 4 deletions nbdev/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

__all__ = ["index", "modules", "custom_doc_links", "git_url"]

index = {"first": "00_export.ipynb",
"read_nb": "00_export.ipynb",
index = {"read_nb": "00_export.ipynb",
"check_re": "00_export.ipynb",
"check_re_multi": "00_export.ipynb",
"is_export": "00_export.ipynb",
Expand Down Expand Up @@ -44,8 +43,6 @@
"type_repr": "02_showdoc.ipynb",
"format_param": "02_showdoc.ipynb",
"show_doc": "02_showdoc.ipynb",
"parse_nbdev_show_doc": "02_showdoc.ipynb",
"nbdev_show_doc": "02_showdoc.ipynb",
"md2html": "02_showdoc.ipynb",
"get_doc_link": "02_showdoc.ipynb",
"doc": "02_showdoc.ipynb",
Expand Down
90 changes: 31 additions & 59 deletions nbdev/export.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/00_export.ipynb (unless otherwise specified).

__all__ = ['first', 'read_nb', 'check_re', 'check_re_multi', 'is_export', 'find_default_export', 'ReTstFlags',
'export_names', 'extra_add', 'relative_import', 'reset_nbdev_module', 'get_nbdev_module',
'save_nbdev_module', 'split_flags_and_code', 'create_mod_file', 'create_mod_files', 'add_init',
'update_version', 'update_baseurl', 'notebook2script', 'DocsTestClass']
__all__ = ['read_nb', 'check_re', 'check_re_multi', 'is_export', 'find_default_export', 'ReTstFlags', 'export_names',
'extra_add', 'relative_import', 'reset_nbdev_module', 'get_nbdev_module', 'save_nbdev_module',
'split_flags_and_code', 'create_mod_file', 'create_mod_files', 'add_init', 'update_version',
'update_baseurl', 'notebook2script', 'DocsTestClass']

# Cell
from .imports import *
from fastcore.script import *
from fastcore.foundation import *
from keyword import iskeyword
from .flags import parse_line

# Cell
def first(x):
"First element of `x`, or None if missing"
try: return next(iter(x))
except StopIteration: return None

# Cell
def read_nb(fname):
"Read the notebook in `fname`."
Expand All @@ -32,14 +27,13 @@ def check_re(cell, pat, code_only=True):
# Cell
def check_re_multi(cell, pats, code_only=True):
"Check if `cell` contains a line matching any regex in `pats`, returning the first match found"
for pat in pats:
tst = check_re(cell, pat, code_only)
if tst: return tst
return L(pats).map_first(partial(check_re, cell, code_only=code_only))

# Cell
def _mk_flag_re(magic_flag, body, n_params, comment):
def _mk_flag_re(body, n_params, comment):
"Compiles a regex for finding nbdev flags"
prefix = r"%nbdev_" if magic_flag else r"\s*\#\s*"
assert body!=True, 'magics no longer supported'
prefix = r"\s*\#\s*"
param_group = ""
if n_params == -1: param_group = r"[ \t]+(.+)"
if n_params == 1: param_group = r"[ \t]+(\S+)"
Expand All @@ -52,87 +46,69 @@ def _mk_flag_re(magic_flag, body, n_params, comment):
{param_group}
[ \t]* # any number of spaces and/or tabs
$ # end of line (since re.MULTILINE is passed)
""", re.MULTILINE | re.VERBOSE if magic_flag else re.IGNORECASE | re.MULTILINE | re.VERBOSE)
""", re.MULTILINE | re.VERBOSE)

# Cell
_re_blank_export = _mk_flag_re(False, "export[si]?", 0,
_re_blank_export = _mk_flag_re("export[si]?", 0,
"Matches any line with #export, #exports or #exporti without any module name")
_re_blank_export_magic = _mk_flag_re(True, "export(?:|_and_show|_internal)", 0,
"Matches any line with any kind of export magic without any module name")
# any kind of export magic = %nbdev_export, %nbdev_export_and_show or %nbdev_export_internal

# Cell
_re_mod_export = _mk_flag_re(False, "export[si]?", 1,
_re_mod_export = _mk_flag_re("export[si]?", 1,
"Matches any line with #export, #exports or #exporti with a module name and catches it in group 1")
_re_mod_export_magic = _mk_flag_re(True, "export(?:|_and_show|_internal)", 1,
"Matches any line with any kind of export magic with a module name and catches it in group 1")

# Cell
_re_internal_export = _mk_flag_re(False, "exporti", (0,1),
_re_internal_export = _mk_flag_re("exporti", (0,1),
"Matches any line with #exporti with or without a module name")
_re_internal_export_magic = _mk_flag_re(True, "export_internal", (0,1),
"Matches any line with export internal magic with or without a module name")

# Internal Cell
def _is_external_export(tst):
"Check if a cell is an external or internal export. `tst` is an re match"
if _re_internal_export.search(tst.string) is not None: return False
if _re_internal_export_magic.search(tst.string) is not None: return False
return True
return _re_internal_export.search(tst.string) is None

# Cell
def is_export(cell, default):
"Check if `cell` is to be exported and returns the name of the module to export it if provided"
tst = check_re_multi(cell, [_re_blank_export, _re_blank_export_magic])
tst = check_re(cell, _re_blank_export)
if tst:
if default is None:
print(f"This cell doesn't have an export destination and was ignored:\n{cell['source'][1]}")
print(f"No export destination, ignored:\n{cell['source']}")
return default, _is_external_export(tst)
tst = check_re_multi(cell, [_re_mod_export, _re_mod_export_magic])
tst = check_re(cell, _re_mod_export)
if tst: return os.path.sep.join(tst.groups()[0].split('.')), _is_external_export(tst)
else: return None

# Cell
_re_default_exp = _mk_flag_re(False, 'default_exp', 1,
"Matches any line with #default_exp with a module name and catches it in group 1")
_re_default_exp_magic = _mk_flag_re(True, 'default_export', 1,
"Matches any line with %nbdev_default_export with a module name and catches it in group 1")
_re_default_exp = _mk_flag_re('default_exp', 1, "Matches any line with #default_exp with a module name")

# Cell
def find_default_export(cells):
"Find in `cells` the default export module."
for cell in cells:
tst = check_re_multi(cell, [_re_default_exp, _re_default_exp_magic])
if tst: return tst.groups()[0]
res = L(cells).map_first(check_re, pat=_re_default_exp)
return res.groups()[0] if res else None

# Cell
class ReTstFlags():
"Provides test flag matching regular expressions"
"Provides test flag matching regular expressions (for internal nbdev use)"
def __init__(self, all_flag):
"`all_flag` tells us to match flags applied to all cells (`True`) or individual cells (`False`)"
self.all_flag = all_flag

def _deferred_init(self):
"Compile at first use but not before since patterns need `Config().tst_flags`"
if not hasattr(self, '_re'):
tst_flags = Config().get('tst_flags', '')
_re_all, _re_magic_all = ('all_', '[ \t]+all') if self.all_flag else ('', '')
self._re = _mk_flag_re(False, f"{_re_all}({tst_flags})", 0,
"Matches any line with a test flag and catches it in a group")
self._re_magic = _mk_flag_re(True, f"({tst_flags})_test{_re_magic_all}", 0,
"Matches any line with a magic test flag and catches it in a group")
if hasattr(self, '_re'): return
tst_flags = Config().get('tst_flags', '')
_re_all = 'all_' if self.all_flag else ''
self._re = _mk_flag_re(f"{_re_all}({tst_flags})", 0, "Any line with a test flag")

def findall(self, source):
"Return all test flags found in `source`"
self._deferred_init()
return self._re.findall(source) + self._re_magic.findall(source)
return self._re.findall(source)

def search(self, source):
"Return a match object for the first test flag found in `source`"
self._deferred_init()
for pat in [self._re, self._re_magic]:
tst = pat.search(source)
if tst: return tst
return self._re.search(source)

# Cell
_re_patch_func = re.compile(r"""
Expand Down Expand Up @@ -218,17 +194,13 @@ def _f(gps):
#Same with __all__
_re__all__def = re.compile(r'^__all__\s*=\s*\[([^\]]*)\]', re.MULTILINE)

_re_all_def_magic = _mk_flag_re(True, 'add2all', -1,
"# Catches a cell with %nbdev_add2all \*\* and get that \*\* in group 1")

# Cell
def extra_add(flags, code):
"Catch adds to `__all__` required by a cell with `_all_=` or `%nbdev_add2all`"
m = check_re_multi({'source': code}, [_re_all_def, _re_all_def_magic], False)
"Catch adds to `__all__` required by a cell with `_all_=`"
m = check_re({'source': code}, _re_all_def, False)
if m:
code = m.re.sub('#nbdev_' + 'comment \g<0>', code)
code = re.sub(r'([^\n]|^)\n*$', r'\1', code)
if not m: m = check_re({'source': flags}, _re_all_def_magic, False)
if not m: return [], code
def clean_quotes(s):
"Return `s` enclosed in single quotes, removing double quotes if needed"
Expand Down Expand Up @@ -345,8 +317,7 @@ def split_flags_and_code(cell, return_type=list):
code_lines = cell['source'].split('\n')
split_pos = 0 if code_lines[0].strip().startswith('#') else -1
for i, line in enumerate(code_lines):
if line.startswith('%nbdev_'): split_pos=i
elif not line.startswith('#') and line.strip() and not _re_from_future_import.match(line): break
if not line.startswith('#') and line.strip() and not _re_from_future_import.match(line): break
split_pos+=1
res = code_lines[:split_pos], code_lines[split_pos:]
if return_type is list: return res
Expand Down Expand Up @@ -473,5 +444,6 @@ class DocsTestClass:
def test(): pass

# Internal Cell
#exporti
#for tests only
def update_lib_with_exporti_testfn(): pass
Loading

2 comments on commit 11cade3

@pete88b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Morning @jph00 looks like you beat me to it (o: think you also want to delete

  • nbdev/flags.py
  • docs/magic_flags.html

and remove magic_flags.html from

  • docs/sidebar.json

@jph00
Copy link
Member Author

@jph00 jph00 commented on 11cade3 Sep 26, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.