Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor debug rendering #2356

Merged
merged 4 commits into from
Jan 17, 2025
Merged

Refactor debug rendering #2356

merged 4 commits into from
Jan 17, 2025

Conversation

bloodearnest
Copy link
Member

@bloodearnest bloodearnest commented Jan 14, 2025

The sandbox method of evaluating and rendering ehrql objects was via
monkey patching various repr functions The initial debug implementation
re-used this method, and manually called repr() from within the show()
call.

Now that we don't have a sandbox, all rendering can be done explicitly
by the show() function, so there is no need for repr monkey patching.
This refactor switches the evaluate/render steps to be done explicitly
in show(), rather than going via repr.

However, the repr monkey patching implementation used a closure to
capture the supplied renderer and engine details in the monkeypatched
function. As show() is a user imported static function, it cannot use
a closure. Instead, we formalize the DEBUG_QUERY_ENGINE global into
a new DEBUG_CONTEXT global, which is set via activate_debug_context,
and then available to use inside show(). This DEBUG_CONTEXT stores the
engine instance and the renderer, and knows how to render and object
with those.

Also includes some follow refactors around truncation and tests.

Copy link

cloudflare-workers-and-pages bot commented Jan 14, 2025

Deploying databuilder-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9761f4b
Status: ✅  Deploy successful!
Preview URL: https://b17fa1b2.databuilder.pages.dev
Branch Preview URL: https://refactor-debug-rendering.databuilder.pages.dev

View logs

The sandbox method of evaluating and rendering ehrql objects was via
monkey patching various repr functions  The initial debug implementation
re-used this method, and manually called `repr()` from within the show()
call.

Now that we don't have a sandbox, all rendering can be done explicitly
by the show() function, so there is no need for repr monkey patching.
This refactor switches the evaluate/render steps to be done explicitly
in show(), rather than going via repr.

However, the repr monkey patching implementation used a closure to
capture the supplied renderer and engine details in the monkeypatched
function. As `show()` is a user imported static function, it cannot use
a closure. Instead, we formalize the DEBUG_QUERY_ENGINE global into
a new DEBUG_CONTEXT global, which is set via `activate_debug_context`,
and then available to use inside `show()`. This DEBUG_CONTEXT stores the
engine instance and the renderer, and knows how to render and object
with those.
Previously, we did the truncation post render, probably in order not to
mess with the default repr, which cannot be parametrised with head/tail
arguments.  However, that is gone now, and we no longer render via
a parameterless repr, so we refactor the truncation at render time.
This has several advantages

 - can use the same logic for both html and ascii rendering
 - only done in one place
 - more efficient, as we do not render the truncated rows at all.

This includes the following changes:

 - head and tail are arguments to the render() function call. The user
   interface in show() allows them to be `int | None`, but we force them
   to integer values in the implementation, with 0 meaning None, as both
   are falsey. This necessetated some changes to not use _render_
   functions, and call to_records() directly, so we could pass the
   head/tail args at render time.
 - head and tail logic is factored out into a shared function, and
   handles ellipsis logic consistently.
 - The render function's `records` parameter was annotated as
   list[dict], but it was actually a generator.  We assume that it is
   a list, and use indexing for simple head/tail splicing, and fix the
   call sites to list() the the generator.  There's no way to do tail
   w/o exhausting the generator anyway, and we were rendering the full
   thing before, so this is no less efficnet, but is simpler to read.
 - fixed the debug tests to always use a json renderer for testing, for
   consistency.
return start + truncated_rows + end
def records_to_html_table(records: list[dict], head: int = 0, tail: int = 0):
rows = []
headers = "".join([f"<th>{header}</th>" for header in records[0].keys()])
Copy link
Contributor

@rw251 rw251 Jan 17, 2025

Choose a reason for hiding this comment

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

Could records ever be an empty list? If so then I think this throws an error now which wouldn't have happened before. Same comment on the records_to_ascii_table method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, will fix. It might be better to supply the header info independently of the rows, so we can show headers for an empty table, but I am not sure how to do that, so I will go for just not breaking.

table_rows[headers : headers + head] if head is not None else [ellipsis_row]
)
truncated_rows.extend(head_rows)
return f"{START_MARKER}<table><thead><tr>{headers}</tr></thead><tbody>{html_rows}</tbody></table>{END_MARKER}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need the START_MARKER and END_MARKER? Or were they just so the previous regex would find the table?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, they are not for the regex, but for the vscode extension to know where pre-formatted html tables start and end (this bit is what I am working on now).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah of course. I'd naively just searched the ehrql code space for the marker and forgot about the extension.


"""
).strip()

show("Hello")
exec(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

Copy link
Contributor

@rw251 rw251 left a comment

Choose a reason for hiding this comment

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

Some good code simplification there.

@bloodearnest bloodearnest merged commit b4adab2 into main Jan 17, 2025
8 checks passed
@bloodearnest bloodearnest deleted the refactor-debug-rendering branch January 17, 2025 16:32
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.

2 participants