Comments (5)
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.
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.
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.
@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.
closing due to lack of activity. If you have more info/screenshots, we can re-open.
from cyclopts.
Related Issues (20)
- support for union of None like Optional HOT 3
- App() name parameter type hint adjustment HOT 2
- Docstring hyperlink HOT 11
- [Feature request]: Nested pydantic validation HOT 9
- [Discussion] First-class config overrides via pyproject.toml? HOT 1
- [Feature Request] Completion? HOT 6
- App() console parameter HOT 2
- integration with https://pypi.org/project/typed-settings/ HOT 2
- [BUG]: bad error report because of assetion error when there is UnusedCliTokensError HOT 2
- [BUG] Weird behavior of list of tuples HOT 7
- [BUG] list of tuple of single item is parsed but take only the first character of each token get parsed HOT 2
- Misleading error message when flag name has an underscore HOT 2
- the help format parsing is not consistent HOT 2
- Misc likely (rare) typing bugs HOT 1
- Use of the meta application results in a double help display HOT 5
- Automatic conversion from underscore to hyphen HOT 3
- Prompt for missing required parameters HOT 6
- [Feature Request] Add auto_env_var_prefix HOT 9
- Importing cyclopts fails if there is a file called "tokenize.py" in the project which imports cyclopts HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cyclopts.