thunderbug1 / streamlit-javascript

Streamlit component to execute javascript code on the client side and get back the result
MIT License
102 stars 3 forks source link

Get element size #7

Closed konstantin-frolov closed 2 years ago

konstantin-frolov commented 2 years ago

Hello, your componet is wonderfull. But i have an issue. I'm trying to get DPI from a Streamlit app to scale my plotly images. In streamlit code I add custom element with fixed size 1x1 inch: dpi = '<div id="dpi" style="height: 1in;left: -100%;position: absolute;top: -100%;width: 1in;;"></div>' st.markdown(dpi, unsafe_allow_html=True) I then try to get the width/height of the element using your component. js_code = """document.getElementById("dpi").offsetHeight""" height = st_javascript(js_code) And have exception TypeError: Cannot read properties of null (reading 'offsetHeight') But in console I can get elemnt parameters with eval() image st_javascript does not see the created element at the time of the call

konstantin-frolov commented 2 years ago

Fixed by using js_code = """window.parent.document.getElementById("dpi").offsetWidth"""