简体   繁体   English

使用新数据框更新 Python Dash 应用程序

[英]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.我是破折号的新手,并试图制作一个基本表格以显示在 IP 上供每个人查看。 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.为此,我使用任务调度程序每 30 分钟运行一次代码,杀死旧实例。 This way when the 'data.csv' is updated, a new dataframe will be displayed in the table.这样,当“data.csv”更新时,表中将显示一个新的数据框。

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.我的问题是,尽管重新启动了实例并更改了 csv,但只会显示原始 csv 数据。 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?我怎样才能让同一个应用程序更新新的 csv 信息?

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):此代码演示了这一点(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' cell_id - 列名,用于 choropleth_mapbox 'locations'
color_id - name of column, which was used in choropleth_mapbox 'color' color_id - 列名,在 choropleth_mapbox 'color' 中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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