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?
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?