Closed atfot closed 4 months ago
The following code works on my machine:
if 'screen_setting' not in st.session_state:
if (x := streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output = True)) is not None:
if x<662:
st.session_state.screen_setting='mobile'
if x>=662:
st.session_state.screen_setting='pc'
st.write(st.session_state.screen_setting)
or slightly more concise:
if 'screen_setting' not in st.session_state:
if (x := streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output = True)) is not None:
st.session_state.screen_setting='mobile' if x<662 else 'pc'
st.write(st.session_state.screen_setting)
Yeah right? Mine and yours works with no problem, but the thing is it shows really brief error screen and disappears real quick, and works. Maybe this error is from streamlit cloud? idk
Yeah right? Mine and yours works with no problem, but the thing is it shows really brief error screen and disappears real quick, and works. Maybe this error is from streamlit cloud? idk
The streamlit_js_eval
function briefly returns None before it is updated with the actual value. You need to consider that situation in your code so that it does not cause an exception. The second if
in my code is for that reason.
So I tried your codes without changing anything again but it still shows brief error and the error screen disappears. And that error messages are constantly updated after 5 seconds..
@atfot You are likely just deferring the issue to further down in your code (i.e. that line when you check if screen_setting is pc
).
I'm not sure what else can be done, but if yu need that session state variable to have a value, maybe set a default value for it (pc
), and then let SJE change it to the actual value after this short delay.
One example:
if 'screen_setting' not in st.session_state:
if (x := streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output = True)) is not None:
st.session_state.screen_setting='mobile' if x<662 else 'pc'
st.write(st.session_state.screen_setting)
else: st.session_state.screen_setting='pc'
This is my code.
And always I get this error. The code itself is working properly, but why is this happening?