opensafely-core / airlock

A tool to review & release outputs from an OpenSAFELY backend
Other
2 stars 0 forks source link

Add a `.log` file viewer which correctly formats ANSI escape codes #664

Closed evansd closed 2 months ago

evansd commented 2 months ago

Various OpenSAFELY images (in particular, but not exclusively ehrql) write colourised/formatted output to stderr using ANSI escape codes. For example:

Screenshot from 2023-08-18 10-57-00

If we render this in a naive text viewer we get stuff like:

2024-09-10 11:35:21 [info     ] Compiling dataset definition from demo_dataset.py
2024-09-10 11:35:22 [info     ] Generating dummy dataset
2024-09-10 11:35:22 [info     ] Attempting to generate 10 matching patients (random seed: BwRV3spP, timeout: 60s)
2024-09-10 11:35:22 [info     ] Use `dataset.configure_dummy_data(population_size=N)` to change number of patients generated
2024-09-10 11:35:23 [info     ] Building dataset and writing results

It would be great if we could render the escape codes into appropriate HTML so it all looks nice. I very much hope there are non-awful libraries which would handle this for us, but I haven't actually looked yet.

evansd commented 2 months ago

As a very first pass, just stripping the escape codes out would be an improvement.

evansd commented 2 months ago

There's this library, which might be acceptable: https://github.com/pycontribs/ansi2html

I also see that nbconvert has a standalone module for doing this which we could vendor: https://github.com/jupyter/nbconvert/blob/e1599627fd1f9e06d983e3974eadb58d2a92c44c/nbconvert/filters/ansi.py

rebkwok commented 2 months ago

Closed by #673