Proposal to improve table rendering
ronisbr opened this issue · comments
Hi!
For reasons I cannot remember (maybe @bkamins can), the headers in DataFrames output are aligned to the left whereas some numeric columns are aligned to the right. This works pretty well in the terminal and in Jupyter. However, the tables in Quarto are post-process to fill the entire space, leading to a bad representation as follows:
![Captura de Tela 2023-11-01 às 11 30 12](https://private-user-images.githubusercontent.com/1068295/279703790-66f7d9c1-a599-4d33-a3d6-cbf660a67606.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwMjE1MzEsIm5iZiI6MTcyNTAyMTIzMSwicGF0aCI6Ii8xMDY4Mjk1LzI3OTcwMzc5MC02NmY3ZDljMS1hNTk5LTRkMzMtYTNkNi1jYmY2NjBhNjc2MDYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MzBUMTIzMzUxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2YyOTBjNTQxOWEyNTM5MGVlZWYwNzQ3MGZlMDZhMWNlODkzZDZkMTAwZTRhNDg4NmQwYWJkNTkzZGU4ZGRlOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.rRihIINoldYtY1qzWHHu36nyoaXC2E3WlVJ1IVdgisw)
Take a look how the same table is shown in Jupyter:
![Captura de Tela 2023-11-01 às 13 15 06](https://private-user-images.githubusercontent.com/1068295/279733475-5e5ca1b5-6455-4096-929c-89400dd83130.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwMjE1MzEsIm5iZiI6MTcyNTAyMTIzMSwicGF0aCI6Ii8xMDY4Mjk1LzI3OTczMzQ3NS01ZTVjYTFiNS02NDU1LTQwOTYtOTI5Yy04OTQwMGRkODMxMzAucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MzBUMTIzMzUxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWZjNzlkZTk1ODY2Njk5ZGQzMGRhY2ZkYzljMjMzYWM2ODQ5MmJkOTNhMDEzMjZiZmQ3MDdjNTRjNGRiY2ZhZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.yGnNF5dNY8xNcDQHr6sRPGAtyA_Knw6gc7UkUXlnMKI)
Hence, my proposal is to change how DataFrames are shown here in the book. My proposal is to do the following:
` ` `{julia}
#| output: false
using DataFrames
df = DataFrame(
NAME=["John", "Mary", "Paul", "Anne", "Kate"],
AGE=[34, 12, 23, 39, 28],
HEIGHT=[1.78, 1.56, 1.70, 1.80, 1.72],
GENDER=["male", "female", "male", "female", "female"]
` ` `
` ` `{julia}
#| echo: false
show(stdout, MIME("text/html"), df; header_alignment = :c, alignment = :c)
` ` `
Leading to the much nicer version:
![Captura de Tela 2023-11-01 às 13 52 37](https://private-user-images.githubusercontent.com/1068295/279743088-86e5b8c2-4330-4915-8334-e311f1e40d84.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjUwMjE1MzEsIm5iZiI6MTcyNTAyMTIzMSwicGF0aCI6Ii8xMDY4Mjk1LzI3OTc0MzA4OC04NmU1YjhjMi00MzMwLTQ5MTUtODMzNC1lMzExZjFlNDBkODQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgzMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MzBUMTIzMzUxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NWFiNDQ2YTMwNDRiMzgzNzViODg0ZTkzZTMzMjEyMzRkNTZhYmFjYjgwZWFhMzg4Mzk5MWVhM2RkMjdhMzU3ZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.0TRnGUE-0mjS532lDaxtB-OTo-_ZLRwvy3CXjZ6IiUc)
P.S.: I tried to override Base.show
in Quarto to reduce the work, but I couldn't.
Appreciate the fix @ronisbr ❤️ Could you please submit a PR with the proposed changes?
As we discussed elsewhere, it would be nice to get this fixed in Quarto itself or change the DataFrames.jl defaults to centered alignment.
There is a much easier way (but more hacky). Just add to the code section that loads the environment:
using DataFrames
function Base.show(io::IO, mime::MIME"text/html", df::AbstractDataFrame; kwargs...)
return DataFrames._show(io, mime, df; alignment = :c, header_alignment = :c, kwargs...)
end
function Base.show(io::IO, mime::MIME"text/html", dfrs::DataFrames.DataFrameRows; kwargs...)
DataFrames._verify_kwargs_for_html(; kwargs...)
df = DataFrames.parent(dfrs)
title = "$(nrow(df))×$(ncol(df)) DataFrameRows"
return DataFrames._show(
io,
mime,
df;
alignment = :c,
header_alignment = :c,
title = title,
kwargs...
)
end
function Base.show(io::IO, mime::MIME"text/html", dfcs::DataFrames.DataFrameColumns; kwargs...)
DataFrames._verify_kwargs_for_html(; kwargs...)
df = DataFrames.parent(dfcs)
title = "$(nrow(df))×$(ncol(df)) DataFrameColumns"
return DataFrames._show(
io,
mime,
df;
alignment = :c,
header_alignment = :c,
title = title,
kwargs...
)
end
function Base.show(io::IO, mime::MIME"text/html", dfr::DataFrameRow; kwargs...)
DataFrames._verify_kwargs_for_html(; kwargs...)
r, c = parentindices(dfr)
title = "DataFrameRow ($(length(dfr)) columns)"
return DataFrames._show(
io,
mime,
view(parent(dfr), [r], c);
alignment = :c,
header_alignment = :c,
rowid = r,
title = title,
kwargs...
)
end
This code overloads all the printing functions in DataFrames to HTML adding the center alignment. This approach should be the easiest one until I can implement themes support in PrettyTables.jl.
Could you please submit a PR with the proposed changes?
Sure! But what version do you prefer?
it would be nice to get this fixed in Quarto itself or change the DataFrames.jl defaults to centered alignment.
Quarto v1.4 will have an option to disable table processing globally. Maybe this will fix everything. I think everything will be also fixed if DataFrames change the header alignment to match the column alignment. I remember that we discussed this alignment and for some reason I cannot remember it was decided to always align the header to the left.
what version do you prefer?
The first one where we change the specific cells with DataFrame output. They only appear in chapter 01 while we introduce Tables.jl. All other tables in the book should be geotables, which are centered already.