altair icon indicating copy to clipboard operation
altair copied to clipboard

Support evaluating Vega expessions in Python for experimenting and debugging

Open jonmmease opened this issue 1 year ago • 3 comments

An idea came to mind while reviewing https://github.com/altair-viz/altair/pull/3362. In order to help people understand expressions better, it might be nice to make it possible to evaluate them and see their results in Python (using VegaFusion)

Something like:

expr = alt.expr.sqrt(alt.datum["col"])
scope = {"col": 4}

eval_expr(expr, scope)
2.0

Here's a simple implementation using transformed_data and the calculate transform

def eval_expr(expr, scope=None):
    scope = {"_placeholder": [0]} if scope is None else scope
    source = pd.DataFrame({k: [v] for k, v in scope.items()})
    return alt.Chart(source).transform_calculate(result=expr).transformed_data()["result"].iloc[0]

There are a lot of expressions that VegaFusion doesn't support yet, but this might be good motivation to improve coverage. And the error message you get using the approach above is pretty poor, so I think I may want to do some work in VegaFusion to either improve that, or to have a dedicated entry point for expression evaluation.

Anyway, does this seem useful for teaching people how to use expressions in charts?

jonmmease avatar Mar 15 '24 11:03 jonmmease

I do think this would be helpful; I often find it hard to troublshoot when I get lost halfway through something. Although recently I have started becoming more acquainted with the additional functionality in the Vega Editor and I find the Signal viewer they have helpful for understanding what is going on with a value of a parameter. Would there be redundancy between that and what you suggest here, or are there additional use cases that this would support?

joelostblom avatar Mar 15 '24 21:03 joelostblom

The only use case I had in mind was for debugging signals, so teaching people to use the editor to do this would be a fine approach. A downside is that this forces the use of the Expression string syntax, but that might be fine.

It also just occurred to me that, rather than using VegaFusion, vl-convert could perform the expression evaluation, this way the fully expression language would be supported.

jonmmease avatar Mar 15 '24 22:03 jonmmease

I'd find that very useful! 🥳 One use case would be that I no longer/less often would have to go to the Vega Editor to debug expressions and then port the changes back to Altair. Maybe the work on this could also lead to Javascript errors being raised in Python? See https://github.com/vega/vl-convert/issues/47. I sometimes have chart specs which just do not display in a Jupyter notebook due to a JS error and I only see it in the Vega Editor.

Sounds reasonable to me to use vl-convert to get full support for all language features!

binste avatar Mar 16 '24 13:03 binste