How to auto reload the csv file

Hi,Team
I have used the pandas read one csv file, and used dropdown component based on one column of this file, then figure some plots.
But the csv file updated every hour by another program. How to auto reload the csv file by hour or when refresh the website?

Hello @Eddy,

This can be handled from bringing your layout and df call inside of a function. That way the new info is queried every time you refresh. :slight_smile:

I’m a new learner for dash.
Could you give one example or some code for learning?

Sure, here you are:

def serve_layout():
    df = pd.read_csv()
    return html.Div([
             dash_table.DataTable(df.to_dict('records'), columns=[{'name': i, 'id': i} for i in df.columns])])

app.layout = serve_layout

This would just give a basic table upon refreshing the app, updating from the csv file each time.

2 Likes

Hi, Jinnyzor
I tried to use your code, but it can’t work.

The csv file as below:

This is my code, could you help check.
Actually, I just need to use csv file, no need to use dash_table.

from dash import Dash, dcc, html, Input, Output,callback,dash_table#, State
import pandas as pd
import plotly.graph_objects as go

app = Dash(name,suppress_callback_exceptions=True)

df = pd.read_csv(‘/Users/guzhongqian/Downloads/TEST_DATA.csv’)

app.layout = html.Div([

html.Div([

    html.Div(["产品",
        dcc.Dropdown(
            df['MODEL'].unique(),
            'MODEL1',
            id='xaxis-column'       #下来菜单默认选择项
         #   ,multi=True
        ),
        dcc.RadioItems(
            ['Linear', 'Log'],
            'Linear',
            id='xaxis-type',
            inline=True
        )
    ], style={'width': '18%', 'display': 'inline-block'}),

    html.Div(["Process",  # Text show above the dropdown
         dcc.Dropdown(
             placeholder="请选择制程", 
                             
             id='yaxis-column',
             options=df.PROCESS.unique()
        ),
         dcc.RadioItems(
             ['Linear', 'Log'],
             'Linear',
             id='yaxis-type',
             inline=True
         )
    ], style={'width': '20%',  'display': 'inline-block'}),
    html.Div(["FAI",  # Text show above the dropdown
         dcc.Dropdown(
             
            # df['POINT_NUM'].unique(),
             
            placeholder="请选择FAI", 
            #  df['POINT_NUM'][df['PROCESS']==options].unique(),
            # 'D001_L1',
             id='yaxis-column2',
             
             clearable=False
           #  ,multi=True
        ),
         dcc.RadioItems(
             ['Linear', 'Log'],
             'Linear',
             id='yaxis-type2',
             inline=True
         )
    ], style={'width': '15%',  'display': 'inline-block'}),
    html.Br(),
    html.Div(["类型",  # Text show above the dropdown
         dcc.Dropdown(
             df['TYPENM'].unique(),
             id='yaxis-column1',

             multi=True
        ),
         html.Button("select all",id="select-all",n_clicks=0),
    ], style={'width': '70%',  'display': 'inline-block'}),
    
    
]),

dcc.Graph(id='graphicbox'),       

])

@app.callback(
Output(‘yaxis-column2’, ‘options’),
Input(‘yaxis-column’, ‘value’),
prevent_initial_call=True
)
def update_output(value):
return df[df.PROCESS == value].POINT_NUM.unique()

@app.callback(Output(“yaxis-column1”,“value”),Input(“select-all”,“n_clicks”))
def select_all(n_clicks):
return df.TYPENM.unique()

@callback(
Output(‘graphicbox’, ‘figure’),
Input(‘xaxis-column’, ‘value’),
Input(‘yaxis-column’, ‘value’),
Input(‘yaxis-column1’, ‘value’),
Input(‘yaxis-column2’, ‘value’),
Input(‘xaxis-type’, ‘value’)
,
Input(‘yaxis-type’, ‘value’)
)

def update_graph(xaxis_column_name,
yaxis_column_name,
yaxis_column_name1,
yaxis_column_name2
,
xaxis_type
,
yaxis_type
):
if yaxis_column_name2 is None:
fig2=go.Figure()
fig2.add_trace(go.Box(
x=(df[“TEST_SHIFT”]),
y=df[“SPCDATA”],
boxpoints=‘all’,
fillcolor=‘rgba(0,0,0,0)’,
pointpos=0,
))

    return fig2

else:
        s = pd.Series(xaxis_column_name)
        dff1 = df[df['MODEL'].isin(s)]
        s1=pd.Series(yaxis_column_name)
        dff2 = dff1[dff1['PROCESS'].isin(s1)]       #根据选择项生成新的数据
        s2=pd.Series(yaxis_column_name1)
        dff3 = dff2[dff2['TYPENM'].isin(s2)]       #根据选择项生成新的数据
        s3=pd.Series(yaxis_column_name2)
        dff = dff3[dff3['POINT_NUM'].isin(s3)]       #根据选择项生成新的数据

        fig= go.Figure()
    
        fig.add_trace(go.Box(
            x=(dff["TEST_SHIFT"]),
            y=dff["SPCDATA"],
            boxpoints='all',
            fillcolor='rgba(0,0,0,0)',
           # line=dict(color='black', width=0),
            pointpos=0,
            marker_size=3,
            ))


        return fig

