aghasemi / streamlit_js_eval

A custom Streamlit component to evaluate arbitrary Javascript expressions
MIT License
75 stars 12 forks source link

I get an error from measuring width #9

Closed atfot closed 4 months ago

atfot commented 5 months ago

This is my code.

if 'screen_setting' not in st.session_state:
    x = streamlit_js_eval(js_expressions='window.innerWidth', key='WIDTH',  want_output = True)
    sleep(2)           
    if x<662:
        st.session_state.screen_setting='mobile'
    if x>=662:
        st.session_state.screen_setting='pc'

And always I get this error. The code itself is working properly, but why is this happening?

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 17, in <module>

    if x<662:

       ^^^^^

TypeError: '<' not supported between instances of 'NoneType' and 'int'
aghasemi commented 5 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)
atfot commented 5 months ago

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

aghasemi commented 5 months ago

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.

atfot commented 4 months ago

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..

qe

aghasemi commented 4 months ago

@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'