df = pd.DataFrame({
'shop': ['Bakersfield', 'Berkely', 'Big Bear Lake'],
'sales': [3, 1, 5],
'goal': [10, 1, 4],
'address': [
'3000 Mall View Road, Suite 1107 Bakersfield, CA 93306',
'2130 Center Street, Suite 102 Berkeley, CA 94704',
'652 Pine Knot Avenue Big Bear Lake, CA 92315'
]
})
app.layout = html.Div([
dash_table.DataTable(
id='test-table',
data=df.to_dict('records'),
columns=[{'id': c, 'name': c} for c in ['shop', 'sales', 'goal']],
tooltip_data=[{
'shop': {'value': row['address'], 'type': 'markdown'},
'sales': {
'value': 'Sales were **{} {}** than the goal'.format(
str(abs(row['goal'] - row['sales'])),
'less' if row['goal'] > row['sales'] else 'more'
),
'type': 'markdown'
},
'goal': 'Goal was {}'.format(
'not achieved' if row['goal'] > row['sales'] else 'achieved'
),
} for row in df.to_dict('records')],
tooltip_delay=0,
tooltip_duration=None
), html.Button("Test", id='test')]
)
@app.callback(
Output('test-table', 'tooltip_data'),
Input('test', "n_clicks"),
State('test-table', 'tooltip_data')
)
def change_tooltips(n_clicks, tooltip_data):
if n_clicks:
new_tooltip_data = []
for tip in tooltip_data:
tip['shop']['value'] = tip['shop']['value'].replace(",", "\n") # works with \n\n instead of \n
new_tooltip_data.append(tip)
else:
new_tooltip_data = tooltip_data
print(new_tooltip_data)
return new_tooltip_data
So interestingly it seems to only work when I do \n\n
i.e. \n
alone does nothing - and I only want a single carriage return. Additionally, it seems like type
must be markdown
. I can also see that the carriage returns are correctly injected as well:
>>> [{'shop': {'value': '3000 Mall View Road\n Suite 1107 Bakersfield\n CA 93306', 'type': 'markdown'}, 'sales': {'value': 'Sales were **7 less** than the goal', 'type': 'markdown'}, 'goal': 'Goal was not achieved'}, {'shop': {'value': '2130 Center Street\n Suite 102 Berkeley\n CA 94704', 'type': 'markdown'}, 'sales': {'value': 'Sales were **0 more** than the goal', 'type': 'markdown'}, 'goal': 'Goal was achieved'}, {'shop': {'value': '652 Pine Knot Avenue Big Bear Lake\n CA 92315', 'type': 'markdown'}, 'sales': {'value': 'Sales were **1 more** than the goal', 'type': 'markdown'}, 'goal': 'Goal was achieved'}]