if name == ‘main’:
app.run_server(debug=True)

Give this a try:

def serve_layout():
    df = pd.read_csv(‘ / Users / Downloads / TEST_DATA.csv')
    
    html.Div([
    
        html.Div([
    
            html.Div(["产品",
                      dcc.Dropdown(
                          df['MODEL'].unique(),
                          'MODEL1',
                          id='xaxis-column'  # 下来菜单默认选择项
                          #   ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='xaxis-type',
                          inline=True
                      )
                      ], style={'width': '18%', 'display': 'inline-block'}),
    
            html.Div(["Process",  # Text show above the dropdown
                      dcc.Dropdown(
                          placeholder="请选择制程",
    
                          # df['PROCESS'].unique(),                 
                          id='yaxis-column',
                          options=df.PROCESS.unique()
                          #   ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='yaxis-type',
                          inline=True
                      )
                      ], style={'width': '20%', 'display': 'inline-block'}),
            html.Div(["FAI",  # Text show above the dropdown
                      dcc.Dropdown(
    
                          # df['POINT_NUM'].unique(),
    
                          placeholder="请选择FAI",
                          #  df['POINT_NUM'][df['PROCESS']==options].unique(),
                          # 'D001_L1',
                          id='yaxis-column2',
    
                          clearable=False
                          #  ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='yaxis-type2',
                          inline=True
                      )
                      ], style={'width': '15%', 'display': 'inline-block'}),
            html.Br(),
            html.Div(["类型",  # Text show above the dropdown
                      dcc.Dropdown(
                          df['TYPENM'].unique(),
                          # '每日首件',
                          id='yaxis-column1',
                          # value=[]
    
                          # ,
                          multi=True
                      ),
                      html.Button("select all", id="select-all", n_clicks=0),
                      #     dcc.RadioItems(
                      #      ['Linear', 'Log'],
                      #      'Linear',id='yaxis-type1', inline=True   )
                      ], style={'width': '70%', 'display': 'inline-block'}),
    
            #   html.Div(id='output_container'),
    
        ]),
    
        dcc.Graph(id='graphicbox'),  # 输出图
    ])

app.layout = serve_layout

But the callback can’t load df.

How big is your csv file?

If it is not large, you can store it in a dcc.Store to be used in the callbacks, that is a way to avoid using a global variable.

sometimes the file will be around 500M.

Give this one a try:

df = pd.read_csv(‘ / Users / Downloads / TEST_DATA.csv')

def serve_layout():
    global df
    df = pd.read_csv(‘ / Users / Downloads / TEST_DATA.csv')

    html.Div([

        html.Div([

            html.Div(["产品",
                      dcc.Dropdown(
                          df['MODEL'].unique(),
                          'MODEL1',
                          id='xaxis-column'  # 下来菜单默认选择项
                          #   ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='xaxis-type',
                          inline=True
                      )
                      ], style={'width': '18%', 'display': 'inline-block'}),

            html.Div(["Process",  # Text show above the dropdown
                      dcc.Dropdown(
                          placeholder="请选择制程",

                          # df['PROCESS'].unique(),                 
                          id='yaxis-column',
                          options=df.PROCESS.unique()
                          #   ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='yaxis-type',
                          inline=True
                      )
                      ], style={'width': '20%', 'display': 'inline-block'}),
            html.Div(["FAI",  # Text show above the dropdown
                      dcc.Dropdown(

                          # df['POINT_NUM'].unique(),

                          placeholder="请选择FAI",
                          #  df['POINT_NUM'][df['PROCESS']==options].unique(),
                          # 'D001_L1',
                          id='yaxis-column2',

                          clearable=False
                          #  ,multi=True
                      ),
                      dcc.RadioItems(
                          ['Linear', 'Log'],
                          'Linear',
                          id='yaxis-type2',
                          inline=True
                      )
                      ], style={'width': '15%', 'display': 'inline-block'}),
            html.Br(),
            html.Div(["类型",  # Text show above the dropdown
                      dcc.Dropdown(
                          df['TYPENM'].unique(),
                          # '每日首件',
                          id='yaxis-column1',
                          # value=[]

                          # ,
                          multi=True
                      ),
                      html.Button("select all", id="select-all", n_clicks=0),
                      #     dcc.RadioItems(
                      #      ['Linear', 'Log'],
                      #      'Linear',id='yaxis-type1', inline=True   )
                      ], style={'width': '70%', 'display': 'inline-block'}),

            #   html.Div(id='output_container'),

        ]),

        dcc.Graph(id='graphicbox'),  # 输出图
    ])

    app.layout = serve_layout

Make sure it works with refreshing too.

Thank you Jinnyzor.
I used this code, the df was not alert. But there was no output graph, and also no any warning/error.

Can you give me back your code and use this button to wrap the code?



from dash import Dash, dcc, html, Input, Output,callback#,dash_table#, State
import pandas as pd
import plotly.graph_objects as go

app = Dash(__name__,suppress_callback_exceptions=True)



df = pd.read_csv('/Users/Downloads/TEST_DATA.csv')[spoiler]This text will be hidden[/spoiler]


#app.layout = html.Div([
#app.layout = serve_layout


def serve_layout():
    global df
    df = pd.read_csv('/Users/Downloads/TEST_DATA.csv')
    html.Div([

    html.Div([

        html.Div(["产品",
            dcc.Dropdown(
                df['MODEL'].unique(),
                'MODEL1',
                id='xaxis-column'       #下来菜单默认选择项
             #   ,multi=True
            ),
            dcc.RadioItems(
                ['Linear', 'Log'],
                'Linear',
                id='xaxis-type',
                inline=True
            )
        ], style={'width': '18%', 'display': 'inline-block'}),

        html.Div(["Process",  # Text show above the dropdown
             dcc.Dropdown(
                 placeholder="请选择制程", 
                                 
                 #df['PROCESS'].unique(),                 
                 id='yaxis-column',
                 options=df.PROCESS.unique()
              #   ,multi=True
            ),
             dcc.RadioItems(
                 ['Linear', 'Log'],
                 'Linear',
                 id='yaxis-type',
                 inline=True
             )
        ], style={'width': '20%',  'display': 'inline-block'}),
        html.Div(["FAI",  # Text show above the dropdown
             dcc.Dropdown(
                 
                # df['POINT_NUM'].unique(),
                 
                placeholder="请选择FAI", 
                #  df['POINT_NUM'][df['PROCESS']==options].unique(),
                # 'D001_L1',
                 id='yaxis-column2',
                 
                 clearable=False
               #  ,multi=True
            ),
             dcc.RadioItems(
                 ['Linear', 'Log'],
                 'Linear',
                 id='yaxis-type2',
                 inline=True
             )
        ], style={'width': '15%',  'display': 'inline-block'}),
        html.Br(),
        html.Div(["类型",  # Text show above the dropdown
             dcc.Dropdown(
                 df['TYPENM'].unique(),
                 #'每日首件',
                 id='yaxis-column1',
                # value=[]

                # ,
                 multi=True
            ),
             html.Button("select all",id="select-all",n_clicks=0),
        #     dcc.RadioItems(
           #      ['Linear', 'Log'],
           #      'Linear',id='yaxis-type1', inline=True   )
        ], style={'width': '70%',  'display': 'inline-block'}),
        
     #   html.Div(id='output_container'),

        
    ]),

    dcc.Graph(id='graphicbox'),       #输出图
    
])

app.layout = serve_layout



@app.callback(
    Output('yaxis-column2', 'options'),
    Input('yaxis-column', 'value'),
    prevent_initial_call=True
)
def update_output(value):
    return df[df.PROCESS == value].POINT_NUM.unique()

@app.callback(Output("yaxis-column1","value"),Input("select-all","n_clicks"))
def select_all(n_clicks):
    return  df.TYPENM.unique()



@callback(
  #  Output('page-1-display-value', 'children'),        #定义回调函数内容
    Output('graphicbox', 'figure'),
    Input('xaxis-column', 'value'),
    Input('yaxis-column', 'value'),
    Input('yaxis-column1', 'value'),
    Input('yaxis-column2', 'value'),
    Input('xaxis-type', 'value')
    ,
    Input('yaxis-type', 'value')
    )

def update_graph(xaxis_column_name, 
                 yaxis_column_name,
                 yaxis_column_name1,
                 yaxis_column_name2
                 ,
                 xaxis_type
                 , 
                 yaxis_type
                 ):
    if yaxis_column_name2 is None:
        fig2=go.Figure()
        fig2.add_trace(go.Box(
            x=(df["TEST_SHIFT"]),
            y=df["SPCDATA"],
            boxpoints='all',
            fillcolor='rgba(0,0,0,0)',
           # line=dict(color='black', width=0),
            pointpos=0,
            ))
       
        return fig2
    
    else:


            s = pd.Series(xaxis_column_name)
            dff1 = df[df['MODEL'].isin(s)]
            s1=pd.Series(yaxis_column_name)
            dff2 = dff1[dff1['PROCESS'].isin(s1)]       #根据选择项生成新的数据
            s2=pd.Series(yaxis_column_name1)
            dff3 = dff2[dff2['TYPENM'].isin(s2)]        #根据选择项生成新的数据
            s3=pd.Series(yaxis_column_name2)
            dff = dff3[dff3['POINT_NUM'].isin(s3)]       #根据选择项生成新的数据
   
            fig= go.Figure()
        
          #  a_list=dff['TYPENM'].unique()  #将X轴设置成一个系列,为for循环做准备
            fig.add_trace(go.Box(
                x=(dff["TEST_SHIFT"]),
                y=dff["SPCDATA"],
                boxpoints='all',
                fillcolor='rgba(0,0,0,0)',
               # line=dict(color='black', width=0),
                pointpos=0,
                marker_size=3,
                ))


            return fig
if __name__ == '__main__':
    app.run_server(debug=True)
  #  app.run_server(port=8080, host='10.45.141.1',debug=True)