I’m trying to create an application that logs each time the user interacts with the app and displays those results in a Data Table.
Currently, the app can only display one interaction at a time. I would like each result to get added as a new row to the table.
import pandas as pd
from dash import Dash, html, dcc, Input, Output, dash_table
import datetime
app = Dash(__name__)
user = "Rebecca"
app.layout = html.Div([
html.H1('Demoing User Interaction data capture'),
dcc.Dropdown(options=['One', 'Two', 'Three'], id='dropdown'),
html.Div(children = {}, id = 'row-data', style = {'display': 'none'}),
html.Div(children = {}, id = 'display-data')])
# Callback that creates the row data
@app.callback(
Output(component_id='row-data', component_property='children'),
Input(component_id='dropdown', component_property='value')
)
def capture_data(valChosen):
user = "Rebecca"
current_time = datetime.datetime.now()
if valChosen == None:
new_row = [user, current_time, f'{user} logged on']
else:
user = "Rebecca"
current_time = datetime.datetime.now()
valChosenStr = f'{user} chose ' +str(valChosen)
new_row = [user, str(current_time), valChosenStr]
return new_row
@app.callback(
Output(component_id='display-data', component_property='children'),
Input(component_id='row-data', component_property='children')
)
def display_data(newRow):
newRowDf = pd.DataFrame({'User': newRow[0], 'Time': newRow[1], 'Activity': newRow[2]}, index = [0])
dataTable = dash_table.DataTable(newRowDf.to_dict('records'),[{"name": i, "id": i} for i in newRowDf.columns])
return dataTable
if __name__ == '__main__':
app.run_server(debug=True)