重新加载页面时不显示数据表组件中的已修改数据 [英] Modified data in data table component is not displayed on page reload
本文介绍了重新加载页面时不显示数据表组件中的已修改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
数据 solar.csv
: https://raw.githubusercontent.com/plotly/datasets/master/solar.csv
这是一个代码:
import dash
import dash_table
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
from dash.exceptions import PreventUpdate
df = pd.read_csv('D:/solar.csv')
app = dash.Dash(__name__)
app.layout = html.Div([
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("rows"),
editable=True
),
html.Button(id="save-button",n_clicks=0,children="Save"),
html.Div(id="output-1",children="Press button to save changes")
])
@app.callback(
Output("output-1","children"),
[Input("save-button","n_clicks")],
[State("table","data")]
)
def selected_data_to_csv(nclicks,table1):
if nclicks == 0:
raise PreventUpdate
else:
pd.DataFrame(table1).to_csv('D:/solar.csv',index=False)
return "Data Submitted"
if __name__ == '__main__':
app.run_server(debug=True)
当我在浏览器中编辑表中的数据时,按保存按钮后,修改后的数据将正确保存在 solar.csv
中.问题是,如果我刷新页面,则会显示旧数据(未修改).
When I am editing data in table from browser and after pressing the save button the modified data is saved correctly in solar.csv
. The problem is that, if I refresh page old data (non-modified) data is displayed.
我尝试了几种方法,例如在 selected_data_to_csv()
中使用全局变量,但没有任何运气.
I tried several ways like using global variables inside selected_data_to_csv()
but without any luck.
问题:如何在重新加载网页时修改上面的代码以显示修改后的数据?
Question: How to modify code above in order to show modified data when I reload webpage?
查看全文