Skip to content

Commit bb1c969

Browse files
committed
migration
1 parent 4537ae5 commit bb1c969

File tree

4 files changed

+182
-34
lines changed

4 files changed

+182
-34
lines changed

nbdev/migrate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _re_v1():
7979
return re.compile(f"^[ \f\v\t]*?(#)\s*({_tmp})", re.MULTILINE)
8080

8181
def _repl_directives(code_str):
82-
def _fmt(x): return f"#|{x.group(2).replace('-', '_')}"
82+
def _fmt(x): return f"#| {x.group(2).replace('-', '_')}"
8383
return _re_v1().sub(_fmt, code_str)
8484

8585
# %% ../nbs/15_migrate.ipynb 28

nbs/15_migrate.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@
421421
" return re.compile(f\"^[ \\f\\v\\t]*?(#)\\s*({_tmp})\", re.MULTILINE)\n",
422422
"\n",
423423
"def _repl_directives(code_str): \n",
424-
" def _fmt(x): return f\"#|{x.group(2).replace('-', '_')}\"\n",
424+
" def _fmt(x): return f\"#| {x.group(2).replace('-', '_')}\"\n",
425425
" return _re_v1().sub(_fmt, code_str)"
426426
]
427427
},
@@ -445,8 +445,8 @@
445445
"foo\n",
446446
"\"\"\"\n",
447447
"test_eq(_repl_directives(_test_dir),\n",
448-
" \"\"\"\\n#|default_exp\\n#|export\\n#|collapse_show\\n#|collapse_hide\\n#|collapse_output\\n#|collapse_output\n",
449-
"not_dir='#export'\\n#|hide_input\\nfoo\\n\"\"\")"
448+
" \"\"\"\\n#| default_exp\\n#| export\\n#| collapse_show\\n#| collapse_hide\\n#| collapse_output\\n#| collapse_output\n",
449+
"not_dir='#export'\\n#| hide_input\\nfoo\\n\"\"\")"
450450
]
451451
},
452452
{
@@ -496,7 +496,8 @@
496496
" 'data': {'application/vnd.google.colaboratory.intrinsic+json': {'type': 'string'},\n",
497497
" 'plain/text': ['sample output']},\n",
498498
" 'output': 'super'}],\n",
499-
" 'source': ['\\n', '#|default_exp\\n', '#|export\\n', '#|collapse_show\\n', '#|collapse_hide\\n', '#|collapse_output\\n', '#|collapse_output\\n', \"not_dir='#export'\\n\", '# hide_input\\n', 'foo\\n']\n",
499+
" 'source': ['\\n', '#| default_exp\\n', '#| export\\n', '#| collapse_show\\n', '#| collapse_hide\\n', '#| collapse_output\\n',\n",
500+
" '#| collapse_output\\n', \"not_dir='#export'\\n\", '# hide_input\\n', 'foo\\n']\n",
500501
" })"
501502
]
502503
},

