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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.