Hello to all,
I am making a Dashboard on python using Dash. Now that my Dashboard is created, I would like to work with the interaction. To do this, I would like to use the user’s inputs by retrieving them in the form of data frames when the user click on the button.
For example, when the user fills in a table, I would like to retrieve this data in a DataFrame to reuse it.
Does anyone have an idea of how to do this?
I’m posting my code and interface so you can see how I coded it.
Thank you for your help!
hi @Oceane2807
Welcome to the Dash community.
It’s not completely clear what you mean when you say: "I would like to use the user’s inputs by retrieving them in the form of data frames ". What do you mean, you would like to use it? How?
In the code below, you will see how the save data button will take the datatable and convert the data back to a dataframe. You can save it if you want by exporting it as a csv or saving it on the client’s browser with dcc.Store. But I’m not 100% sure what you’re trying to do.
from dash import Dash, html, Output, Input, dash_table, callback
import pandas as pd
from collections import OrderedDict
app = Dash(__name__)
data = OrderedDict(
[
("Date", ["2015-01-01", "2015-10-24", "2016-05-10", "2017-01-10", "2018-05-10", "2018-08-15"]),
("Region", ["Montreal", "Toronto", "New York City", "Miami", "San Francisco", "London"]),
("Temperature", [1, -20, 3.512, 4, 10423, -441.2]),
("Humidity", [10, 20, 30, 40, 50, 60]),
("Pressure", [2, 10924, 3912, -10, 3591.2, 15]),
]
)
df = pd.DataFrame(
OrderedDict([(name, col_data * 10) for (name, col_data) in data.items()])
)
app.layout = html.Div([
html.P(id='save-button-hidden', style={'display':'none'}),
html.Button("Save Data", id='btn'),
dash_table.DataTable(
id='my-table',
data=df.to_dict('records'),
columns=[{'id': c, 'name': c} for c in df.columns],
page_size=10
),
])
@callback(
Output('save-button-hidden', 'children'),
Input('btn','n_clicks'),
Input('my-table', 'data'),
prevent_initial_call=True
)
def save_data(n, data):
df = pd.DataFrame(data)
print(df.head())
return ""
if __name__ == '__main__':
app.run_server(debug=True)
Thank you very much, I will try your method.
I will try to explain more clearly.
I use a dataFrame to create the DataTable. One column of this DataTable is editable. I would like that when the user changes the data, it changes in my dataframe in memory.
I already tried with dcc.Store with your videos but it didn’t work …
Thank you very much for your answer.
If I’m understanding your goal correctly and you would like the data that the user changes to be saved, you would probably need to connect the datatable to an external database and save the changes to that database when user edits the datatable.
Do you know how can I connect the datatable to an external database ?