skip to main content

@tonyfast s notebooks

site navigation
notebook summary
title
integrating typer into the blog
description
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.
cells
15 total
8 code
state
executed out of order
kernel
Python [conda env:root] *
language
python
name
conda-root-py
lines of code
37
outputs
4
table of contents
{"kernelspec": {"display_name": "Python [conda env:root] *", "language": "python", "name": "conda-root-py"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13"}, "title": "integrating typer into the blog", "description": "in this blog, posts are programs and they are designed to be reused in python.\nthat means that we want to be able to reuse code and narrative as much as possible."}
notebook toolbar
Activate
cell ordering
1

integrating 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
2
    IMPORTED,MAIN = "__file__" in locals(),  __name__ == "__main__"; Ø = not IMPORTED and MAIN
3

a small example

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

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

invoking the command line

6
    if Ø:
        !python -m tonyfast
1 outputs.
                                                                                
 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                                                     │
â•°──────────────────────────────────────────────────────────────────────────────╯


7
    if Ø:
        !python -m tonyfast hello --name y\'all
1 outputs.
howdy y'all

8

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

9

a more complex example

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

10
    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)
11

verifying the doit commands

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

13

invoking this document as a standalone script

14
    if IMPORTED and MAIN:
        app.command(name="hello")(main)
        app()
15
    if Ø:
        !importnb 2022-12-19-integrating-typer.ipynb --help
1 outputs.
                                                                                
 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                                                     │
â•°──────────────────────────────────────────────────────────────────────────────╯