Skip to content

integrating typer into the blog¤

in this blog, posts are programs and they are designed to be reused in python. that means that we want to be able to reuse code and narrative as much as possible.

  • add command to main
    IMPORTED,MAIN = "__file__" in locals(),  __name__ == "__main__"; Ø = not IMPORTED and MAIN

a small example¤

the first example we'll describe is a simple function with little consequence. this will validate our integration.

    def main(greeting: str = "howdy", name: str="tony"):
        """a nothing function that says hi to the audience"""
        print(greeting, name)

invoking the command line

    if Ø:
        !python -m tonyfast
                                                                                
 Usage: python -m tonyfast [OPTIONS] COMMAND [ARGS]...                          
                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help  -h        Show this message and exit.                                │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ hello     a nothing function that says hi to the audience                    │
│ tasks     run doit tasks                                                     │
╰──────────────────────────────────────────────────────────────────────────────╯

    if Ø:
        !python -m tonyfast hello --name y\'all
howdy y'all

this is a nice example to start with because it allows us to consider what our works look like as literature.

a more complex example¤

in a recent post, we made it possible add doit tasks to the blog.

    from typer import Typer, Context
    app= Typer(rich_markup_mode="rich")

    @app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
    def tasks(ctx: Context):
        """run doit tasks"""
        from doit.doit_cmd import DoitMain
        from doit.cmd_base import ModuleTaskLoader
        from tonyfast import dodo

        return DoitMain(ModuleTaskLoader(dodo)).run(ctx.args)

verifying the doit commands

    if Ø:
        !python -m tonyfast tasks list --status --all
R mkdocs         build document with mkdocs
R mkdocs:build   
R mkdocs:toc     

invoking this document as a standalone script¤

    if IMPORTED and MAIN:
        app.command(name="hello")(main)
        app()
    if Ø:
        !importnb 2022-12-19-integrating-typer.ipynb --help
                                                                                
 Usage: 2022-12-19-integrating-typer.ipynb [OPTIONS] COMMAND [ARGS]...          
                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --install-completion          Install completion for the current shell.      │
│ --show-completion             Show completion for the current shell, to copy │
│                               it or customize the installation.              │
│ --help                        Show this message and exit.                    │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ hello     a nothing function that says hi to the audience                    │
│ tasks     run doit tasks                                                     │
╰──────────────────────────────────────────────────────────────────────────────╯