✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Modified data in data table component is not displayed on page reload

Data solar.csv: https://raw.githubusercontent.com/plotly/datasets/master/solar.csv

Here is a code:

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)

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.

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?



Original code from: https://stackoverflow.com/questions/57649199/is-it-possible-to-export-dash-datatable-to-a-specific-location-on-disk-or-direct

Hi @vasili111
Welcome to the Dash community.

Dash apps are built in a way that allows hundreds of people to use them at the same time. That is why data should not change in the app globally if the user updates something. For example, if I change your datatable global variables, all the other users would see my change with the new data. And if I put in the wrong data which broke the graphs on the apps, all other users would see a broken app.

That is why, by default, you shouldn’t allow users to change global data. Here you can read more about it.

If you really want changes to be saved for each user, you should probably connect your app to a database like postgresql where changed data can be saved without changing the variables inside the app. Or if you insist, you can read more about going global here.

@adamschroeder Thank you.

Dash apps are built in a way that allows hundreds of people to use them at the same time. That is why data should not change in the app globally if the user updates something.

This is what I need to archive. I need to change global data for all users (not per user) when one of the users presses save button. How can I do that?