Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Updating a variable used in DataTable's style_data_conditional

$
0
0

@Shedy wrote:

Hi everyone,

I’m facing a problem where I can’t update a variable I use for filter_query in style_data_conditional in a data table. In my example code I display 6 rows with IDs and dates. What I want is for dates that:

  • equal today’s date to be colored blue,
  • dates greater than today to be green ,
  • dates less than today to be red.

I managed to do that just fine, but when I leave my server overnight and check the next day it doesn’t update properly. The variable “dateToday” changes like it should when I print it, but the change is not reflected in the table (the colors stay the same).

Here’s my example code:

queryData = “SELECT [ID],convert(varchar,[Date inserted],23) AS Date_inserted FROM myTable ORDER BY Date_inserted ASC”
df_data = pd.read_sql(queryData, sql_conn)

query_dateToday = “SELECT convert(varchar, getdate(), 23) AS Date_today FROM myTable”
df_dateToday = pd.read_sql(query_dateToday, sql_conn)
dateToday = str(df_dateToday[‘Date_today’][0])

app.layout = html.Div(id=‘outer’,
children=[
html.Div(id=‘dateToday’, children=dateToday, style={‘display’: ‘none’}),

    dash_table.DataTable(
        id='table',
        columns = [{'name': i, 'id': i} for i in df_data.columns],
        data = df_data.to_dict('records'),
        style_data_conditional = [
            {
                'if' {
                    'filter_query': '{Date_inserted} eq '+dateToday+''
                },
                'color': 'blue'
            },
            {
                'if' {
                    'filter_query': '{Date_inserted} <  '+dateToday+''
                },
                'color': 'red'
            },
            {
                'if' {
                    'filter_query': '{Date_inserted} >  '+dateToday+''
                },
                'color': 'green'
            }
        ],
        style_table={'width': '15%', 'fontWeight': 'bold'},
    ),

    dcc.Interval(
        id='interval-time',
        interval=1000,
        n_intervals=0
    ),
],

)

@app.callback(Output(‘table’,‘data’),
[Input(‘interval-time’,‘n_intervals’)])
def update_table(n)
df_dateToday = pd.read_sql(query_dateToday, sql_conn)
dateToday = str(df_dateToday[‘Date_today’][0])
df_data = pd.read_sql(queryData, sql_conn)
data = df_data.to_dict(‘records’)

return data

This is how it looks if today is 20 Dec: today

This is how I want it to look the next day (21 Dec starting at 00:01): tomorrow

A workaround I found is to just return the whole DataTable in the callback again but that’s not going to make sense when I have 30 tables like that and I create them with a function outside the callback. I’ve also heard something about “derived_filter_query_structure” when looking for a solution but couldn’t figure out how to use it and if it is indeed related to my problem. How do I make my table to use the updated “dateToday” variable instead of the initial one when the server was started?

Any kind of help would be appreciated!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles