@gshetty wrote:
Hi,
I’m new to dash. I want to fire update_output() function’s callback first and after a delay of few seconds,fire other callbacks. I’m not able to do it since all the callbacks load at the beginning only.
Please provide me help.Here is my code.
from dash.dependencies import Input, Output import dash_core_components as dcc import dash_html_components as html import dash_bootstrap_components as dbc import base64 import io import os import pandas as pd import dash import time #import numpy as np from app import app import chu import fra import tabs1 #from tabs1 import fraud, crosssell, churn , segmentation image_filename = 'C:\\Users\\gagan.shetty\\Multiple_Models\\data\\image_2020_04_01T11_49_41_982Z.png' encoded_image = base64.b64encode(open(image_filename, 'rb').read()) import dash_table as dt style = {'maxWidth': '960px', 'margin': 'auto'} #print(dcc.__version__) # 0.6.0 or above is required UPLOAD_DIRECTORY = "C:/Users/gagan.shetty/Multiple_Models/pro/app_uploaded_files" if not os.path.exists(UPLOAD_DIRECTORY): os.makedirs(UPLOAD_DIRECTORY) app.layout = html.Div( style={ 'verticalAlign':'middle', 'textAlign': 'center'} # ,children=[ dcc.Location(id='url', refresh=False), html.Br(), html.Div(id='page-content'), ]) index_page = html.Div([ dcc.Upload( id='upload-data', children=html.Div([ 'Drag and Drop or ', html.A('Select Files') ]), style={ 'width': '95%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px' }, ), html.Div(id="file-list"), dcc.Link([html.Button('Click here for next page')], href='/page-1'), html.Br(),html.Br(), html.Div(id='output-data-upload'), html.Br(), html.Br(), ]) page_1_layout = html.Div([ dcc.Link([html.Button('Click here for EDA dashboards')], href='/page-3'), html.Br(),html.Br(),html.Br(), dcc.Link([html.Button('Click here for model run')], href='/page-4'), html.Br(),html.Br(),html.Br(), dcc.Link([html.Button('Go back to home')], href='/'), html.Div(id='page-1-content'), html.Div(id='output-data'), ]) tabs_styles = { 'width': '1100px' } page_3_layout = html.Div([ dcc.Markdown('## Dashboards'), dcc.Tabs(id='tabs1', value='tab1-fraud', children=[ dcc.Tab(label='Fraud', value='tab1-fraud'), dcc.Tab(label='Segmentation', value='tab-segmentation'), dcc.Tab(label='Churn', value='tab1-churn'), dcc.Tab(label='CrossSell', value='tab1-crosssell'), ],style=tabs_styles), dcc.Link([html.Button('Go to home page')], href='/'), html.Br(),html.Br(), html.Div(id='tabs1-content'), ], style=style) page_4_layout= html.Div(([ dcc.Link([html.Button('churn')], href='/page-5'), html.Br(),html.Br(), dcc.Link([html.Button('Fraud')], href='/page-6'), html.Br(),html.Br(), dcc.Link([html.Button('Home Page')], href='/'), html.Br(),html.Br() ])) page_5_layout=html.Div([ dcc.Markdown('## Churn'), ]) def parse_contents(contents, filename, date): content_type, content_string = contents.split(',') decoded = base64.b64decode(content_string) try: if 'csv' in filename: # Assume that the user uploaded a CSV file df = pd.read_csv( io.StringIO(decoded.decode('utf-8'))) elif 'xls' in filename: # Assume that the user uploaded an excel file df = pd.read_excel(io.BytesIO(decoded)) except Exception as e: print(e) return html.Div([ 'There was an error processing this file.' ]) return filename,df def uploaded_files(): """List the files in the upload directory.""" files = [] for filename in os.listdir(UPLOAD_DIRECTORY): path = os.path.join(UPLOAD_DIRECTORY, filename) if os.path.isfile(path): files.append(filename) return files def save_file(name, content): """Decode and store a file uploaded with Plotly Dash.""" data = content.encode("utf8").split(b";base64,")[1] with open(os.path.join(UPLOAD_DIRECTORY, name), "wb") as fp: fp.write(base64.decodebytes(data)) @app.callback(Output('output-data-upload', 'children'), [Input('upload-data', 'contents'), Input('upload-data', 'filename'), Input('upload-data', 'last_modified')]) def update_output(contents, filename, date): if contents is not None: #parse_contents(contents, filename, date) filename, df = parse_contents(contents, filename, date) def chkd(col): if col.dtype== 'float64' or col.dtype == 'Int64': return 'Continuous' else: return 'Categorical' df1=df.describe(include='all') df1=df1.round(2) df_tab=pd.DataFrame(df1).stack().unstack(0) try: df_tab['cnt_null'] = df.apply(lambda col: col.isnull().sum()) df_tab['Null_%'] = df_tab.apply(lambda row: round((row['cnt_null']/(row['cnt_null']+row['count']))*100,2), axis=1) df_tab['Cnt_zeros'] = df.isin([0]).sum(axis=0) df_tab['Data_Type'] = df.dtypes df_tab['Variance']=df.var(axis=0) df_tab['mode'] = df.mode().iloc[0,:] df_tab['1%'] = df.quantile(0.01) df_tab['5%'] = df.quantile(0.05) df_tab['10%'] = df.quantile(0.1) df_tab['90%'] = df.quantile(0.9) df_tab['95%'] = df.quantile(0.95) df_tab['99%'] = df.quantile(0.99) df_tab['skew']=df.skew(axis=0) df_tab['kurtosis']=df.kurt(axis=0) df_tab['Variable_Type'] = df.apply(lambda col: 'Categorical' if col.dtype=='object' else chkd(col)) except TypeError: pass df_tab=df_tab.reset_index(level=0) df_tab.rename(columns={'50%':'median'},inplace=True) df_tab.rename(columns={'count':'cnt_non_null'},inplace=True) df_tab.rename(columns={'index':'Variable'},inplace=True) df_tab=df_tab[['Variable','Variable_Type','Data_Type','cnt_non_null','cnt_null','Null_%','Cnt_zeros','mean','std','mode','min','1%','5%','10%','25%','median','75%','90%','95%','99%','max','Variance','skew','kurtosis']] # df_tab = pd.DataFrame(df_tab) # df_tab=df_tab.reset_index(level=0) df_ta=df_tab.round(2) #cols=[{'name': i, 'id': i} for i in df_tab.columns] df_ta = df_ta.sort_values(by ='Variable_Type',ascending=False) print("wait for 3 sec") time.sleep(3) df_ta.to_csv('uni.csv') new_df = pd.read_csv('uni.csv') table = html.Div([ html.H5(filename), dt.DataTable( data=new_df.to_dict('records'), columns=[{'name': i, 'id': i} for i in df_ta.columns] ), ]) return table @app.callback(Output('page-content', 'children'), [Input('url', 'pathname')]) def display_page(pathname): if pathname == '/page-1': return page_1_layout # elif pathname == '/page-2': # return page_2_layout elif pathname =='/page-3': return page_3_layout elif pathname == '/page-4': return page_4_layout elif pathname == '/page-5': return chu.layout elif pathname == '/page-6': return fra.layout else: return index_page @app.callback(Output('tabs1-content', 'children'), [Input('tabs1', 'value')]) def render_content(tab): if tab == 'tab1-fraud': return tabs1.fraud.layout elif tab == 'tab-segmentation': return tabs1.segmentation.layout elif tab == 'tab1-churn': return tabs1.churn.layout elif tab == 'tab1-crosssell': return tabs1.crosssell.layout if __name__ == '__main__': app.run_server(debug=True,port=2222)
Posts: 1
Participants: 1