I’m trying to add a custom directive to nbdev, but without setting up nbdev project from scratch. I’d like to be able to just use nb_export as shown in “modular nbdev” section of the docs, but use a custom directive with it. But based on nbdev_extensions docs, it requires adding something to settings.ini.
How can I add a custom directive when I use just nb_export without other project settings?
To add a custom directive to nbdev while using the nb_export function without setting up a full nbdev project, you can still utilize nbdev_extensions. You’ll need to create a minimal settings.ini file to register your custom directive.
Create a Minimal settings.ini: In the root of your notebook directory, create a file named settings.ini with the following structure:
[DEFAULT]
custom_directives = my_custom_directive
Replace my_custom_directive with the name of your directive.
Define Your Custom Directive: Create a Python file (e.g., my_directives.py) and define your custom directive within it. Here’s an example:
from nbdev import *
from nbdev_extensions import *
@directive
def my_custom_directive(content):
# Your custom logic here
return f"Processed: {content}"
3. Import Your Custom Directive in Your Notebook: In your Jupyter notebook, import the directive:
from my_directives import my_custom_directive
4. Use nb_export: You can now use the nb_export function in your notebook. Just annotate your functions or classes as you normally would:
Exporting a function
@my_custom_directive
def my_function():
pass
5. Run nbdev_export: When you run nbdev_export, it will process your notebook and include your custom directive.