How to connect date picker for json data,

I am able to add functionality for the date picker and json data. But when i select the data for a particular date it doesnot updated the same .``
like when i select the data for 26 and 27 its doesnot show data for only 2 days.
I think the data is just get displayed and not working along with date picker.
I would need to keep the data in the json format and not convert it to dataframes.
can someone plz suggest any changes to my code,


        a=i.to_dict()

        ###### DATE
        a['date']=a["@timestamp"][0:10]
        a['date'] = datetime.datetime.strptime(a['date'], '%Y-

        ###### TIME
        time=a["@timestamp"][12:16]
        time = datetime.datetime.strptime(time, '%H:%M')
        a['times']=time.time()

        l.append(a)


app.layout = html.Div([

    dcc.DatePickerRange(
    id='graphview',
    start_date=date(2022, 11, 26),
    end_date=date(2022, 12, 30),
    show_outside_days=True,
    day_size=32,
    display_format='YYYY-MM-DD', 
    ),

    html.Br(),

    dash_table.DataTable(
        id = 'table',
        data=l,
        page_size=25,
    ),

])

# @app.callback(
Output('table', 'data'),
[Input('graphview', 'start_date'),
 Input('graphview', 'end_date')]


def update_columns(start_date, end_date):
    start_date=datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date=datetime.datetime.strptime(end_date, '%Y-%m-%d')
    d=[]
    for i in range(len(l)):
        a=(l[i].loc(l[i]['date']>start_date and l[i]['date']<end_date))
        #d.append(l[i])
        return a

`**> Blockquote**`

if __name__ == "__main__":
    app.run_server(port=8050, debug = True)
```***`**strong text**`***

Hello @ArunGowda,

This looks strange:

First thing I notice is you are returning a in the below function during a loop, this will automatically end the function and should only return one row:

def update_columns(start_date, end_date):
    start_date=datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date=datetime.datetime.strptime(end_date, '%Y-%m-%d')
    d=[]
    for i in range(len(l)):
        a=(l[i].loc(l[i]['date']>start_date and l[i]['date']<end_date))
        #d.append(l[i])
        return a

Why exactly do you want to stay in json format, you can convert to a dataframe and back into json. It looks like you are trying to filter it like a dataframe already.

This is how you would apply a filter to a dictionary:

I have appended all the dictionary into a list and then processing it under the dash_table. The reason for me not to convert to dataframe as i have 1ml records, converting them to dataframe would consume more cpu time and would take more time to load. The data is extracted from elasticsearch which is in json format hence having a json format is mush faster (i felt).
kindly plz help me how to connect datepicker and json data together.


app.layout = html.Div([

    dcc.DatePickerRange(
    id='graphview',
    start_date=datetime.datetime(2022,11,26),
    end_date=datetime.datetime(2022,11,27),
    show_outside_days=True,
    day_size=32,
    display_format='YYYY-MM-DD', 
    ),

    html.Br(),

    dash_table.DataTable(
        id = 'table',
        data=l,
        columns=[{'id': c, 'name': c} for c in a],
        page_size=25)
])



@app.callback(
Output('table', 'data'),
[Input('graphview', 'start_date'),
 Input('graphview', 'end_date')]
)




def update_columns(start_date, end_date):
    for i in range(len(l)):
        if(l[i]['date']==start_date or l[i]['date']==end_date):
            data=(l[i])
            return data
1 Like

Alright, now it makes more sense. :slight_smile:

Give this a shot:

def update_columns(start_date, end_date):
    start_date=datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date=datetime.datetime.strptime(end_date, '%Y-%m-%d')
    d=[x for x in l if x['date'] >= start_date and x['date'] <= end_date]
    return d

Operators may be wrong. XD