Exporting multiple notebooks to the same module

Hi,

Spending a lot of time learning nbdev, fanstastic tool! Here’s the situation:

Situation

  • A single project is under development, myproject0
  • A statistical model is developed in a notebook, exported to module model_v1 so we access it with myproject0.model_v1
  • 5 Helper functions are needed, which I will reuse in other models, and each is developed in its own notebook, because easier to handle like that.
  • In that project, those 5 helpers will be found in module helper so accessible through myproject0.helper.f0 and where 0 in f0 can be replaced with any value in [0:5].

Questions

  1. How can I export each of the 5 helper functions notebooks to the same module? If I use the same name with the directive default_exp helper, they overwrite each other, so in the end the named module contains only one function.
  2. How can the helper functions evolve from a project to another? Should I create a separate project that contains those helper functions and myproject0 calls it, as well as the future myproject1?
  3. If indeed I create a separate project for the helper functions, how can the documentation of myproject0 and myproject1 include the doc of the helper functions, given they are all in a separate project?
1 Like

Have you tried this directive? nbdev - Directives

I haven’t tried to do what you’re talking about, but I have the sense it is possible.

2 Likes

I believe that combined @hugetim’s point of using #|export some.module.name should be enough.

You may also become interested in "patch"ing individual methods to the same class. I haven’t tried it across notebooks, but I suspect it should work.

As you suggest, organizing your modules so that helper functions live in their own separate project is a common technique. It can add some complexity though, so just use your best judgement as the scale of your projects grows. In some cases, it’s much easier to just replicate the same function a few times until you’ve got a stable version you are happy with. And only extract it into the helper library at that point.

Aside: deciding when that point comes is a matter of personal taste. In my experience, most simple helper functions don’t need extraction until you’ve copied it into three places. But it really depends on the level of quality control in you work context.

2 Likes

Unfortunately, it seems that that feature hasn’t been implemented in nbdev v2 (yet, at least).

1 Like

In my test it worked like that (see my answer in Allow a single notebook to export to multiple .py modules · Issue #742 · fastai/nbdev · GitHub)

However, I don’t like the fact that it appears that you can’t have two notebooks with the same default_exp (as it says here, it gets overwritten. However, i can’t find anything in the docs about this)