fastai / nbdev

Create delightful software with Jupyter Notebooks
https://nbdev.fast.ai/
Apache License 2.0
4.93k stars 488 forks source link

Exporting to multiple files from one notebook? #186

Closed puzzler10 closed 4 years ago

puzzler10 commented 4 years ago

Wondering if there is an easy way to write to two python files from one notebook.

I saw here we could do it with the #export flag, but I'm probably doing something wrong because it's not working for me.

Here is a test script, saved as testing.ipynb (blank lines represent cell dividers):

# default_exp components

#export 
def test(): print("test1")

#export another_file
def test2(): print("test2")

from nbdev.export import notebook2script
notebook2script('testing.ipynb')

I get an error message from notebook2script like: FileNotFoundError: [Errno 2] No such file or directory: 'pathhere/package_name/another_file.py'

Any thoughts?

sgugger commented 4 years ago

another_file needs to have been created by a notebook before. E.g., you need a notebook numbered with a lower index that will default export to another_file.

puzzler10 commented 4 years ago

Got it! Thanks for that.

I guess what I'm looking for is a way to develop using one Jupyter notebook, and be able to export all the various components to their own files, rather than later creating a new notebook for each class. I'm using AllenNLP now and something like that would make the workflow very easy.

This might also be handy with Pytorch, for example:

from torch.utils.data import Dataset

#export dataset
class SomeDataset(Dataset): 
    # some code here loading a dataset

#export model
class Model(nn.Module):
  # some model code here 

#export diagnostics 
class Diagnostics: 
  # misc class for unit testing, output checking
sizhky commented 3 years ago

Hi I was looking for the exact same thing. Sure it helps to separate out the notebooks to do their own things, but sometimes we develop multiple components in the same notebook and it helps to export them to different locations

hamelsmu commented 3 years ago

We are tracking this feature request here https://github.com/fastai/nbdev/issues/405