Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notebook as scripts #20

Open
hamelsmu opened this issue Jul 10, 2022 · 8 comments
Open

Notebook as scripts #20

hamelsmu opened this issue Jul 10, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@hamelsmu
Copy link
Contributor

From @seeM

image

@hamelsmu hamelsmu added the enhancement New feature or request label Jul 10, 2022
@seeM
Copy link
Contributor

seeM commented Jul 13, 2022

Here's a quick implementation that I've started using:

# nbscript.py
from fastcore.script import *
from nbprocess.export import nb_export
from nbprocess.processors import _hide_dirs
from pathlib import Path
from runpy import run_path
from tempfile import TemporaryDirectory

def keep_code(cell):
    "Remove cells that aren't exported or hidden (opposite of `rm_export`)"
    if not (cell.directives_ and (cell.directives_.keys() & _hide_dirs) and cell.cell_type == 'code'): del(cell['source'])

@call_parse(nested=True)
def nbscript(nb_path:str, # Notebook path to execute 
             script_name:str=None): # Name of the script used in the help message (default: `nb_path`  with special chars replaced by `_`)
    "Run notebooks as scripts"
    nb_path = Path(nb_path)
    script_name = script_name or nb_path.stem.replace('-','_') # TODO: should replace all special chars
    with TemporaryDirectory() as td:
        lib_path = Path(td)/script_name
        nb_export(nb_path, lib_path)
        run_path(str(lib_path), run_name='__main__')

@seeM
Copy link
Contributor

seeM commented Jul 13, 2022

Since this is such a thin wrapper, it might be move convenient if this could be achieved by piping nbprocess_export to python, e.g:

nbprocess_export —stdout script.ipynb | python -

(The --stdout option doesn't exist yet.)

@hamelsmu
Copy link
Contributor Author

That seems very doable!

@dleen
Copy link
Contributor

dleen commented Aug 3, 2022

I was also thinking about this for a bit and came up with this which involves parametrizing and scripts:

Have a cell in your notebook that essentially is a docment:

#|params
a:int=1 # This is parameter a
b:str="foo" # This is another parameter...
c:int=my_func() # Call a function to get the default value?

You can have a markdown cell before the params which acts as the helpstring for the script e.g.

"This is the rest of the documentation for the script?"

As usual a cell after the #|params cell is injected with the values given by the user.

This notebook could be run via a new function like:

def exec_nb_params(
    ...
    *args
    **kwargs
)

where args and kwargs are the positional and optional arguments. The injection
with inject_idx etc is taken care of by this function .

Because we are using docments and essentially the call_parse mechanism used elsewhere in fastai then we can get a CLI script

exec_nb_params my_nb.ipynb --help

which would print

This is the rest of the documentation for the script

@jph00
Copy link
Contributor

jph00 commented Aug 7, 2022

The cell could just have the docstring in it directly:

#|params
a:int=1 # This is parameter a
b:str="foo" # This is another parameter...
c:int=my_func() # Call a function to get the default value?
"This is the rest of the documentation for the script?"

Just to confirm my understanding - the purpose of this is to get a help message (and param error checking) for the "script"? And also to have any values passed at the command line injected into the appropriate cell?

Perhaps you could also specific the cell index of the place where the params will be injected? e.g. to inject into cell 2:

#|params 2

@dleen
Copy link
Contributor

dleen commented Aug 7, 2022

Just to confirm my understanding - the purpose of this is to get a help message (and param error checking) for the "script"? And also to have any values passed at the command line injected into the appropriate cell?

Correct

Perhaps you could also specific the cell index of the place where the params will be injected? e.g. to inject into cell 2:

Makes sense

Mostly just exchanging ideas here, haven't implemented anything yet

@MichaelJFishmanBA
Copy link

MichaelJFishmanBA commented Aug 9, 2022

I was just looking for a tool to run a notebook with parameters and save the resulting notebook. Papermill does this, but its parameters rely on tagging Jupyter cells, which I can't do easily in VSCode.

@seeM
Copy link
Contributor

seeM commented Aug 11, 2022

I don't think it would be too hard to get a working prototype of this with execnb. If anyone would like to take a shot, I'm happy to share pointers. I unfortunately won't be able to get around to it myself just yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants