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

add nested parser support to fastcore.script #428

Merged
merged 1 commit into from
Jul 11, 2022

Conversation

seeM
Copy link
Contributor

@seeM seeM commented Jul 11, 2022

There are some limitations, see the docs on --. Also some behaviour I don't understand, like nbscript --script_name=update update-union-annots.ipynb -- -h works as intended, but nbscript update-union-annots.ipynb --script_name=update -- -h doesn't. -- is ignored in the first example but parsed in the second.

Example use-case - run notebooks as scripts:

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__')

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@jph00
Copy link
Member

jph00 commented Jul 11, 2022

lgtm. does this need to be a draft, or can it be merged?

@seeM
Copy link
Contributor Author

seeM commented Jul 11, 2022

@jph00 I consider it experimental. Could still be merged if you're happy

@seeM seeM marked this pull request as ready for review July 11, 2022 22:05
@seeM seeM changed the title nested call parse add nested parser support to fastcore.script Jul 11, 2022
@jph00
Copy link
Member

jph00 commented Jul 11, 2022

lgtm!

@jph00 jph00 merged commit fc80c64 into fastai:master Jul 11, 2022
@jph00 jph00 added the enhancement New feature or request label Jul 11, 2022
@seeM seeM deleted the nested-call-parse branch July 11, 2022 23:57
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

Successfully merging this pull request may close these issues.

2 participants