在页面重新加载时重置破折号数据表
dash DataTable reset on page reload
以下代码从 CSV 文件中读取一个简单模型(3 列 50 行),以便在我的(更大的)dash 应用程序的 table 中进行编辑。编辑单元格会按预期将整个 table 写回文件。但是,重新加载页面会显示 table,因为它最初是从文件加载的,因此会丢失所有编辑。关于如何在页面重新加载之间保持编辑的任何线索?
df_topic_list=pd.read_csv(model_file)
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([
dash_table.DataTable(df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],editable=True
),
])
])
@app.callback(Output('memory-output', 'data'),
Input('memory-table', 'data'))
def on_data_set_table(data):
pd.DataFrame(data).to_csv(model_file,index=False)
return data
app.run_server(port=8052)
当您刷新页面时,它不会再次 运行 所有代码,但它只会再次发送 app.layout
它只生成一次并且包含来自文件的原始数据。当您在 table 中更新单元格中的数据时,它只会更新浏览器中的值(使用 JavaScript),而不是代码 app.layout
.
中的值
但是它有选项可以在浏览器内存中保留值,并且它应该在重新加载后使用这些值。
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([
dash_table.DataTable(
df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],
editable=True,
persistence=True, # <---
persisted_props=["data"], # <---
)
])
])
它对我有用,但似乎有些人对此有疑问。
查看问题:Dash table edited data not persisting · Issue #684 · plotly/dash-table
但我找到了其他方法来保存它。
我将 table 分配给分隔变量 - 即。 table
- 在回调中我将 table.data
替换为 table.
from dash import Dash, Input, Output, callback
from dash import dcc, html, dash_table
import pandas as pd
model_file = 'data.csv'
df_topic_list = pd.read_csv(model_file)
app = Dash(__name__)
table = dash_table.DataTable(
df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],
editable=True,
)
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([table])
])
@app.callback(
Output('memory-output', 'data'),
Input('memory-table', 'data')
)
def on_data_set_table(data):
pd.DataFrame(data).to_csv(model_file, index=False)
table.data = data # <--- replace data
return data
app.run_server(port=8052)
以下代码从 CSV 文件中读取一个简单模型(3 列 50 行),以便在我的(更大的)dash 应用程序的 table 中进行编辑。编辑单元格会按预期将整个 table 写回文件。但是,重新加载页面会显示 table,因为它最初是从文件加载的,因此会丢失所有编辑。关于如何在页面重新加载之间保持编辑的任何线索?
df_topic_list=pd.read_csv(model_file)
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([
dash_table.DataTable(df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],editable=True
),
])
])
@app.callback(Output('memory-output', 'data'),
Input('memory-table', 'data'))
def on_data_set_table(data):
pd.DataFrame(data).to_csv(model_file,index=False)
return data
app.run_server(port=8052)
当您刷新页面时,它不会再次 运行 所有代码,但它只会再次发送 app.layout
它只生成一次并且包含来自文件的原始数据。当您在 table 中更新单元格中的数据时,它只会更新浏览器中的值(使用 JavaScript),而不是代码 app.layout
.
但是它有选项可以在浏览器内存中保留值,并且它应该在重新加载后使用这些值。
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([
dash_table.DataTable(
df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],
editable=True,
persistence=True, # <---
persisted_props=["data"], # <---
)
])
])
它对我有用,但似乎有些人对此有疑问。
查看问题:Dash table edited data not persisting · Issue #684 · plotly/dash-table
但我找到了其他方法来保存它。
我将 table 分配给分隔变量 - 即。 table
- 在回调中我将 table.data
替换为 table.
from dash import Dash, Input, Output, callback
from dash import dcc, html, dash_table
import pandas as pd
model_file = 'data.csv'
df_topic_list = pd.read_csv(model_file)
app = Dash(__name__)
table = dash_table.DataTable(
df_topic_list.to_dict('records'),
id='memory-table',
columns=[{"name": i, "id": i} for i in df_topic_list.columns],
editable=True,
)
app.layout = html.Div([
dcc.Store(id='memory-output'),
html.Div([table])
])
@app.callback(
Output('memory-output', 'data'),
Input('memory-table', 'data')
)
def on_data_set_table(data):
pd.DataFrame(data).to_csv(model_file, index=False)
table.data = data # <--- replace data
return data
app.run_server(port=8052)