Conditionally formatting values in Dash data table (Julia)

Viewed 388

I've built a table in Dash using Julia:

df = DataFrame(Day=["Monday","Tuesday","Wednesday"],
               Object=["Egg","Cat","Phone"],
               Letters=["A","B","C"],
               Food=["Milk","Egg","Cheese"])

app = dash()

app.layout = html_div() do
    dash_datatable(id="data_table",
                   columns = tuple([(name=x,id=x) for x in names(df)])[1],
                   data = df_to_datatable(df),
                   style_cell = (textAlign="center", fontSize=16,
                                 backgroundColor="rgb(50,50,50)",
                                 color="white"),
                   style_header = (backgroundColor="rgb(30, 30, 30)",),
                   style_table = (textAlign="center", minwidth="35%",
                                  width="35%",maxwidth="35%",
                                  marginLeft="auto",marginRight="auto"),
                   #style_cell_conditional = (if(filter_query="Egg"), backgroundColor="#3D9970"),
                  ),
    html_div(id="output_div")
end

Resulting in this table:

enter image description here

I'm trying to conditionally format cells that contain "Egg", but I'm having trouble. I've tried following other examples and the documentation for DashTable, converting the Python syntax to Julia. I've also tried addressing errors produced by my attempts (ex. "LoadError: syntax: unexpected ',' ") with no success. Would appreciate any help!

Edit: I've also been consulting the conditional formatting documentation, but the syntax is Python, which I'm still having trouble converting to Julia.

1 Answers

We can't use filter_query with style_cell_conditional.

We can use filter_query with style_data_conditional like this:

style_data_conditional = [
    Dict(
        "if" =>
            Dict("column_id" => "$(column)", "filter_query" => "{$(column)} = 'Egg'"),
        "backgroundColor" => "blue",
    ) for column in names(df)
]

Result:

enter image description here


The above does an exact match for 'Egg'. If you want to match all words that contain "Egg" you can use contains instead of =.

Related