{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# default_exp demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How To Export Code From Notebooks in nbdev\n", "\n", "> A quick demonstration on how to export code in nbdev" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The below cell doesn't get exported to any module because there is no `#export` flag and is also hidden from the docs due to the `#hide` flag." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "from nbdev.showdoc import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The below cell will export to `demo.py`, because there is no argument to export and the default was set to `demo` in the first cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# export\n", "def func(): pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The below cell will export to `module1.py` because the argument `module1` was passed to export." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# export module1\n", "def func2():pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The below cell will export to `module2.py` because the argument `module2` was passed to export." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# export module2\n", "def func3(): pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To export code, first **make sure a notebook exists for each module. If you don't do this, you will get an error. The notebooks can be minimal like the ones demonstrated in this repository**\n", "\n", "There are two choices for exporting notebooks:\n", "\n", "1. run `nbdev_build_lib` from the command line. \n", "2. run `nbdev.export.notebook2script` in python:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converted index.ipynb.\n", "Converted module1.ipynb.\n", "Converted module2.ipynb.\n" ] } ], "source": [ "from nbdev.export import notebook2script\n", "notebook2script()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }