QuantStack / py2vega

BSD 3-Clause "New" or "Revised" License
9 stars 4 forks source link

Py2Vega Name Error with variables defined outside function scope #36

Open supriyakhandekar opened 4 years ago

supriyakhandekar commented 4 years ago
    import pandas as pd 
    import numpy as np
    from ipydatagrid import DataGrid, TextRenderer, Expr, VegaExpr
    size = 50
    np.random.seed(5)

    # Create some random data to work with
    df = pd.DataFrame({'Quantitative': np.random.randn(size),
                   'Categorical': [np.random.choice(['A', 'B','C']) for i in range(size)],
                   'Temporal': pd.date_range(end=pd.Timestamp('2019-09-01'), periods=size),
                  })

    df = df[['Quantitative', 'Categorical', 'Temporal']]
    example_data = json.loads(df.to_json(orient='table'))

    outside_colors_dict= {'C': '#9400D3', 'D': '#00FF00'}

    def formatter(cell):
        inside_colors_dict = {'A': '#E61E3C', 'B': '#FF7F00'}

        #using inside_colors_dict
        #these cells will be colored
        if(cell.value == 'A'):
            return inside_colors_dict['A']
        elif(cell.value == 'B'):
            return inside_colors_dict['B']
        #using outside_colors_dict
        #these cells will not be colored
        elif(cell.value == 'C'):
            return outside_colors_dict['C']
        elif(cell.value == 'D'):
            return outside_colors_dict['D']
        else:
            return '#787878'

    renderers = {'Categorical': TextRenderer(background_color=Expr(formatter))}

    DataGrid(data=example_data, base_column_size=125, layout={'height':'250px'}, renderers=renderers)

In the code above, we see that we are encountering an error when the function passed into Expr() (used in the Text Renderer) tries to reference the dictionary outside_colors_dict, which is defined outside the formatter() function, when trying to set the background color of a cell in the grid. On the other hand, it does not have any issue with using the inside_colors_dict. (Note: this code should have no issue if all references to ‘outside_colors_dict’ in the formatter function are changed to ‘inside_colors_dict’

It seems as though we are encountering this py2VegaNameError when we pass a function into Expr() that makes use of a variable that is defined outside of the function. The only way the function works currently is by redefining the same outside variables needed (which may be used in other parts of the code) again in the function passed into the Expr().

Would it be possible to have it such that the function passed to Expr() can make use of variables defined outside of its scope?

martinRenou commented 4 years ago

Thanks for opening an issue! It is indeed not possible yet.

We might be able to pass the function scope to the transpiler, we just need to know how to access this scope.

Note that you won't be able to have access to variables like NumPy arrays, only simple types like lists, tuples, dicts, numbers, strings are supported.

martinRenou commented 4 years ago

Instead of trying to access the function scope, it might be a good idea to change the py2vega API. adding a scope argument to the expression.

This would more clearly convey the idea that it's not a classic Python function, that it is not called in this context.