xKhine
1
Hello, I am very new to Dash and have been trying to figure this out for a while.
Right now, I have a callback that outputs a datatable based on user input from 3 dropdowns.
I want to be able to click on a cell in the datatable and have a sentence that prints out the value of the call. I have tried the following code:
However, it only prints out the row and column id of the cell instead of the actual value of the cell.
How can I have write a callback to print the value of a cell from a datable that is the output from another callback?
Would really appreciate any help, thank you!
akroma
2
Hey @xKhine ,
Welcome to the community.
Instead of screenshots adding a minimal reproducible example to your question will help community to answer your question.
You can access cell values like below:
from dash import Dash, Input, Output, callback, dash_table
import pandas as pd
import dash_bootstrap_components as dbc
df = pd.read_csv('https://git.io/Juf1t')
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([
dbc.Label('Click a cell in the table:'),
dash_table.DataTable(df.to_dict('records'),[{"name": i, "id": i} for i in df.columns], id='tbl'),
dbc.Alert(id='tbl_out'),
])
@callback(Output('tbl_out', 'children'), Input('tbl', 'active_cell'))
def update_graphs(active_cell):
data_row= active_cell['row']
data_col_id = active_cell['column_id']
cell_value = df.loc[data_row, data_col_id]
print(data_row)
print(data_col_id)
print(cell_value)
return str(cell_value) if active_cell else "Click the table"
if __name__ == "__main__":
app.run_server(debug=True)
https://dash.plotly.com/datatable
https://stackoverflow.com/questions/70931002/pandas-get-cell-value-by-row-index-and-column-name
Have a nice day.