Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support evaluating Vega expessions in Python for experimenting and debugging #3365

Open
jonmmease opened this issue Mar 15, 2024 · 3 comments

Comments

@jonmmease
Copy link
Contributor

An idea came to mind while reviewing #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?

@joelostblom
Copy link
Contributor

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?

@jonmmease
Copy link
Contributor Author

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 jonmmease changed the title Support evaluating Vega expessions with VegaFusion Support evaluating Vega expessions in Python Mar 15, 2024
@binste
Copy link
Contributor

binste commented Mar 16, 2024

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 vega/vl-convert#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!

@jonmmease jonmmease changed the title Support evaluating Vega expessions in Python Support evaluating Vega expessions in Python for experimenting and debugging Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants