How do I access `self` in callback method?

Hey everyone! So I’ve been messing around with All in one components and pattern matching callbacks and know that they have a lot of cool stuff to play around with, but I’m getting stuck on trying to access self within methods that use the callback decorator on them? I wouldn’t be surprised if this is an entirely nonsensical question given my ineptitude when it comes to working with classes, but would love some direction if anyone has good resources to look at to help with that. Thanks in advance! :slight_smile:

Hi there,

What is your class exactly, can you post a minimum working example code here?

In general, the self using lines like self.some_parameter = 0 is only accessible from within the class code (e.g. from a method defined in the class itself). If you want to modify the class from the outside, e.g. to store the result of your callback on the class instance and return it, you can do this using your_class.a_new_parameter = result.

This means that in order to access the class data (what you call self above I assume) in your callback, you will need to find a way to pass the class element itself to the callback.

Again I’m not exactly sure what it is you want to do and if my explanation helps, a code snippet of your class and callback where you want to use it would help a lot for the explanation.

2 Likes

Yep I definitely think that you’re on the track for the solution I need - let me try to assemble a minimum working example either tonight or tomorrow and reply to your post then if that’s ok? I’ve been trying to learn how structure my classes in a legible / production-ready setup, but I’ll have to apologize in advance if it’s not the prettiest code. Thanks for the help and I’ll be in touch :slight_smile:

One thing to keep in mind is that the class structure in AIO components is really for “organization” only, it’s not meant to be “object-oriented”. That’s because the callbacks still need to be stateless in order to handle multiple users with independent sessions hitting the same callback functions. So, be careful about writing variables with self inside a callback function as what one session writes in one callback may override what the next session writes when the next callback is called.

Hey @chriddyp. So I think I’m confusing myself, but the component that I’m trying to build out is basically a set of dropdowns that will “waterfall” information down based on a Pandas Dataframe that has the mapping structure of the relationships in long format. (i.e. lets say that it is a DataFrame that looks like this:
image)
Now I would like two sets of dropdowns, of which the first set allows you to choose the order of the columns to be sliced (I.e. subject > sex > condition | sex > subject > condition | condition > sex > subject, etc). And the second set of dropdowns would set the options for the dropdown based on column order selected in the first set of dropdowns (i.e. If I selected subject > sex > condition in the first set of dropdowns, it would allow me to choose from (1,2,3,4), then (M, F), then (control, cond1, cond2). The ultimate goal of this being to create the ability for a user to dynamically slice and aggregate data , just given a table of mapping options. If I could solve this problem, then it would allow me to iterate through exposing data from a database and would allow the user to have full flexibility on how they view the data. Any help that you can provide on this front would be immensely appreciated and I’m happy to answer any questions you have or try to get off the ground with best attempt at it. Thank you!! :slight_smile:

I guess to see if I’m going in the right direction, will this require making a store component that can hold the mapping data, then accessing the information from that component in each of the subsequent callbacks, instead of trying to access the dataframe directly from the init of the class, if that makes sense?