I’m trying to fire an event, whenever a toast gets resized.
import dash
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, ClientsideFunction
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
import numpy as np
style_toast = {
"position": "fixed",
"top": 50,
"left": 50,
'zIndex': 6,
"maxWidth": "90vw",
"width": "15vw",
"height": "30vw",
"maxHeight": "90vh",
"resize": "both"
}
style_fig = {
'height': 'inherit'
}
x, y = np.random.uniform(size=50), np.random.uniform(size=50)
fig = go.Figure(data=[go.Scattergl(x=x, y=y, mode='markers')])
fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
dbc.Toast(dcc.Graph(figure=fig, style=style_fig),id="my_toast", style=style_toast, header="resize me!"),
html.Div(id='dummy')
])
app.clientside_callback(
ClientsideFunction(namespace="clientside", function_name="inherit_height"),
Output('dummy', 'children'),
Input('my_toast', 'id')
)
if __name__ == "__main__":
app.run_server(debug=True)
assets/scripts.js:
/* JavaScript namespaces */
if (!window.dash_clientside) {
window.dash_clientside = {};
}
window.dash_clientside.clientside = {
inherit_height: function(id) {
console.log('event triggered');
// this gets printed
document.getElementById(id).addEventListener('resize', function() {
console.log('size changed');
// this never gets printed
// TODO: later: resize div of graph's height
})
return window.dash_clientside.no_update;
}
}
Like mentioned in the comments, the function inherit_height is called but the event listener is not listening to resizes.
Anyone with a clue why?
I’m doing this and the function is called ‘inherit height’ because I’m still trying to make a graph inside a toast inherit the toast’s
height and not only its width, which seems to a bug: