Is it possible to run python commands interactively through Dash?

Hi all,

I am thinking of using Dash for a potential project, however I am not fully aware of all the options and features that Dash can offer.

I was wondering if it is possible, that the is some kind of console in the Dash app that the user can execute commands on the spot.

E.g. Lets say that you have a data table called “stocks”. And the app shows the line graph of stock A or stock B that are inside the “stocks”. The user may want to see the line graph of A and B.

He could then execute in python
stocks[‘AnB’] = stocks[‘A’] + stocks[‘B’]

and update the data table stocks.
Something similar I think exists in SPSS.

Is that possible in Dash by plotly?

Thanks

I don’t think there is an option for running Python interactively, but in your example I think you could define a form asking for the columns to operate on, a dropdown with the allowed operations, and a button to add the calculated field to the table, and manage all the logic in a callback.

If if it is for a graph, you might be better off with a checklist of the stocks to aggregate, and then building a callback that takes the selected stocks, sums the values, and then returns the graph. That way if only A or B are selected you would get the original lines, but if both are selected you would get the sum.

If you do need to run python statements, then I think you could use something like https://docs.python.org/3/library/functions.html#exec and pass the statements as strings from an input. This is probably a really bad idea since the users could run whatever they want, and in any case at best I suppose you could only modify the data in a data store or return the results of the statement in a Div.

the other users would be also data scientists.

or maybe I would like to have a fast and easy but unique operation to the “main” dataset that goes into the graphs.

If I use the exec on the ‘stocks’, is the ‘stocks’ datatable going to be updated inside the Dash framework?

You can use exec to pass commands via the callback, but not only is this probably a rather bad idea due to security issies, as @rafaelc says it also means that your users have to understand the state of the Python process (ie which variables are initialised and their type) in order to interact with the dashboard.

You’d be better of creating different a simple set of abstractions around the commands you want to pass into your DataFrames. Like check boxes with the columns you want to include etc, and dropdowns with any operations to apply.