The cache does not seems to work with API call

I created this small app, it call PowerBI API and get the result back, I don’t know what I am doing wrong, but the cache does not seems to be working ?
here is the link to the app

thanks for your help

1 Like

Hey @mim,

As promised from Twitter I have taken a peek at your app and its source code this morning! :bird:

If I understand the execution of your app and its functions I know why it seems as though @st.cache isn’t working for you.

Spoiler Alert:

:rotating_light: It’s running but when it’s finished the checks, changes are always detected in either the function or parameters!:rotating_light:

So, just a review on @st.cache, it remembers the last call of the function that it decorates and specifically checks 3 things: (see here for more info)

  1. The name of the function
  2. The actual code that makes up the body of the function
  3. The input parameters that you called the function with

It’s the 3rd one that’s tripping cache up in your app! The first parameter in Run_Query is DAX_Query_Value. The issue is DAX_Query_Value changes depending on the user input from the sidebar multi-select and select boxes!

This means that on the first run, DAX_Query_Value has the “default value”, then once the data is pulled from the PowerBI API call, the user options are created and depending on what’s chosen by the user this changes the DAX_Query_Value thus triggering @st.cache to rerun the function.

The best way to solve this is to have a function that just gets all the data you want, and then another that filters the data frame to have only the data that the user selects.

Let me know if this solves your problem,
Happy Streamlit-ing!

1 Like

hey @Marisa_Smith

thank you very much, I think I misunderstood how the cache works, I thought the cache will save multiple result based on a different Query input for example

Query1 ------ cache 1
Query2 ------ Cache 2 etc
loading all the data and filtering in Streamlit will not be very practical.

1 Like

Hey @mim,

Ah, yes I see. :eyes: The good news is your app works very speedily as is, so that is good! :star_struck:

Interesting info for you: :face_with_monocle:

The second iteration of @st.cache is currently being worked on! SO if you had any ideas or suggestions on what you think would make it better or easier to use.

Let me know here and I will make sure those ideas are passed along!

Happy Streamlit-ing!