Avoid expensive reload of the page

Dear all,

I’ve a page that is structured like this

There are more or less shown 600+ couples of rows that are similar. For a certain condition (depending on the values of the rows) I’m creating a radio button for some couples. I’m ending up with more or less 400 radio buttons. Apart of the fact that the loading of the page is very slow, but every time I click the radio button of a certain couple streamlit reloads the entire page (note that the radio button doesn’t influence the page, so it’s reloading exactly the same page) and it takes ~1 minutes for every radio button and this makes this page unuasble for a user…
Any suggest on how to avoid this? Code snippet here:

Is there a way maybe to cache only the st.write at line 212? This would avoid 80% of the useless reloading of the page. Thanks for helping :slight_smile:

Lorenzo

The following may or may not be applicable to your case:

  1. In most cases users do not need to have 600 tables and 400 radio buttons in the same page. Usually they are more confortable dealing with smaller chunks.
  2. ~1 min is way too much. The picture of your code suggests that you are repeating a lot of calculations and logic in each rerun. You can store the result and use it in succesive reruns instead.
  3. Widgets inside a form do not trigger a rerun when the user interacts with them.

Hi Goyo,

  1. Yes I know that from an UX point of view it’s quite a tragedy. In this page I’m showing, with some criteria before it, all the possible duplicates within a dataset. Usually I’d not expect this amount (400) but in this case they’re a lot.
    I know it’s something the user probably won’t do but I’d like to give him the possibility to choose to keep one row or the other.

  2. The answer is without any doubt yes, but the problem is that I can’t avoid this… imagine that I’m in the same state of the page, I’ve to do these computations somewhere and I know the parameters only when the page is shown the first time. How can I prevent a rerun to execute again all the computations if they are coded in the same page that is being rerun?

  3. This could be interesting, but can I store 400 variables in 400 different widgets in 400 different forms? And moreover, how could I encapsulate only the radio button in a form? I wouldn’t like to add also the submit button… is this doable?

Thanks for the feedback

I already told you. The first time you compute and store the results, next time you use the stored results, no need to compute the same thing again.

I am not aware of any limits in the number of widgets in a form or the number of forms. You can also put the 600 tables and 400 radio buttons inside just one form.

No. But then, what is an user supposed to do? Because, according to your description, just clicking the radio buttons isn’t going to take them anywhere. So they should do something else after that, shouldn’t they?

  1. I already told you. The first time you compute and store the results, next time you use the stored results, no need to compute the same thing again.

Yes, I know that I can do it somewhere else, but the problem is that the expensive computation is for the 99% due to the printing of the rows, that is something I can’t do in advance. So the point is yes, to compute the numbers outside the for and not every time the page reloads would acutally help, but will speed up the time only of 1%

  1. No. But then, what is an user supposed to do? Because, according to your description, just clicking the radio buttons isn’t going to take them anywhere. So they should do something else after that, shouldn’t they?

The idea is to store every radio result in a session_state array and when the page is refreshed I still have all the results and I can apply the transformation I want

I don’t see why you couldn’t also cache the numbers computed inside the for. That might require some refactoring. Unfortunately I can’t provide more specific advice without actual code that I can run.

But does the transformation depend on the radio button? Because if it does, that doesn’t match what you wrote before: “the radio button doesn’t influence the page, so it’s reloading exactly the same page”.

If the transformation depends on the radio button you only have to apply it when the radio button changes, not on any reload. Again, I can’t be more specific without code that I can run.