from streamlit_js_eval import streamlit_js_eval
if 'screen_setting' not in st.session_state:
x = streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output = True)
st.write(x)
if x<662:
st.session_state.screen_setting='mobile'
if x>=662:
st.session_state.screen_setting='pc'
2024-04-22 05:33:03.469 Uncaught app exception
Traceback (most recent call last):
File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 584, in _run_script
exec(code, module.__dict__)
File "/mount/src/neri/streamlit_app.py", line 16, in <module>
if x<662:
^^^^^
TypeError: '<' not supported between instances of 'NoneType' and 'int'
2024-04-22 05:33:03.470 503 GET /script-health-check (10.12.221.206) 2.78ms
but in the screen it shows brief error and disappear
also it shows st.write(x) just fine.
Why is this happening?
if 'screen_setting' not in st.session_state:
x = streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output=True)
st.write(x)
if x is not None:
if x < 662:
st.session_state.screen_setting = 'mobile'
else:
st.session_state.screen_setting = 'pc'
@Charly_Wargnier
It still shows me error.
The owner of the repo said that it gets None as the value briefly and updates the value.
Can I really avoid any error messages?
This is another code that I tried
if 'screen_setting' not in st.session_state:
x = streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH', want_output=True)
try:
if x < 662:
st.session_state.screen_setting = 'mobile'
else:
st.session_state.screen_setting = 'pc'
except Exception as e:
pass
I’ve also checked out the GitHub issue you shared.
It might be worthwhile to assign a default value to screen_setting immediately and then update it once x has a proper value:
if 'screen_setting' not in st.session_state:
st.session_state.screen_setting = 'pc' # default value
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'
Hopefully, this should help to eliminate the error. Let me know how it goes.