Streamlit cloud can't read integer variable

Okay this is my code.

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.
w
Why is this happening?

Hey @hyun_kyu_Cho

Thanks for your question!

Have you tried the following code:

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'

Best,
Charly

@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

And I made the issue in this repo

Also error messages are updated after every 5 seconds, just like this.

Thanks @hyun_kyu_Cho.

The error seems to indicate that the code is trying to access screen_setting in st.session_state, but this attribute hasnโ€™t been set.

Did you initialize screen_setting in the session state at the start of your Streamlit app?

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.

Best,
Charly

1 Like

@Charly_Wargnier
It actually worked really nice! Thank you so much sir.

Very welcome! Glad it helps! :hugs:

Charly

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.