It will be cripple by various consoles constrains on various platforms. I’m more interested by decent printing in various notebooks around like in Python.
You can do this using PrettyTables with the following code:
using Colors
using ColorSchemes
using DataFrames
using PrettyTables
# Function that defines the decoration in each cell.
function colormap_decoration(h, data, i, j)
max = maximum(eachcol(df)) |> maximum
min = minimum(eachcol(df)) |> minimum
# Get the color of the current cell.
color = get(
colorschemes[:coolwarm],
data[i, j],
(min, max)
)
# Convert to hexadecimal.
hex_color = color |> hex
# Add to the decoration.
return HTMLDecoration(background = "#" * hex_color)
end
# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)
# Define the highlighter that will add the color to each cell.
hl = HTMLHighlighter(
(data, i, j) -> true,
colormap_decoration
)
# Print the table.
pretty_table(df, highlighters = (hl,), backend = :html)
I used the master branch of PrettyTables.jl, but it should work in the released version. Notice that PrettyTables is passing thought a huge rewrite and next version should be some breaking changes.
Side note: this code is not optimized (max and min values are being computed when analyzing each cell), but it works
using Colors
using ColorSchemes
using DataFrames
using PrettyTables
function print_df(df)
max = maximum(eachcol(df)) |> maximum
min = minimum(eachcol(df)) |> minimum
# Function that defines the decoration in each cell.
function colormap_decoration(h, data, i, j)
# Get the color of the current cell.
color = get(
colorschemes[:coolwarm],
data[i, j],
(min, max)
)
# Convert to hexadecimal.
hex_color = color |> hex
# Add to the decoration.
return HTMLDecoration(background = "#" * hex_color)
end
# Define the highlighter that will add the color to each cell.
hl = HTMLHighlighter(
(data, i, j) -> true,
colormap_decoration
)
# Print the table.
pretty_table(df, highlighters = (hl,), backend = :html)
end
# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)
print_df(df)
The second is to define your own highlighter structure:
using Colors
using ColorSchemes
using DataFrames
using PrettyTables
struct MyHighlighter{T}
f::Function
fd::Function
min::T
max::T
end
# Function that defines the decoration in each cell.
function colormap_decoration(h, data, i, j)
# Get the color of the current cell.
color = get(
colorschemes[:coolwarm],
data[i, j],
(h.min, h.max)
)
# Convert to hexadecimal.
hex_color = color |> hex
# Add to the decoration.
return HTMLDecoration(background = "#" * hex_color)
end
function print_df(df)
max = maximum(eachcol(df)) |> maximum
min = minimum(eachcol(df)) |> minimum
# Define the highlighter that will add the color to each cell.
hl = MyHighlighter(
(data, i, j) -> true,
colormap_decoration,
min,
max
)
# Print the table.
pretty_table(df, highlighters = (hl,), backend = :html)
end
# Create a ramdom DataFrames.
df = DataFrame(rand(20, 5), :auto)
print_df(df)
This code looks OK in console but not in VS Code. I believe i disturb you with thinks that are not from the PrettyTable library.
No problem! Please, feel free to contact me anytime
It seems to me it is a problem of neither PrettyTables nor Crayons. I believe it might be something related to the console in VSCode. Can you execute the following code in Julia inside the VSCode console please:
julia> println("\e[38;2;255;255;0;48;2;0;0;255mThis must have blue background\e[0m")
julia> println("\e[33;44mThis must have blue background\e[0m")