Skip to content

Rework pydantic model examples display#51

Merged
t20100 merged 2 commits into
mainfrom
update-examples-display
Jun 4, 2026
Merged

Rework pydantic model examples display#51
t20100 merged 2 commits into
mainfrom
update-examples-display

Conversation

@t20100

@t20100 t20100 commented Jun 3, 2026

Copy link
Copy Markdown
Member

This PR proposes to rework pydantic model examples display by using pretty print and syntax highlighting.
It also inlines "short" examples.

It only works for html output, I hope this is affordable.
TBH, it's quite a struggle to do what you want through docutils 😅 .

closes #9

What it looks with this PR:

image image

What it is without it:

image

@t20100 t20100 requested review from loichuder and payno June 3, 2026 12:39
has_default = False
else:
has_default = True
examples = [repr(example) for example in examples or []]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move example formatting closer in sphinx task utils, closer to rendering

}

.ewokssphinx-task code[class="highlight"] {
white-space: pre-wrap;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to keep carriage return in pretty printed code

lexer = PythonLexer()
formatter = HtmlFormatter(nowrap=True)
return (
f'<code class="highlight">{highlight(code, lexer, formatter).rstrip()}</code>'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodes.literal_block uses:

  • <pre> which is meant for block display
  • has border, background and 1rem padding

Rather than changing this through CSS and inlining <pre>, I used <code> (meant for inline).

Comment thread src/ewokssphinx/sphinx_task_utils.py Outdated

@loichuder loichuder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good !

Comment thread src/ewokssphinx/sphinx_task_utils.py Outdated
)


def _examples_node(formatted_examples: list[Any]) -> nodes.container:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input examples are not formatted yet.

Suggested change
def _examples_node(formatted_examples: list[Any]) -> nodes.container:
def _examples_node(examples: list[Any]) -> nodes.container:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! F2...

]

if sum(len(str(example)) for example in formatted_examples) < 50 and not any(
"\n" in example for example in formatted_examples

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What case are you trying to handle when checking for \n ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty print span the example on multiple lines, e.g. a long dict of list

Comment thread src/ewokssphinx/sphinx_task_utils.py Outdated
@loichuder

Copy link
Copy Markdown
Member

As for the HTML limitation, you should be able to get the output format from the directive/app options and change the formatter accordingly.

Could be another PR.

@codecov

codecov Bot commented Jun 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/ewokssphinx/sphinx_task_utils.py 87.50% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@loichuder loichuder self-requested a review June 3, 2026 13:07
@t20100

t20100 commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

you should be able to get the output format from the directive

Thanks for the hint!

Last commit bf8055d adds fallbacks (literal and literal_block) for other formatters

@t20100 t20100 force-pushed the update-examples-display branch from d60a652 to bf8055d Compare June 3, 2026 13:25
@t20100 t20100 force-pushed the update-examples-display branch from bf8055d to fb3e8d2 Compare June 4, 2026 08:00
@t20100 t20100 merged commit ef3e700 into main Jun 4, 2026
8 checks passed
@t20100 t20100 deleted the update-examples-display branch June 4, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve display of examples

3 participants