What's the best way to debug reruns of the app?

Hey all,
I have an app that does the following:

  1. gets available data from db
  2. lets user select data he wants (multiple samples)
  3. for each selected sample data it
    A. optimizes a fit for the curve
    B. plots the data, the fitted curve, some settings
  4. Merges all plots together and shows a merged plot and a table of results
  5. Allows uploading the results into the db
  6. Sidebar with settings to control plots and other settings

The fitting and plotting part is time consuming, and each widget interaction leads to a rerun of the app, and it runs the fitting function again and again
All functions (including the fit function) are cached and should not rerender and rerun on every iteration
It is very time consuming when i have a lot of samples selected, but also for a few samples, widget changes should not cause a rerun of the whole app.

I’ve tried implementing logger to print every function rerun and time it, but i still cannot find which part is not cached and causes a rerun of the whole app…

I’ve tried using st.fragment which solves the rerun problem, but changes of the widgets (on sidebar for example) now do not affect the fragments and also the 4th point above is affected, when changing each separate plot, it does not affect the merged plot.

Minimal reproducible example is almost impossible since it is a huge app and I am lost trying to find what causes the reruns

Can anyone shed light on what could be causing it? Ideas on how to debug and find what causes the rerun?

Hey @alonsh, I agree that debugging reruns of Streamlit apps is not necessarily trivial. The best thing you can probably do is having a look at the console for Rerun message, e.g. when clicking on a button with key=Foo you would see something like this:

2024-08-30 11:53:48.253 DEBUG   streamlit.runtime.scriptrunner.script_runner: Running script RerunData(widget_states=widgets {
  id: "$$WIDGET_ID-9dcfd67d2a579080785719eed0566599-Foo"
  trigger_value: true
}
, page_script_hash='32cf055030671eafef27e2492f0de810')

so, providing keys to your widgets would help to map them to the rerun logs as the key shows up in the id (last part).
And then, perhaps it makes sense to add a debug log at the top of each of your cached functions (perhaps together with the arguments). If they are truly cached and no argument value changes, then you should see the logs only once right?
From the docs, especially this part is interesting: “By default, all parameters to a cached function must be hashable. Any parameter whose name begins with _ will not be hashed”.

I hope this somehow helps you to narrow down the issue :slightly_smiling_face:

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