crxi / multipage_streamlit

MultiPage Streamlit
Other
16 stars 3 forks source link

Initializing states on all pages #3

Open frankvgompel opened 2 years ago

frankvgompel commented 2 years ago

Thanks! Amazing functionality.

Perhaps you can help with my multi-page form. I am collecting data across pages and on the last page submitting the form (saving the file). I would like to initialize states from all pages when submitted.

Is there a straightforward solution?

crxi commented 2 years ago

I take in that you mean you have data X on Page A, data Y on Page B and a form F on Page C. You will set the values of X and Y on Page A and B respectively and when you enter Page C, you want the form F to be initialized with the values of X and Y that you set previously. Is that right?

Perhaps you can post a small example here. It would make it a lot easier to figure out a solution together.

frankvgompel commented 2 years ago

The most simple example is a record of 3 variables (x, y, z) that we fill in.

page 01: initially x = '' change to x = 10 save to json

page 02: initially y ='' change to y = 20 append to json

page 03: initially z = '' change to z = 30 append to json

page 04 load .IPC as df load json as df append df write as .IPC

After saving the data, we start over for a new record so I would like to initialize all settings from all pages.

I hope my example makes sense. In reality we have in total 80 variables (with search functions) to assign; divided over 3 or more pages.

crxi commented 2 years ago

How about using multiple st.expander on one page instead of multi-page to organize the 80 variables? That way all the states (x, y, z, etc) are accessible from the form which is on the same page. It is much easier to manage streamlit states that are on one page. For the users, they can scroll up and down to see the effect of changing x, y, z on the form.

If you still want multi-page, then perhaps you can code a small mock example to illustrate the issue you are facing. Your description says what you want the app to do but does not show the problem you are facing getting it to work.

frankvgompel commented 2 years ago

How about using multiple st.expander on one page

That might obviously work and I will try that. However, the idea was to use a multipage solution.

Sorry for not getting across what my actual problem is. I don't know how to clear the saved states. Not for a single page and certainly not for several.

How would you do that? I tried a few things that don't work such as:

clear_page = st.sidebar.button('Clear States')
if not clear_page:
    state.save()

I saw there is a clear method in the Streamlit SessionState class but I am not experienced enough to incorporate this into your code.

 def clear(self) -> None:
        """Reset self completely, clearing all current and old values."""
        self._old_state.clear()
        self._new_session_state.clear()
        self._new_widget_state.clear()
        self._key_id_mapping.clear()

But that probably only clears states from a single page?

crxi commented 2 years ago

Given that Streamlit runs every time an UI element changes in value for almost all of its widgets and you have 80 of them, it is probably best to encapsulate them in a single form. Elements in forms are only evaluated when "Submit" button is pressed.

https://share.streamlit.io/crxi/demos/main/st_expforms/demo.py Above is an example which might be close to what you are aiming for. No multi-page required. Just expanders.

Code is at https://github.com/crxi/demos/blob/main/st_expforms/demo.py

frankvgompel commented 2 years ago

Thanks for your help and feedback.

frankvgompel commented 2 years ago

Unfortunately for my use of selectboxes, forms seem unusable. I reported a bug but it appears it is intended behaviour:

https://github.com/streamlit/streamlit/issues/4664#issuecomment-1112653501

Is there a way to call st.session_state.clear() ?