nbs/getting_started.ipynb

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"cell_type": "markdown",
1414
"metadata": {},
1515
"source": [
16-
"**NB: This is nbdev v2, a new from-scratch rewrite of nbdev that's not backwards compatible. Whilst the differences to nbdev1 aren't huge, it does require some changes. So if you wish to continue to use nbdev1 until you've got the time to upgrade, you can find the old version docs at [nbdev1.fast.ai](https://nbdev1.fast.ai). You can use version-pinning in your `requirements.txt` or `settings.ini` (i.e use `'nbdev<2'` as your dependency) to stop nbdev from being upgraded.**\n",
16+
"**NB: This is nbdev v2, a new from-scratch rewrite of nbdev that's not backwards compatible. Whilst the differences to nbdev1 aren't huge, it does require some changes. So if you wish to continue to use nbdev1 until you've got the time to upgrade, you can find the old version docs at [nbdev1.fast.ai](https://nbdev1.fast.ai). You can use version-pinning in your `requirements.txt` or `settings.ini` (i.e use `'nbdev<2'` as your dependency) to stop nbdev from being upgraded. To upgrade to to nbdev2, follow the steps in the [migration tutorial](/migrating.html).**\n",
1717
"\n",
1818
"---\n",
1919
"\n",
@@ -132,13 +132,6 @@
132132
"!nbdev_help"
133133
]
134134
},
135-
{
136-
"cell_type": "markdown",
137-
"metadata": {},
138-
"source": [
139-
"## FAQ"
140-
]
141-
},
142135
{
143136
"cell_type": "markdown",
144137
"metadata": {},
@@ -153,27 +146,6 @@
153146
"[Watch this video](https://youtu.be/9Q6sLbz37gk). Don't worry, we still get this too, despite having used `nbdev` for a wide range of \"very serious\" software projects over the last three years, including [deep learning libraries](https://github.com/fastai/fastai), [API clients](https://github.com/fastai/ghapi), [Python language extensions](https://github.com/fastai/fastcore), [terminal user interfaces](https://github.com/nat/ghtop), and more!"
154147
]
155148
},
156-
{
157-
"cell_type": "markdown",
158-
"metadata": {},
159-
"source": [
160-
"## nbdev in the wild"
161-
]
162-
},
163-
{
164-
"cell_type": "markdown",
165-
"metadata": {},
166-
"source": [
167-
"### fastai ecosystem"
168-
]
169-
},
170-
{
171-
"cell_type": "markdown",
172-
"metadata": {},
173-
"source": [
174-
"`nbdev` has been used to build innovative software in the fastai ecosystem, including the [`fastai`](https://docs.fast.ai/) deep learning library which implements a [unique layered API and callback system](https://arxiv.org/abs/2002.04688), and [`fastcore`](https://fastcore.fast.ai/), which supercharges Python leveraging its dynamic nature. Furthermore, `nbdev` allows a very small number of developers to maintain and grow a [large ecosystem](https://github.com/fastai) of software engineering, data science, machine learning, and devops tools."
175-
]
176-
},
177149
{
178150
"cell_type": "markdown",
179151
"metadata": {},

nbs/migrating.ipynb

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# nbdev1 migration\n",
8+
"\n",
9+
"> How to change your nbdev1 repo to work with nbdev2"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"nbdev v2 is a new from-scratch rewrite of nbdev that's not backwards compatible. This page describes the changes you need to make to upgrade your nbdev v1 repo to work with the new version. The steps shown here should work on macOS or Linux (including Windows WSL)\n",
17+
"\n",
18+
"The biggest change is that nbdev2 uses [Quarto](https://quarto.org/) to generate your website, whereas nbdev1 used nbconvert and jekyll. You can use all of Quarto's features directly in nbdev, so checkout the Quarto website to see all the amazing functionality it supports."
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"## Initial setup"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"If you've pinned nbdev in `requirements.txt` or `settings.ini` (e.g `nbdev<2`) remove the version pin. (*If you don't know what this means, then you don't have it, so you can ignore this step*).\n",
33+
"\n",
34+
"Install the latest version of nbdev by typing:\n",
35+
"\n",
36+
" pip install -U nbdev\n",
37+
"\n",
38+
"or:\n",
39+
"\n",
40+
" conda install -c fastai nbdev\n",
41+
"\n",
42+
"You may need to restart your terminal for the new commands to be visible to your shell."
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Upgrade directives"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"nbdev has slightly changed how \"directive comments\" like `export` and `default_exp` work, in order to align with how Quarto does things. Now, instead of just adding a `#` to the start to indicate a directive (e.g `#export`), you now need to use `#|` (e.g `#|export`). You can also optionally add a space (e.g `#| export`).\n",
57+
"\n",
58+
"To automatically upgrade your directives to the new format, run in the root of your repo:\n",
59+
"\n",
60+
" nbdev_migrate_directives\n",
61+
"\n",
62+
"You should now test that you can export your module by running:\n",
63+
"\n",
64+
" nbdev_export\n",
65+
"\n",
66+
"Note that `nbdev_export` replaces `nbdev_build_lib`. Run `nbdev_export -h` to see the options you can pass to it (normally you won't need to pass any). To see a list of all the commands available in nbdev2, run `nbdev_export`."
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"## Add and remove files"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"First set a variable with the name of your library, by running the following (replacing \"yourlib\" with the name of your library's subdirectory)\n",
81+
"\n",
82+
" export LIBNAME=yourlib\n",
83+
"\n",
84+
"Now run the following:\n",
85+
"\n",
86+
"```bash\n",
87+
"git rm Makefile\n",
88+
"git add $LIBNAME/_modidx.py\n",
89+
"rm -rf docs\n",
90+
"rm -f .gitconfig \n",
91+
"rm -f .git/hooks/post-merge\n",
92+
"\n",
93+
"rm -f setup.py\n",
94+
"curl -O https://github.com/fastai/nbdev-template/blob/master/styles.css\n",
95+
"curl -O https://github.com/fastai/nbdev-template/blob/master/setup.py\n",
96+
"\n",
97+
"cat >>.gitignore <<EOF\n",
98+
"_docs/\n",
99+
"sidebar.yml\n",
100+
"EOF\n",
101+
"```\n",
102+
"\n",
103+
"As you see above, we've remove the Makefile -- that's because all the things done by `make` before are now handled by `nbdev` commands directly."
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"## Update directive names"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"A number of directives have changed names. We'll use `perl` to fix them. Run these lines in the root of your repo:\n",
118+
"\n",
119+
"```bash\n",
120+
"[[ $SHELL = *bash ]] && shopt -s globstar\n",
121+
"\n",
122+
"perl -pi -e 's/#\\|\\s*hide_input/#| echo: false/' **/*.ipynb\n",
123+
"perl -pi -e 's/#\\|\\s*hide_output/#| output: false/' **/*.ipynb\n",
124+
"perl -pi -e 's/#\\|\\s*skip/#| eval: false/' **/*.ipynb\n",
125+
"perl -pi -e 's/from nbdev.export import notebook2script/from nbdev import nbdev_export/' **/*.ipynb\n",
126+
"perl -pi -e 's/notebook2script/nbdev_export/' **/*.ipynb\n",
127+
"perl -pi -e 's/#\\|?\\s*all_slow/#| nbflags skip_exec/' *.ipynb\n",
128+
"```\n",
129+
"\n",
130+
"These change the following directives to use functionality built into Quarto:\n",
131+
"\n",
132+
"- `hide_input` --> `echo: false`\n",
133+
"- `hide_output` --> `output: false`\n",
134+
"- `skip` --> `eval: false`\n",
135+
"\n",
136+
"They also update the new location and name of the `nbdev_export` python function, and use the new `nbflags` functionality if you have any notebooks that you've asked nbdev1 to skip (using `all_slow`)."
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"## Final steps"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"You should now edit `settings.ini`, and change `doc_path` from `docs` to `_docs`, since that's where nbdev2 will build your website.\n",
151+
"\n",
152+
"If you use a custom domain for your website, you should move your `CNAME` file into the directory containing your notebooks.\n",
153+
"\n",
154+
"Before pushing to GitHub, check that your website looks OK locally by running:\n",
155+
"\n",
156+
" nbdev_preview\n",
157+
"\n",
158+
"Now prepare to commit to GitHub:\n",
159+
"\n",
160+
" nbdev_prepare\n",
161+
"\n",
162+
"You can now commit to GitHub as usual. Finally, update Github Pages by clicking on the Settings tab in your repo, then click Pages on the left side bar. Set “Source” to gh-pages branch and the /root folder."
163+
]
164+
}
165+
],
166+
"metadata": {
167+
"kernelspec": {
168+
"display_name": "Python 3 (ipykernel)",
169+
"language": "python",
170+
"name": "python3"
171+
}
172+
},
173+
"nbformat": 4,
174+
"nbformat_minor": 4
175+
}

0 commit comments

Comments
 (0)