@danpryjma has a good solution. Also, if you want to keep the new columns and make them editable, that can be updated in a callback like in this example:
import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_html_components as html
import pandas as pd
from random import randint, seed
app = dash.Dash(__name__)
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/solar.csv")
app.layout = html.Div(
[
html.H4("Copy and paste any data into this table"),
dash_table.DataTable(
id="copy_paste_table",
columns=[
{
"name": "Column {}".format(i),
"id": "column-{}".format(i),
"deletable": True,
"renamable": True,
}
for i in range(1, 10)
],
data=[
{"column-{}".format(i): (randint(0, 100)) for i in range(1, 10)}
for j in range(3)
],
editable=True,
row_deletable=True,
),
html.H4("Copy and paste updated solar data"),
html.Button("Edit New Columns", id="edit_col"),
html.Button("Add Row", id="add_row"),
dash_table.DataTable(
id="solar",
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("records"),
editable=True,
row_deletable=True,
),
]
)
@app.callback(
Output("solar", "data"),
Input("add_row", "n_clicks"),
State("solar", "data"),
State("solar", "columns"),
prevent_initial_call=True,
)
def add_row(_, rows, columns):
if rows is None:
return df.to_dict("records")
else:
rows.append({c["id"]: "" for c in columns})
return rows
@app.callback(
Output("solar", "columns"),
Input("edit_col", "n_clicks"),
State("solar", "columns"),
prevent_initial_call=True,
)
def edit_col(_, columns):
if columns is None:
return dash.no_update
else:
for c in columns:
if c["id"] not in df.columns:
c["deletable"] = True
c["renamable"] = True
return columns
if __name__ == "__main__":
app.run_server(debug=True)