Skip to content

Allow running any callable in an CLI notebook #163

@bollwyvl

Description

@bollwyvl

elevator pitch

Provide a way to run any function (or other exported name) from a notebook from the command line.

motivation

argparse (or a heavier-weight dependency like typer or click) adds a fair amount of boilerplate to a notebook which would otherwise be a useful standalone, literate collection of functions.

This is particularly useful in a situation where a notebook is being used as the "meat" of some other CLI-based syntax, such as pixi.toml or dodo.py, where making a script-per-task would be a lot of extra files.

design ideas

Add -e/--entry-point as an alternative to -m.

importnb -e Untitled42:foo.bar.baz a b c

as an equivalent to:

with importnb.Notebook():
    import Untitled42
sys.exit(Untitled.foo.bar.baz("a", "b", "c"))

challenges

Arguments are complex: there's really no accounting for taste, and it's generally not possible to distinguish between positional arguments, flags (-x or --long-x, no argument), and proper options with values.

Being able to get help is also important, but maybe an IPython-style trailing ? or ?? would be workable, but would require quoting under most conditions

importnb -e Untitled42:foo.bar.baz -h
importnb -e "Untitled42:foo.bar.baz?"

Some care would need to be taken for the return code to hand to sys.exit, and might need another flag for some behavior switches:

  • pass through int unchanged
  • a la doit, treat any truthy or None as 0 (success), and anything else as 1 (fail)

There's probably some clever way (again, another flag) to suggest stdin should be passed as some kind
of argument:

ls * | importnb -e Untitled:foo --stdin=pargs      # call once, with every line as a positional argument
ls * | importnb -e Untitled:foo --stdin=kwarg:bar  # call once, with every line as a named argument
ls * | importnb -e Untitled:foo --stdin=map        # call once per line
ls * | importnb -e Untitled:foo --stdin=any        # call until the first truthy return value
ls * | importnb -e Untitled:foo --stdin=all        # call until the first falsey return value

Natively, these would be as bytes, though maybe being able to specify (or default) the encoding, e.g. --stdin-encoding=utf-8, would be sufficient for most cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions