Hi,
for some reason I need to know the size in pixel of a container.
Is there any way to get the actual size of a html.Div container with a clientside or serverside callback?
I can see those props in the browser inspection but in dash I cannot acces to them.
Can someone setup a simple example?
Here a JS example that works
https://stackoverflow.com/questions/3839227/how-to-get-height-of-div-in-px-dimension
Thank you so much
@davidpsq ,
You can perform javascript via a clientside_callback and either return the values in the callback, or just have the values sent to the console with console.log.
The javascript in the stackoverflow should work, just pass it the id of the element that you want to know.
Sorry,
my understanding of JS is poor.
I written this code but it does not work:
var myDiv = document.getElementById(“myDiv”);
myDiv.Height;
Associate this with a button in your layout, this is just an example to give you the height and width of the div in an alert box.
import dash
from dash import html, dcc, Input, State, Output, ctx
app = dash.Dash()
app.layout = html.Div([html.Div(id='myDiv', children="hello there"),
html.Button(children='Get myDiv info', id='myButton', n_clicks=0)])
app.clientside_callback(
"""
function showDetails(n1) {
if (n1 > 0) {var myDiv = document.getElementById("myDiv");
alert([myDiv.getBoundingClientRect().height, myDiv.getBoundingClientRect().width]);
} return 'Get myDiv info'
}
""",
Output('myButton', 'children'),
Input('myButton', 'n_clicks'),
prevent_initial_call=True
)
app.run_server(debug=True, port=8051)
1 Like
@jinnyzor Interesting!
thank you very much. btw it does work in Chrome but not in Firefox.
Is there any way to trigger the callback using the changing size of the Div (for example resizing the browser)?
@davidpsq ,
You might benefit from this:
@AnnMarieW , thanks for sharing this with me.
1 Like
@davidpsq
You might find these two posts helpful as well:
Hi all, I just published a new dash_breakpoints component on pypi.
It allows to create DOM breakpoints (width and height) that trigger callbacks when they are crossed. The main use case is to change the content of the app based on screen sizes, when css only does not cut it. My use case was the following: I have a layout with a topbar and a sidebar, but I want that on a small screen, all the content of the topbar and sidebar is rendered in a drawer instead, while keeping the same component ids …
@IvanLiu Ah, my bad, realizing the topic header says “expands on hover” lol. My solution expands on window resize, not hover.
As for the container, I created a container that had a margin-left equal to the width of the sidebar.
app.layout = dmc.MantineProvider(
theme={
'fontFamily': '"Inter", sans-serif'
},
withGlobalStyles=True,
withNormalizeCSS=True,
children=[
dcc.Location(id='url', refresh=False),
dmc.Notification…
Yes this code is working . Thanks
1 Like
Finally i used it and the issue solved for the website i was working.
1 Like