nbdev is getting a little %magic
Introducing magic flags in nbdev.
- Overview
- How do I start using magic flags?
- How do comment flags correspond to magic flags?
- Why might we add magic flags for _all_ and show_doc?
You'll also get feedback when flags are used incorrectly:
How do I start using magic flags?
Grab an editable install of nbdev then run nbdev_upgrade
from the command line - this will update notebooks that use comment flags like:
#export special.module
to use magic flags:
%nbdev_export special.module
To make magic flags work, nbdev_upgrade
might need to add a new code cell to the top of the notebook:
from nbdev import *
You can run nbdev_upgrade
any number of times - which means you can update the same project every time new magic flags get added to nbdev.
... you'll probably need to update settings.ini
then restart your notebook.
Test flags are configured in your settings.ini
(set tst_flags
, separating flags by a |
if you have several of them).
When the nbdev.flags
module is imported, test flags are created dynamically from your settings.ini. If tst_flags=slow|gpu
, the following flags would be available:
%nbdev_slow_test
%nbdev_gpu_test
If magic flags are found, the flags part can contain multiple lines
This could make a difference because nbdev writes just the code part to both your library and HTML docs.
How do comment flags correspond to magic flags?
Comment flag | Magic flag | |
---|---|---|
default_exp |
nbdev_default_export |
Define the name of the module everything should be exported in |
exports |
nbdev_export_and_show |
Export and show code in the docs |
exporti |
nbdev_export_internal |
Export but don’t show in docs and don’t add to __all__
|
export |
nbdev_export |
Export but don’t show in docs |
hide_input |
nbdev_hide_input |
Do not show input of a test cell in docs |
hide_output |
nbdev_hide_output |
Do not show output of a test cell in docs |
hide |
nbdev_hide |
Do not show a test cell or markdown in docs |
default_cls_lvl |
nbdev_default_class_level |
Define the default toc level of classes |
collapse_output or collapse-output
|
nbdev_collapse_output |
Inlcude output in the docs under a collapsable element |
collapse_show or collapse-show
|
nbdev_collapse_input open |
Inlcude intput in the docs under a collapsable element that is open by default |
collapse_hide or collapse-hide
|
nbdev_collapse_input |
Inlcude intput in the docs under a collapsable element |
collapse |
nbdev_collapse_input |
Inlcude intput in the docs under a collapsable element |
_all_ = ['progress_bar','master_bar']
but we could ...
%nbev_add2__all__ progress_bar,master_bar
This flag could
- check that everything exists and names are valid (when the cell is executed) and
- make it possible to tab-complete the items you're adding to
__all__
The example above uses a line magic, so you'd have to move everything on line 3 up to line 2 but ... we could use a cell magic
# nbdev removes show_doc cells, so we need to use a alias
show__doc = show_doc
show__doc(ArithmeticError)
show__doc(ArithmeticError.mro)
show__doc(ArithmeticError.__init__)
but we could ...
%nbev_show_doc ArithmeticError mro __init__
This flag could make it possible to "show doc" a class and any number of its methods with a single line of code.