Multi-page app with session state

I am currently getting the follow error when trying to run this code:

'SessionState' object has no attribute 'input'
1 Like

Hello @Codeguyross,

Could you share your source code? Itā€™d be easier to identify the problem.
My class has an underscore before SessionState. Maybe you tried to use this class along with Thiagoā€™s ?

1 Like

Hi @okld I am referring to this example to get better understanding of Session State Management


But after getting the return from the _get_state method , I canā€™t able to set the items to the SessionState class.

Hello @Gaurav_Gupta,

So you seem to have the same kind of error as @Codeguyross. In both cases, the exception is related to a SessionState object which I donā€™t use. In my code there is an underscore prepended: _SessionState.

Could you share your source code? I need more info to identify the issue.

1 Like

Hi @okld

1 Like

Hello @Gaurav_Gupta,

You have removed all the methods of my SessionState class in your code, no doubt it doesnā€™t function properly now ^^. The method which allows you to set new items in your class is __setattr__(). And you also need to access them by implementing __getattr__(). Include all the function I implemented (not only the 2 I mentioned), and it should work better.

And also, in your example you need to call state.sync() at the very end of your app to avoid rollback issues with your slider.

1 Like

Hi @okld

I havenā€™t used that beacuse I canā€™t see that we are calling that methods anywhere.
If possible can you explain me briefly.

1 Like

Methods surrounded by two underscores are called magic methods. Like the constructor __init__(), theyā€™re not meant to be called explicitly in your code, but are used when you interact with an instance of your class in some specific ways.

Letā€™s say you have initialized a new state object with state = SessionState().
To summarize briefly what each method does:

  • __call__(self, arguments): called when you do state(arguments), you use state as if it was a function.
  • __getitem__(self, item): called when you do state["My item"]. In the function, item == "My item"
  • __getattr__(self, item): called when you do state.my_item. In the function, item == "my_item"
  • __setitem__(self, item, value): called when you do state["My item"] = "my value". In the function, value == "my value"
  • __setattr__(self, item, value): called when you do state.my_item = "my value". In the function, value == "my value"

There are more magic methods that I donā€™t use for other specific usages. You have some tutorials/blogs presenting and explaining more of them in details if youā€™re interested:

5 Likes

Hi @okld
Thanks for your kind. Now its done its working and maintaining the state too.

2 Likes

Hi @okld
After updating to the latest Streamlit version, Iā€™ve came across an error
ModuleNotFoundError: No module named 'streamlit.ReportThread'

Have you implemented anything new to overcome this? Thanks.

2 Likes

Hello @vnguyendc,

I didnā€™t had time to check all the recent Streamlit changes, but with release 0.65 there was some code refactoring that broke session state implementations. Iā€™ll work on it asap!

2 Likes

Alright, Iā€™ve made a quick (untested :scream:) fix. Iā€™ve walked through the new code, and there was no major change in the parts I use. Some python files were renamed, and updating the imports should be enough.

1 Like

Thanks. Yes it works on v0.65.


Off-topic:
Is there a way to persist the session state across refreshes?
Maybe something like getting the device fingerprint should do right, instead of session id?
Is there a way for that? Or can it be added?

If memory serves me correctly, thereā€™s another implementation of state with a GlobalState that is maintained between refreshes.

2 Likes

Great, thanks! That worked.
But it seems to be global for anyone accessing the page from anywhere.

This would be a good solution for that right?

Itā€™s an interesting propositionā€¦could you take this in another post on the forum so we donā€™t hijack this one too much :wink: ?

1 Like

I still get this error 'ModuleNotFoundError: No module named ā€˜streamlit.ReportThreadā€™ after installing 0.65.0. Do i need to explicitly change anything in my code?

Hi @okld
Thanks for your sharing.
Can you share how can set up Session State for multi users working. As I use Session State of @thiago but session merge btw each users frequently

In my app, Thatā€™s embed in our ERP system, where one user click to Streamlit App, will have one auth_token in url para itā€™s user_id. But when other users come to same time, itā€™s will go to earler user session.

Do you have some advise for me?

I have been getting it aswell. Not sure how onerous it would be for your code, but you could always write the data using pickle and read it again when you need it at a later stage. Its how I over came this issueā€¦not the most elegant solution but it workedā€¦

Hmm, this is weird. Merging is not supposed to happen and may be related to another problem. Could you share some code reproducing the issue youā€™re encountering?