简体   繁体   中英

Python Dash App updating with new dataframe

I am new to dash and trying to make a basic table to display on an IP for everyone to look at. This prevents the need for emails or placing data anywhere specific. I use the following code to create a VERY simple dash table pulled from the documentation, but I would like to update the dashboard on a regular basis. I do this by using task scheduler to run the code every 30 minutes, killing the old instance. This way when the 'data.csv' is updated, a new dataframe will be displayed in the table.

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

df = pd.read_csv('data.csv')


def generate_table(dataframe, max_rows=30):
    return html.Table(
        # Header
        [html.Tr([html.Th(col) for col in dataframe.columns])] +

        # Body
        [html.Tr([
            html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
        ]) for i in range(min(len(dataframe), max_rows))]
    )


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H4(children='Title'),
    generate_table(df, max_rows=len(df))
])


if __name__ == '__main__':
    ADDRESS='100.100.100.100'  #ipv4 address for computer code is run on
    PORT=int(1000)
    app.run_server(debug=True, host=ADDRESS, port=PORT)

My issue is that despite restarting the instance and changing the csv, only the original csv data will be shown. I can only fix it by changing the port and starting a new app, which isn't an option for what I want. How can I get the same app to update with new csv information?

I think you don't need to kill the "old" app instance and run a new one each every few minutes . You can just start your app once and set an "update interval" to desired time (in milliseconds) so that the new data is being downloaded by the app in the callback function while it keeps running.

This code demonstrates this (Dash v1.6.0):

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd

app = dash.Dash(__name__)

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app.layout = html.Div([
      html.H4('Dashboard'),
      dcc.Interval('graph-update', interval = 2000, n_intervals = 0),
      dash_table.DataTable(
          id = 'table',
          data = df.to_dict('records'),
          columns=[{"name": i, "id": i} for i in df.columns])])

@app.callback(
        dash.dependencies.Output('table','data'),
        [dash.dependencies.Input('graph-update', 'n_intervals')])
def updateTable(n):
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
    return df.to_dict('records')

if __name__ == '__main__':
     app.run_server(debug=True, port=10451)

Update data without create figure again. This reduces rendering delays.

@app.callback(
    Output(component_id='graph_map', component_property='figure'),
    Input(component_id='scale_slider', component_property='value'),
    State(component_id='graph_map', component_property='figure')
)
def update_output(set_scale, map_fig):
    df = df_original[df_original['scale'] == set_scale]
    
    map_fig['data'][0]['locations'] = df['cell_id'].tolist()
    map_fig['data'][0]['z'] = df['color_id'].tolist()

    return map_fig

cell_id - name of column, which was used in choropleth_mapbox 'locations'
color_id - name of column, which was used in choropleth_mapbox 'color'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM