Hi,
I want to create a slider and a colored box that changes brightness depending on slider position (i.e.: slider at 0= white, slider at 100=black and shades of grey inbetween).
The straightforward implementation with streamlit’s slider was suboptimal because everytime the slider was used, the code reran making it a pain to use because the colored box didn’t update immediately (a smooth transition is very important in this use case).
I tried to create the same thing using javascript and put it in a html component:
import streamlit as st
import streamlit.components.v1 as components
html_code = f"""
<html>
<body>
<input type="range" id="brightnessSlider" min="0" max="100" value="50" onchange="updateColor()">
<div id="colorBox" style="width: 200px; height: 50px; background-color: rgb(0,0,0);"></div>
<script>
function updateColor() {{
var sliderValue = document.getElementById('brightnessSlider').value;
var grayValue = 255 - (sliderValue * 2.55);
document.getElementById('colorBox').style.backgroundColor = 'rgb(' + grayValue + ',' + grayValue + ',' + grayValue + ')';
}}
updateColor();
</script>
</body>
</html>
"""
components.html(html_code, width=200, height=250)
This works great, the only problem is now: How do I access the sliderValue variable so that I can use it in my script for other stuff? I understand the html component can access python variables, but how can the python code access the html variables?
I have seen this tutorial but for some reason I cannot recreate the example project. At the same time it seems a bit overkill considering that I just need one small simple thing.
Thanks!