Giter Club home page Giter Club logo

Comments (5)

bkanuka avatar bkanuka commented on May 27, 2024

I have a pretty ugly hack here. It's ugly because I can't find a way of "rendering" text using Rich without printing it to a console.

Example:

import io

from docstring_parser import parse

from rich.console import Console
from rich.markdown import Markdown
from rich.text import Text

docstring = """
    short description
    
    Long description that can span multiple lines. **This is a bold sentence**. This is test sentence 2. I do not want a 
    line break in this sentence.
    
        * bullet one
        * bullet two
        
    Another paragraph.
    """

parsed = parse(docstring)

print("Before rich:")
print("************")
print(parsed.long_description)


def md_to_rich(md: str) -> Text:
    with io.StringIO() as f:
        console = Console(
            file=f,
            record=True,
            force_terminal=True,
            force_jupyter=False,
            force_interactive=False,
            soft_wrap=True,
        )

        console.print(Markdown(md))

        f.seek(0)
        console_output = f.read()

    return Text.from_ansi(console_output)


rich_text = md_to_rich(parsed.long_description)
print("")
print("After md_to_rich:")
print("*****************")
print(rich_text)

This outputs:

Before rich:
************
Long description that can span multiple lines. **This is a bold sentence**. This is test sentence 2. I do not want a 
line break in this sentence.

    * bullet one
    * bullet two
    
Another paragraph.

After md_to_rich:
*****************
Long description that can span multiple lines. This is a bold sentence. This is test sentence 2. I do not want a line break in this sentence.

                                                                                
 * bullet one                                                                   
 * bullet two                                                                   
                                                                                

Another paragraph.

So if I'm reading your code correctly we could change _resolve_docstring https://github.com/BrianPugh/cyclopts/blob/main/cyclopts/resolve.py#L97 to something like:

def _resolve_docstring(f: Callable, signature: inspect.Signature) -> ParameterDict:
    iparam_to_docstring_cparam = ParameterDict()
    if f.__doc__ is None:
        return iparam_to_docstring_cparam
    f_docstring = docstring_parse(f.__doc__)

    for dparam in f_docstring.params:
        try:
            iparam = signature.parameters[dparam.arg_name]
        except KeyError:
            # Even though we could pass/continue, we're raising
            # an exception because the developer really aught to know.
            raise DocstringError(
                f"Docstring parameter {dparam.arg_name} has no equivalent in function signature."
            ) from None
        else:
            # Render dparam.description into rich Text
            iparam_to_docstring_cparam[iparam] = Parameter(help=md_to_rich(dparam.description))

    return iparam_to_docstring_cparam

This has the obvious problem of only supporting markdown (and plain text). But it's unlikely that there will be lots of complex rst formatting in a parameter description so maybe thats okay?

from cyclopts.

bkanuka avatar bkanuka commented on May 27, 2024

Sorry for all the messages. I'm working through this an learning new things 😄

This is a much less hacky way of converting the markdown to a str that rich likes:

def md_to_rich(md: str) -> str:
    console = Console()
    with console.capture() as capture:
        console.print(Markdown(md))

    return capture.get().strip()

from cyclopts.

BrianPugh avatar BrianPugh commented on May 27, 2024

Hey @bkanuka! Thanks for opening the issue and diving into the code!

I'm currently working on the branch bugfix/consistent-help-formatting to address #113, but it's currently waiting on my PRs for wasi-master/rich-rst. It might not directly resolve your issue, but I'm touching those areas of code. That branch is very much a WIP, so don't base anything off of it 😅 .

Can you post a full cyclopts example, as well as a screenshot of the output help page? Because I think it should currently behave as you'd expect, so I would like to be able to replicate.

from cyclopts.

BrianPugh avatar BrianPugh commented on May 27, 2024

@bkanuka any update on this? The default help_format="rst" (or the non-default "md") should be placing your words/sentences as expected.

from cyclopts.

BrianPugh avatar BrianPugh commented on May 27, 2024

closing due to lack of activity. If you have more info/screenshots, we can re-open.

from cyclopts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.