This is way too long for a reasonable question. For a quick summary: two functions exist, very extremely similar to one another. One causes entire page to reload, the other doesn’t. Details on the code causing the behavioural difference in a reply below.
I’m adding a debug menu to my app to manipulate database tables for testing, and am experiencing some inconsistent behaviour when interacting with buttons that call functions that write to the database.
For context, I’m using pymongo, and have 2 tables, A and B.
Table A is pretty simple, 4 rows with 2 columns, “name” and “score”.
Table B is a bit more complex, and stores records of changes to those scores, so each row has “name”, “timestamp”, and “score_delta”.
My debug menu is laid out like so:
- container:
- container:
- col > button: purge_scores (deletes all scores
- col > button: populate_scores (inserts 4 known names with scores of 0)
- col > button: randomize_scores (randomizes all score values 1-999)
- table (session_state(“scores”))
- container:
- col > button: purge_records (deletes all records)
- col > button: random_record (insert 1 randomly generated record)
- table (session_state(“records”))
- container:
Session states values are loaded at run (if X not in session_state), and all of the functions called by the above functions do their thing to the table, the clear the cache on get_scores and get_records functions, then update session_state = get_scores/get_records.
The problem is this:
When interacting with the scores section, behaviour is mostly as expected, if I click “purge”, the table refreshes, now empty, but the page doesn’t reload. I can then click “populate” and the table contents will update and be shown, with the page not reloading. Same with random. If I switch between buttons one after the other the same behaviour occurs, with no unexpected reloading.
If I then click purge_records, the entire page reloads and the now empty table shows. If I then click generate_random, the entire page reloads again and the new record is shown. This reload occurs whenever I switch between the two record buttons, but not if I press the same button multiple times.
After interacting with the records area, going back and clicking any of the scores buttons refreshes the page entirely.
With that long winded explanation out of the way, my question is this: how do I avoid this entire page refresh occurring?
I’m using cache_data with a ttl for the functions that get scores, and rendering the tables using data in session state, making sure to enforce a cache clear when data changes, however the mismatch in behaviour has me stumped.