Skip to content

Commit

Permalink
Merge pull request #2355 from opensafely-core/formatted-tracebacks
Browse files Browse the repository at this point in the history
Add markers for html table output.
  • Loading branch information
bloodearnest authored Jan 15, 2025
2 parents 5646fb9 + 8f0cf60 commit bb8c900
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ehrql/renderers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import re


START_MARKER = "<!-- start debug output -->"
END_MARKER = "<!-- end debug output -->"


def records_to_html_table(records: list[dict]):
rows = []
headers_written = False
Expand All @@ -13,7 +17,7 @@ def records_to_html_table(records: list[dict]):
rows.append(f"<tr>{row}</tr>")
rows = "".join(rows)

return f"<table><thead>{headers}</thead><tbody>{rows}</tbody></table>"
return f"{START_MARKER}<table><thead>{headers}</thead><tbody>{rows}</tbody></table>{END_MARKER}"


def records_to_ascii_table(records: list[dict]):
Expand All @@ -35,7 +39,7 @@ def _truncate_html_table(table_repr: str, head: int | None, tail: int | None):
values to indicate where it's been truncated
"""
regex = re.compile(
r"(?P<start>^<table>.*<tbody>)(?P<rows><tr>.*<\/tr>)(?P<end><\/tbody>.*<\/table>)"
rf"(?P<start>^{START_MARKER}<table>.*<tbody>)(?P<rows><tr>.*<\/tr>)(?P<end><\/tbody>.*<\/table>{END_MARKER})"
)
match = regex.match(table_repr)
if match is None:
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_render_table(render_format):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>i1</th><th>i2</th>"
Expand All @@ -50,6 +51,7 @@ def test_render_table(render_format):
"<tr><td>5</td><td>501</td><td>511</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}
rendered = DISPLAY_RENDERERS[render_format](TABLE.to_records()).strip()
Expand All @@ -68,6 +70,7 @@ def test_render_column(render_format):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>value</th>"
Expand All @@ -77,6 +80,7 @@ def test_render_column(render_format):
"<tr><td>2</td><td>201</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}

Expand All @@ -103,6 +107,7 @@ def test_render_table_head(render_format):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>i1</th><th>i2</th>"
Expand All @@ -113,6 +118,7 @@ def test_render_table_head(render_format):
"<tr><td>...</td><td>...</td><td>...</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}

Expand All @@ -134,6 +140,7 @@ def test_render_table_tail(render_format):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>i1</th><th>i2</th>"
Expand All @@ -144,6 +151,7 @@ def test_render_table_tail(render_format):
"<tr><td>5</td><td>501</td><td>511</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}

Expand All @@ -167,6 +175,7 @@ def test_render_table_head_and_tail(render_format):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>i1</th><th>i2</th>"
Expand All @@ -179,6 +188,7 @@ def test_render_table_head_and_tail(render_format):
"<tr><td>5</td><td>501</td><td>511</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}

Expand Down Expand Up @@ -207,6 +217,7 @@ def test_render_table_bad_head_tail(render_format, head_tail):
"""
).strip(),
"html": (
"<!-- start debug output -->"
"<table>"
"<thead>"
"<th>patient_id</th><th>i1</th><th>i2</th>"
Expand All @@ -219,6 +230,7 @@ def test_render_table_bad_head_tail(render_format, head_tail):
"<tr><td>5</td><td>501</td><td>511</td></tr>"
"</tbody>"
"</table>"
"<!-- end debug output -->"
),
}
head, tail = head_tail
Expand Down

0 comments on commit bb8c900

Please sign in to comment.