Dash data table callbacks and drop down

How to return dash data table and whole properties from callback using drop down list. I need to modify data and return it transposed and add style conditions based on filter of drop down list, i faced that error no data

import pandas as pd
import numpy as np
import seaborn as sns
import warnings
import matplotlib.pyplot as plt
#from prophet import Prophet
import dash  # you need Dash version 1.15.0 or higher
import dash_table
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output,State
import dash_auth
from dash.dash_table import DataTable, FormatTemplate
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
df=pd.read_excel("D:\\Early alarm system\\quarters and annual.xlsx",sheet_name='Sheet1')
#df
df=df.sort_values(by=['Date'],ascending=True)
df.replace(to_replace='-',value=np.nan,inplace=True)
#df=df.drop('Year',axis=1).round(decimals=2)
#df=df.round(decimals=2)
df.head(5)
df['quarter'] = pd.PeriodIndex(df.Date, freq='Q')
df['Year'] = pd.PeriodIndex(df.Date, freq='Y')
df
periodicity=df.periodicity.unique()
app = dash.Dash(__name__)

app.layout = html.Div([

html.Div([ html.Label('Periodicity'),dcc.Dropdown(
            id="filter_dropdown",
            options=[{'label':i,'value':i} for i in df.periodicity.unique()],
            placeholder="Select periodicity",
            #multi=True,
            value=periodicity[0]
)]),
               
                
html.Div(id="final_table")


])


@app.callback(Output("final_table","table"),
              [Input("filter_dropdown","value")])

def update(Select_periodicity):
    df_filtered = df[df['periodicity']==Select_periodicity].dropna(how='all',axis=1)
    if Select_periodicity == 'Annual':
        df_Transpose=df_filtered.drop(['Date','quarter','periodicity'],axis=1)
        df_Transpose=df_Transpose_annual.set_index('Year').T
        df_Transpose.reset_index(inplace=True)
        df_Transpose.rename(columns={'index':'Indicators'},inplace=True)
    elif Select_periodicity == 'Quarter':
        df_Transpose=df_filtered.drop(['Date','Year','periodicity'],axis=1)
        df_Transpose=df_Transpose_annual.set_index('quarter').T
        df_Transpose.reset_index(inplace=True)
        df_Transpose.rename(columns={'index':'Indicators'},inplace=True)
    return [ dash_table.DataTable(
                id='table',
                columns=[{'name':i,'id': i} for i in df_Transpose.columns],
                data= df_Transpose.to_dict("records"))]
            
if __name__ == "__main__":
    app.run_server(debug=False)

Could you try to change your Ouput from Output("final_table","table") to Output("final_table","children")?

1 Like