I have a page using a class with __init__ method, using self for all methods and attributes. And I tried for the first time using @st.cache_data on my def pipeline(self, path) function. It is working with st.cache (just gives a warning about deprecated method and etc). But when I use the new one, it gives this error: TypeError: SellingPrices.pipeline() missing 1 required positional argument: 'self'
It treats self like a parameter that should be passed on the function calling, so I tried excluding this parameter from the cache by setting an underscore before the self variable as the docs explained but the same error message continues.
I really like making my streamlit app using classes with __init__ methods, and Iâd like to use these cache methods as well, so could anyone please help me with that?
The caching function stores an output associated to a specific input, so it doesnât strike me as something Iâd want to put on a method. Can you create a builder function and cache that?
Down below is the skeleton of my page using this class, and Iâd like to use the caching decorators for most of these methods. I like doing like this as this keeps the code more organized, readable and better for maintenance. Especially because I can call the methods for some attributes âbeforeâ instantiating them, by using self.method in the __init__ constructor method.
Iâm just not sure it makes any sense to put a caching decorator directly on a method. The caching function has to index the output it is caching according to the input and I wouldnât assume a priori that itâs made to be compatible with self as a parameter. (Indeed, your test appears to confirm it is not compatible with self as a parameter.)
If you want to adapt a decorator to accept a method, you can do that⊠python - Using the same decorator (with arguments) with functions and methods - Stack Overflow However, I think getting the desired results would be quite tricky for a caching decorator, specifically. The caching function does not save side effects; it only saves the final output. So caching any function or method that requires a change not reflected in the actual, returned value is a no-go. (Your reset_date method is just not something youâd cache as its purpose is the change it makes to session state, not the object it is returning.)
I played around with the idea a little, but it is very convoluted to make it do want youâd likely want even in the subset of methods where caching might make some sense.
I guess Iâll have to choose between coding with classes, in which I prefer because of better codeâs organization, readability and reliability. Or coding in a procedural way so I can use the streamlit caching decorators on my page.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking âAccept allâ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.