GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — nbdev/docs/example.html at master · SpectrumIO/nbdev Expand file tree/ Copy pathexample.htmlMore file actions 303 lines (271 loc) · 31.6 KB / Copy pathexample.htmlFile metadata and controls303 lines (271 loc) · 31.6 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 --- title: A Minimal Example keywords: fastai sidebar: home_sidebar summary: "A minimal end-to-end example of creating a nbdev project from scratch." description: "A minimal end-to-end example of creating a nbdev project from scratch." nb_path: "nbs/example.ipynb" --- <!-- ################################################# ### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ### ################################################# # file to edit: nbs/example.ipynb # command to build the docs after a change: nbdev_build_docs --> <div class="container" id="notebook-container"> {% raw %} <div class="cell border-box-sizing code_cell rendered"> </div> {% endraw %} <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>This section assumes you have gone through the <a href="https://nbdev.fast.ai/tutorial.html">tutorial</a>. The following is a minimal example of creating a nbdev project from scratch, with some commentary around why certain things work the way they do.</p> <p>For this example, we will use code from <a href="https://github.com/AllenDowney/ThinkPython2">Allen Downey's excellent book, Think Python 2</a>, particularly the <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card.py">Card</a> module. We will not cover all the features of <code>nbdev</code> as we focus on providing you with just enough information to become productive. We recommend reading the rest of <a href="https://nbdev.fast.ai/">the docs</a> when you are done with this example.</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-1:-Setup-your-nbdev-GitHub-Repo">Step 1: Setup your nbdev GitHub Repo<a class="anchor-link" href="#Step-1:-Setup-your-nbdev-GitHub-Repo"> </a></h2> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>Per the instructions in <a href="https://nbdev.fast.ai/tutorial.html">the tutorial</a>, we will make a new repository using <a href="https://github.com/fastai/nbdev_template">the template</a>. In this case, we make a repository called <a href="https://github.com/fastai/deck_of_cards">deck_of_cards</a>: {% include note.html content='If you plan on writing installable Python modules, we highly recommend naming your repo after your python module.' %}</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p><img src="/images/att_00010.png" alt="image.png"></p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>After you do this, you will have a repo with the necessary files to get started. You should also <a href="https://nbdev.fast.ai/tutorial.html#Set-up-Your-Jupyter-Server">setup nbdev</a> and <a href="https://nbdev.fast.ai/tutorial.html#Install-git-hooks">install the githooks</a> per the instructions in <a href="https://nbdev.fast.ai/tutorial.html">the tutorial</a>.</p> <h2 id="Step-2:-Modify-Configuration-Files">Step 2: Modify Configuration Files<a class="anchor-link" href="#Step-2:-Modify-Configuration-Files"> </a></h2><h3 id="Edit-settings.ini">Edit settings.ini<a class="anchor-link" href="#Edit-settings.ini"> </a></h3><p><a href="https://nbdev.fast.ai/tutorial.html#Edit-settings.ini">Editing settings.ini</a> is required for nbdev to work properly. These settings are used to populate required information for you to host documentation on <a href="https://nbdev.fast.ai/tutorial.html#Github-pages">GitHub Pages</a>, as well to <a href="https://nbdev.fast.ai/tutorial.html#Upload-to-pypi">publish your modules as packages to pypi</a>.</p> <p>These are the fields that we changed in <a href="https://github.com/fastai/deck_of_cards/blob/master/settings.ini">settings.ini</a>:</p> <div class="highlight"><pre><span></span><span class="na">lib_name</span> <span class="o">=</span> <span class="s">deck_of_cards</span> <span class="na">description</span> <span class="o">=</span> <span class="s">"A minimal example of nbdev using code from Allen Downey's Think Python 2nd Ed"</span> <span class="na">keywords</span> <span class="o">=</span> <span class="s">nbdev</span> <span class="na">author</span> <span class="o">=</span> <span class="s">Hamel Husain</span> <span class="na">author_email</span> <span class="o">=</span> <span class="s">[email protected]</span> <span class="na">copyright</span> <span class="o">=</span> <span class="s">Hamel, inc.</span> <span class="na">user</span> <span class="o">=</span> <span class="s">fastai</span> </pre></div> <p>{% include note.html content='the value for user wasn’t actually changed in this example, but you will have to change it to be the org corresponding to your GitHub repository (usually your username) on GitHub. ' %} The values in settings.ini file are propagated to various systems for you automatically, which help minimize boilerplate and learning complicated configuration files. For example,</p> <ul> <li>The <code>author</code> and <code>author_email</code> fields are read by <a href="https://github.com/fastai/deck_of_cards/blob/master/setup.py">setup.py</a> for python packaging.</li> <li>The <code>lib_name</code> is used by both by <a href="https://github.com/fastai/deck_of_cards/blob/master/setup.py">setup.py</a> and <a href="https://jekyllrb.com/">Jekyll</a>'s configuration file, <a href="https://github.com/fastai/deck_of_cards/blob/master/docs/_config.yml">_config.yaml</a> to make sure the rendered docs are configured correctly on GitHub pages.</li> </ul> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-3:-Write-Code-(Or-Copy/Paste-Into-A-Notebook)">Step 3: Write Code (Or Copy/Paste Into A Notebook)<a class="anchor-link" href="#Step-3:-Write-Code-(Or-Copy/Paste-Into-A-Notebook)"> </a></h2> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>Next, we take the <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card.py#L17">Card module from the ThinkPython2 repo</a> and write it in nbdev.</p> <p>The first step is to copy and paste the <code>Card</code> class from <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card.py">Card.py</a> into a new Jupyter notebook, which we have named <a href="https://github.com/fastai/deck_of_cards/blob/master/00_card.ipynb">00_card.ipynb</a>. Copying and pasting code from python files is a reasonable way to convert existing python scripts into Jupyter notebooks. A useful trick for copying big blocks of code into notebooks is to copy the whole file into a single cell and use <code>ctrl-shift-minus</code> to split the code into separate cells.</p> <p>If you are trying to convert an existing python project to <code>nbdev</code>, we recommend incrementally converting specific files to <code>nbdev</code> over time. Specifically, we recommend choosing one python file to begin with like the example with <code>card.py</code> shown below. {% include note.html content='The number at the beginning of the filename is not required; it is a convention we use to keep notebooks in a desired order when they are sorted by the the file system.' %} In the first cell of <a href="https://github.com/fastai/deck_of_cards/blob/master/00_card.ipynb">the notebook</a>, write a comment that looks like this (this is not required but we do it here to highlight an important feature of nbdev):</p> <div class="highlight"><pre><span></span><span class="c1"># default_exp card</span> </pre></div> <p>In this case, the argument <code>card</code> specifies that code exported from this notebook will be placed in the destination <code>card.py</code> by default. You can read more about how python modules are created from notebooks <a href="https://nbdev.fast.ai/export.html">here</a>. A reasonable way to arrange the notebook will be like this:</p> <p><img src="/images/att_00011.png" alt="image.png"></p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h3 id="Flags">Flags<a class="anchor-link" href="#Flags"> </a></h3><p>nbdev uses special comments, or flags, as a markup language that allows you to control various aspects of the docs and how code is exported to modules, and how code is tested. In addition to <code>default_exp</code>, the the following other flags are present in this notebook:</p> <p><code>#hide</code> {% include note.html content='This comment instructs nbdev to hide this cell when generating the docs.' %}</p> <p><code>#export</code> {% include note.html content='This comment instructs nbdev to export this cell to the appropriate python file. When no argument is provided to <a href="/export.html"><code>export</code></a>, this defaults to the module specified by <code>default_exp</code> as described above.' %}</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h3 id="Where-The-Magic-Happens:-Write-Documentation-And-Tests">Where The Magic Happens: Write Documentation And Tests<a class="anchor-link" href="#Where-The-Magic-Happens:-Write-Documentation-And-Tests"> </a></h3><p>In the original code base, tests for Card are separate, located in <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card_test.py">Card_test.py</a>. Furthermore, the documentation for <code>Card</code> is primarily located in the <a href="https://github.com/AllenDowney/ThinkPython2/tree/master/book">book folder</a> of Allen's repo as well as some documentation in the docstring. While this is a typical arrangement for a Python project, we believe <code>nbdev</code> can make your workflow easier by organizing the docs, tests and source code into a single context. We believe this allows developers to write higher quality documentation and code, and encourages more testing.</p> <p>Here is what the documentation + code looks like for Card:</p> <p><img src="/images/att_00012.png" alt="image.png"></p> <p>These comments and tests are rendered by the documentation system which is discussed in a later section. Furthermore, the assert statements automatically become tests that are run by the <a href="https://nbdev.fast.ai/tutorial.html#CI">continuous integration system setup by default in nbdev</a> in your GitHub repository. {% include note.html content='The nbdev programming environment comes set up with a <a href="https://nbdev.fast.ai/tutorial.html#CI">continuous integration (CI)</a> system for you. You don’t have to do anything extra to enable it, and it starts working immediately. This is especially great for people who have no experience with CI; it is a gentle way to start using it.' %}{% include important.html content='There are testing utilities provided by <a href="https://fastcore.fast.ai/test.html">fastcore’s testing utilities</a>, which provide wrappers around common types of assert statements and also provide better default error messages. Using these is optional, but recommended.' %}</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h3 id="Edit-index.ipynb">Edit index.ipynb<a class="anchor-link" href="#Edit-index.ipynb"> </a></h3> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p><code>nbdev</code> repositories require a notebook named <code>index.ipynb</code>, which is included in your repository when you use the template. <code>index.ipynb</code> serves two purposes:</p> <ol> <li>It becomes the README for your repo (this notebook is converted to <code>README.md</code>)</li> <li>It becomes home page (<code>index.html</code>) for your documentation. </li> </ol> <p>You will notice the following boilerplate in <code>index.ipynb</code>:</p> <div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">your_lib.core</span> <span class="kn">import</span> <span class="o">*</span> </pre></div> <p>You should remove this line of code or comment it out, as it will cause a syntax error. Later, when you are finished creating your module, you can replace this with the appropriate import statement. We left this line in here on purpose so that you can experience how the continuous integration system (discussed above) warns you of errors.</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-4:--Convert-Notebooks-To-Python-Modules-&-Docs">Step 4: Convert Notebooks To Python Modules & Docs<a class="anchor-link" href="#Step-4:--Convert-Notebooks-To-Python-Modules-&-Docs"> </a></h2><p><strong>Run the command <a href="/export2html.html#nbdev_build_lib"><code>nbdev_build_lib</code></a></strong> from the root of the repo. This exports notebook cells tagged with <code>#export</code> to the appropriate python module. For example, the notebook <a href="https://github.com/fastai/deck_of_cards/blob/master/00_card.ipynb">00_cards.ipynb</a> gets converted to <a href="https://github.com/fastai/deck_of_cards/blob/master/deck_of_cards/card.py">card.py</a>.</p> <p><strong>Run the command <a href="/test.html#nbdev_test_nbs"><code>nbdev_test_nbs</code></a></strong> to run the code and tests in all the notebooks. This command also gets run by the <a href="https://nbdev.fast.ai/tutorial.html#CI">continuous integration system setup for you by nbdev</a>, but it is useful to run these tests locally to get immediate feedback. {% include note.html content='there is a way to optionally skip certain tests that are long running or slow by using special tags <a href="https://nbdev.fast.ai/test.html">described here</a>' %}</p> <h3 id="Preview-The-Docs">Preview The Docs<a class="anchor-link" href="#Preview-The-Docs"> </a></h3><p>To preview the docs, run the command <code>make docs_serve</code> from the root of your repo. This command runs the CLI command <a href="/export2html.html#nbdev_build_docs"><code>nbdev_build_docs</code></a> behind the scenes for you, which generates a documentation site from the notebooks. After running this command, you will see a URL in the terminal indicating where the docs have been hosted locally. For the <a href="https://github.com/fastai/deck_of_cards/">fastai/deck_of_cards</a> repository we are using for this example, the url is <code>http://127.0.0.1:4000/deck_of_cards/</code></p> <p>If you navigate to the cards page at <code>http://127.0.0.1:4000/deck_of_cards/card.html</code>, you will see the docs that we just wrote, which we have annotated for further explanation:</p> <p><img src="/images/att_00016.png" alt="image.png"></p> <h4 id="Explanation-of-annotations:">Explanation of annotations:<a class="anchor-link" href="#Explanation-of-annotations:"> </a></h4><ol> <li><p>The heading <strong>Card</strong> corresponds to the first <code>H1</code> heading in a notebook with a note block <em>API Details</em> as the summary.</p> </li> <li><p><code>nbdev</code> automatically renders a Table of Contents for you.</p> </li> <li><p><code>nbdev</code> automatically renders the signature of your class or function as a heading.</p> </li> <li><p><code>nbdev</code> automatically adds a link to the corresponding source code (which is a plain-text python file) on GitHub. Remember, <code>nbdev</code> converts Jupyter notebooks to source code with the command <a href="/export2html.html#nbdev_build_lib"><code>nbdev_build_lib</code></a>.</p> </li> <li><p>This part of docs is rendered automatically from the docstring.</p> </li> <li><p>The rest of the notebook is rendered by converting your markdown into HTML, showing the inputs and outputs of each of your cells (including charts and images), and so forth. You can hide entire cells, hide only cell input or hide only output by using the <a href="https://nbdev.fast.ai/export2html.html">flags described on this page</a>.</p> </li> <li><p>nbdev supports special block quotes that render as colored boxes in the documentation. You can read more about them <a href="https://nbdev.fast.ai/export2html.html#add_jekyll_notes">here</a>. In this specific example, we are using the <code>Note</code> block quote.</p> </li> <li><p>Words you surround in backticks will be automatically hyperlinked to the associated documentation where appropriate. This is a trivial case where <code>Card</code> class is defined immediately above, however this works across pages and modules. We will see another example of this in later steps.</p> </li> </ol> <h3 id="show_doc">show_doc<a class="anchor-link" href="#show_doc"> </a></h3><p><a href="/showdoc.html#show_doc"><code>show_doc</code></a> allows you to control how documentation is shown on the docs. You can control the location, order, heading and other details of how documentation is rendered. You can read more <a href="https://nbdev.fast.ai/showdoc.html#show_doc">about it here</a>. For example, this is how you can use <a href="/showdoc.html#show_doc"><code>show_doc</code></a> to render the docs for the <code>__eq__</code> method of <code>Card</code> (notice how the tests are naturally included below the documentation):</p> <p><img src="/images/att_00017.png" alt="image.png"></p> <h4 id="Important-Notes-about-show_doc:">Important Notes about <a href="/showdoc.html#show_doc"><code>show_doc</code></a>:<a class="anchor-link" href="#Important-Notes-about-show_doc:"> </a></h4><ul> <li><strong>For functions and classes, <a href="/showdoc.html#show_doc"><code>show_doc</code></a> is called by default automatically</strong> in the same location where the function or class is defined. This is why you see a heading for the class <code>Cards</code> in the docs example above even though <a href="/showdoc.html#show_doc"><code>show_doc</code></a> was never explicitly called. <ul> <li>You can override this default by explicitly calling <a href="/showdoc.html#show_doc"><code>show_doc</code></a> in your desired location.</li> </ul> </li> <li><strong>For methods, you must call <a href="/showdoc.html#show_doc"><code>show_doc</code></a></strong> for a documentation heading to appear, as illustrated in the <code>Card.__eq__</code> method above. This is by design, since unlike functions, you typically define all of the methods for a class in a single contiguous block of code. Therefore, <a href="/showdoc.html#show_doc"><code>show_doc</code></a> allows you to control both the order and placement of documentation headers for methods which help you write prose and tests for each method organized under the appropriate header.</li> <li>We recommend experimenting with <a href="/showdoc.html#show_doc"><code>show_doc</code></a> by editing a notebook and re-rendering the docs (described below) to see the what happens in different scenarios.</li> </ul> <h3 id="Refresh-the-docs">Refresh the docs<a class="anchor-link" href="#Refresh-the-docs"> </a></h3><p>If you want to edit the docs, you can make a change to the corresponding notebook(s) and run <a href="/export2html.html#nbdev_build_docs"><code>nbdev_build_docs</code></a> followed by a <a href="https://www.getfilecloud.com/blog/2015/03/tech-tip-how-to-do-hard-refresh-in-browsers/#.X52j8lNKjng">hard refresh</a> in your browser to re-render the docs.</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-5:-Push-Files-To-GitHub-And-View-Hosted-Docs">Step 5: Push Files To GitHub And View Hosted Docs<a class="anchor-link" href="#Step-5:-Push-Files-To-GitHub-And-View-Hosted-Docs"> </a></h2> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>This step assumes you have already <a href="https://nbdev.fast.ai/tutorial.html#Github-pages">enabled GitHub pages</a>.</p> <p>At this point we are ready to push your first files to GitHub. If you have <a href="https://nbdev.fast.ai/#Avoiding-and-handling-git-conflicts">installed the git hooks per the tutorial instructions</a>, <code>nbdev</code> will automatically clean unnecessary metadata out of notebooks to avoid conflicts and overly verbose diffs. Before pushing your files to GitHub for the first time, we recommend running the command <code>git status</code> so you can see all the files generated by <code>nbdev</code> for you. You will notice that the following files were created:</p> <ul> <li><code>.py</code> files corresponding to the notebooks you created, in a folder corresponding to the library name, which in this case is called <a href="https://github.com/fastai/deck_of_cards/tree/master/deck_of_cards">deck_of_cards</a>. For example, an <code>__init__.py</code> file is automatically created in the proper directory in order to organize a python module.</li> <li>files for your docs site in a <code>docs/</code> folder. This directory contains HTML, CSS, and other files that are used for hosting your docs site on GitHub Pages. </li> </ul> <p>Make sure you add all these files to your commit with <code>git add</code> before you push to GitHub, because they are all needed for everything to work correctly.</p> <p>Pushing your files to GitHub will automatically trigger <a href="https://nbdev.fast.ai/#Using-nbdev-as-part-of-your-CI">continuous integration (CI) using GitHub Actions</a>. The CI will automatically perform a number of checks <a href="https://nbdev.fast.ai/tutorial.html#CI">outlined here</a>. You can see the CI process running in GitHub Actions by navigating to the Actions tab in your GitHub repo.</p> <p>After pushing your files, GitHub will rebuild your docs automatically. You can view the status of your doc build by going to your repository settings and finding the GitHub Pages section under options. When GitHub is in the process of building your pages, it will look like this:</p> <p><img src="/images/att_00018.png" alt="image.png"></p> <p>When the page is finished being built, the color and status message will change to look like this:</p> <p><img src="/images/att_00019.png" alt="image.png"></p> <p>Furthermore, assuming that you have already <a href="https://nbdev.fast.ai/tutorial.html#Github-pages">enabled GitHub Pages</a>, you can see the status of your Github Pages deployments at anytime. If you add <code>/deployments</code> to your repository's GitHub URL you will see a deployments dashboard. For example, below is a screen shot of <a href="https://github.com/fastai/deck_of_cards/deployments">https://github.com/fastai/deck_of_cards/deployments</a> right after pushing new files:</p> <p><img src="/images/att_00021.png" alt="image.png"></p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-6:-Add-More-Code">Step 6: Add More Code<a class="anchor-link" href="#Step-6:-Add-More-Code"> </a></h2><p>Congratulations, you authored your first piece of code with nbdev! However, to fully grasp how nbdev works, it is worthwhile to add additional code in a new notebook that imports the code you wrote earlier. Next, we will add the <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card.py#L55">Deck class form cards.py</a> into a new notebook called <a href="https://github.com/fastai/deck_of_cards/blob/master/01_deck.ipynb">01_deck.ipynb</a>. This notebook imports the previously created <code>Card</code> class and creates a <code>Deck</code>, which is a collection of Cards:</p> <p><img src="/images/att_00022.png" alt="image.png"></p> <p>Similar to the previous notebook, the first cell has the nbdev flag <code># default_exp deck</code> which means that code blocks marked with <code>#export</code> will be exported to the file <code>deck.py</code> by default. You can see that we import the <code>Card</code> object and export that code to <code>deck.py</code> with the following code cell:</p> <div class="highlight"><pre><span></span><span class="c1">#export</span> <span class="kn">from</span> <span class="nn">deck_of_cards.card</span> <span class="kn">import</span> <span class="n">Card</span> </pre></div> <p>This works because the cli command, <a href="/export2html.html#nbdev_build_lib"><code>nbdev_build_lib</code></a> converted <code>00_card.ipynb</code> to <code>card.py</code>, which we have imported here.</p> <h3 id="Tests">Tests<a class="anchor-link" href="#Tests"> </a></h3><p>Downey's code contains a test for the <code>Deck</code> class in a separate file called <a href="https://github.com/AllenDowney/ThinkPython2/blob/master/code/Card_test.py">Card_test.py</a>. This file is a good example that highlights the strengths of <code>nbdev</code>. The contents of this file is as follows:</p> <div class="highlight"><pre><span></span><span class="sd">"""This file contains code for use with "Think Stats",</span> <span class="sd">by Allen B. Downey, available from greenteapress.com</span> <span class="sd">Copyright 2014 Allen B. Downey</span> <span class="sd">License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html</span> <span class="sd">"""</span> <span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span><span class="p">,</span> <span class="n">division</span> <span class="kn">import</span> <span class="nn">unittest</span> <span class="kn">from</span> <span class="nn">Card</span> <span class="kn">import</span> <span class="n">Card</span><span class="p">,</span> <span class="n">Deck</span> <span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span> <span class="k">def</span> <span class="nf">testDeckRemove</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">deck</span> <span class="o">=</span> <span class="n">Deck</span><span class="p">()</span> <span class="n">card23</span> <span class="o">=</span> <span class="n">Card</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="n">deck</span><span class="o">.</span><span class="n">remove_card</span><span class="p">(</span><span class="n">card23</span><span class="p">)</span> <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span> <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span> </pre></div> <p>The code shown above is problematic for the following reasons:</p> <ul> <li>It is not clear what the purpose of the test is. </li> <li>The test is located in a file that is separate from the implementation, so you have to open multiple windows and/or switch contexts to understand the test. </li> <li>The tests uses an api, <code>unittest</code> that you must learn and think about if you want to write tests.</li> <li>The tests are separate from the documentation and any prose associated with explaining what the class <code>Deck</code> does. </li> </ul> <p>All of these problems are handled in nbdev, as you can write code, docs and tests all in the same context. Below is a screen shot of the relevant parts of <a href="https://github.com/fastai/deck_of_cards/blob/master/01_deck.ipynb">01_deck.ipynb</a> that expresses the code and this test in a more readable, expressive way:</p> <p><img src="/images/att_00023.png" alt="image.png"></p> <p>The above code expresses the same unit test, but also integrates documentation alongside the original implementation of Deck. You can view the notebook on GitHub <a href="https://github.com/fastai/deck_of_cards/blob/master/01_deck.ipynb">here</a>. One additional tool shown in this notebook is the <code>nbdev</code> function <a href="/showdoc.html#show_doc"><code>show_doc</code></a>, which allows you to control the placement of documentation. In this example, <code>showdoc(Deck.remove_card)</code> will create a section in the documentation with an appropriate heading.</p> <p>If you run the CLI command <code>make docs_serve</code>, you can preview what these docs will look like locally. Below is an annotated screenshot of what this looks like:</p> <p><img src="/images/att_00024.png" alt="image.png"></p> <h4 id="Explanation-of-annotations:">Explanation of annotations:<a class="anchor-link" href="#Explanation-of-annotations:"> </a></h4><ol> <li><p>When writing these docs, we simply enclosed <code>Card</code> with backticks. <code>nbdev</code> automatically transforms this into a hyperlink to the appropriate page that documents <code>Card</code>.</p> </li> <li><p>This heading for the method <code>Deck.remove_card</code> was created by <a href="/showdoc.html#show_doc"><code>show_doc</code></a>.</p> </li> <li><p><code>nbdev</code> is designed to encourage you to write your tests as part of your documentation as shown here.</p> </li> </ol> <p>You can see this page live at <a href="https://fastai.github.io/deck_of_cards/deck.html">https://fastai.github.io/deck_of_cards/deck.html</a>.</p> <p>When you are done make sure you run the following cli commands before pushing to GitHub.</p> <ul> <li><a href="/export2html.html#nbdev_build_lib"><code>nbdev_build_lib</code></a>: this converts your notebooks into modules.</li> <li><a href="/export2html.html#nbdev_build_docs"><code>nbdev_build_docs</code></a>: this generates the your documentation site.</li> <li><a href="/test.html#nbdev_test_nbs"><code>nbdev_test_nbs</code></a>: this runs all your tests (which is a good idea so you can catch errors).</li> <li><code>git status</code> to see which files have changed, which is a good exercise when first getting started with <code>nbdev</code> to understand which files are automatically generated.</li> </ul> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="Step-7:-Publish-Python-Module-to-Pypi">Step 7: Publish Python Module to Pypi<a class="anchor-link" href="#Step-7:-Publish-Python-Module-to-Pypi"> </a></h2> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <p>You can publish your module to pypi by following <a href="https://nbdev.fast.ai/tutorial.html#Upload-to-pypi">these instructions</a>.</p> </div> </div> </div> <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell"> <div class="text_cell_render border-box-sizing rendered_html"> <h2 id="References">References<a class="anchor-link" href="#References"> </a></h2><p>All the code for this example is available on the GitHub repo <a href="https://github.com/fastai/deck_of_cards">fastai/deck_of_cards</a>.</p> <h3 id="Live-Demo">Live Demo<a class="anchor-link" href="#Live-Demo"> </a></h3><p>The below video shows a live demonstration of this example, with a Q&A section at the end.</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/ZJTop5uqC2U" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </div> </div> </div> |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |