typer icon indicating copy to clipboard operation
typer copied to clipboard

Get the typer output with html format to provide it to termynal

Open GreNait opened this issue 2 years ago • 5 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

script -q -c "python data_stream_measurement/main.py --help" test.out
cat test.out | ./ansi2html.sh test.html

Description

Within the typer documentation, termynal is used to interactively show the typer features etc. They are also all styled like the typical terminal output. It would be awesome, if I could replicate this for my typer app documentation.

I tried to capture the terminal styling with several approaches:

  • Capture output as html -> didn't work
  • Use script to capture the terminal and afterwards use ansi2html -> still no coloring

Is there a way, to get the typer output saved into a file, with the html styling? So I could copy that to my markdown?

Operating System

Linux

Operating System Details

Ubuntu 20.04

Typer Version

0.7.0

Python Version

3.8.10

Additional Context

No response

GreNait avatar Jan 24 '23 16:01 GreNait

This should work

script -q -c "python /tmp/asd.py --help" | ansi2html > asd.html

That being said i guess it would be nice to have something like --color-always option like with the ls-command:

ls -la --color=always | ansi2html > asd.html

heiskane avatar Sep 21 '23 16:09 heiskane

That being said i guess it would be nice to have something like --color-always option like with the ls-command:

Colors appear just fine here, after script -q -c "clitool --help" | ansi2html > clitoolhelp.html and viewing the latter file in a browser.

NikosAlexandris avatar Oct 30 '23 10:10 NikosAlexandris

Another approach is to use the rich export functions:

If you're using sphinx-doc with reStructuredText, I wrote a directive extension to do this for you. Unfortunately typer has no official way to access the console object, so a monkey patch is involved.

bckohan avatar Dec 29 '23 23:12 bckohan

If you're using sphinx-doc with reStructuredText, I wrote a directive extension to do this for you. Unfortunately typer has no official way to access the console object, so a monkey patch is involved.

Nice work! Pity, I am trying out Material MkDocs :-/

NikosAlexandris avatar Jan 04 '24 22:01 NikosAlexandris

In case anyone wants to integrate this kind of thing directly into their application, here's an example of a command I created for our Typer application to get the full help output as HTML:

import typer
from typer.cli import docs, state

help = typer.Typer()

@help.command()
def view(ctx: typer.Context):
    """View HTML help for this tool."""

    state.module = HELP_DOCS_TARGET  # e.g. "myapp.main"

    help_docs_gen_dir = Path(tempfile.mkdtemp())
    markdown = help_docs_gen_dir / "help.md"
    html = help_docs_gen_dir / "help.html"

    docs(ctx, name=COMMAND_NAME, title=MAIN_TITLE, output=markdown)

    # Convert Markdown containing Rich console syntax to HTML
    markdown_content = markdown.read_text()
    rich_text = Text.from_markup(markdown_content)
    formatted_markdown_content = Markdown(str(rich_text))

    console = Console(record=True, file=open(os.devnull, "w"))
    console.print(formatted_markdown_content)
    console.save_html(str(html))
    
    print(f"Docs saved to: {html}")
    typer.launch(f"file://{html}")

It's possible this will be greatly simplified and improved with #819

daneah avatar May 25 '24 00:05 daneah