Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Can we save component properties as variables?

Hello coders!
I am using daq.BooleanSwitch to change the state of my page and also I want to save those boolean values as a variable so that i can use it in further application.
Here is a snippet of my code-

                    daq.BooleanSwitch (id='switch',
                                      label='Manual | Automatic', 
                                      labelPosition= 'bottom',
                                      color='#00CC96',
                                      on = True,

Here i want to save property ‘on’ in variable but I can’t find a way to do it. I tried using callback definition’s return value to save as variable but it does not work. Is there any other way to do it?

Thanks in advance! :slight_smile:

Hi,

Welcome to the community!

Could you elaborate a bit what you mean by in further application? Is it a state you want to persist over the session?

In any case, I would recommend you to take a look in the dcc.Store component:

https://dash.plotly.com/dash-core-components/store

This is the canonical way to persist data in an application client-side, so it can be reused in different callbacks and further sessions for the same user.

Hi, thanks for your reply and sorry for inaccurate information.
Yes i checked dcc.store component but unfortunately that is of no use to me.
So to explain, i need those boolean values stored in a variable so that I can use that variable in another python script which is independent of dash.

I hope you get it now and I get the solution.

No worries, thanks for clarifying it! :smiley:

If the purpose is to use as input to a separate process, then I would consider as first approach to save it as a file (say, pickle). It adds a bit of complexity, but it also separates the application to the other process better. Then, as a next step, you can replace it for a database…

Would that be an alternative?

That sounds like a solution but I have would have dynamic data, for example there would be ‘n’ number of times I would use the switch and it would generate those boolean values. So is it possible to update it in pickle file each time automatically?

I think I would need to understand the context a bit better before I can give you a proper answer… The risk of doing this using files is how you keep the application and files updated across different users and concurrency, so I tend to believe that there might be a better solution depending on what is the purpose of such switch.

So the main purpose of switch is to generate boolean values and use those values in another python script which is independent of dash firmware. That is the reason I wanted to save those values as a variable. Is it possible?

Sorry, I might have expressed myself wrong… What I meant to ask is if this switch needs to have a state that is shared across multiple users, if you expect the state to be synced with the value used in the other application and so on…

You could in principle create a file each time the switch is toggled, but that won’t work if you have two users making updates concurrently. Plus it will be very hard to keep the switch value synced with this approach, as you would need to read it from the latest file.

Another alternative could be to use [server-side caching] and access the caching state from the other python process, but I don’t know what triggers the other process and how you are running it, so please take it as a direction to investigate further.

Thank you for your valuable input.
There will be single user but my only concern here is that I want the state of the switch to be saved as variable so that I can use that variable in another python scripts.