You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpandasaspdimportnumpyasnpfromipydatagridimportDataGrid, TextRenderer, Expr, VegaExprsize=50np.random.seed(5)
# Create some random data to work withdf=pd.DataFrame({'Quantitative': np.random.randn(size),
'Categorical': [np.random.choice(['A', 'B','C']) foriinrange(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'}
defformatter(cell):
inside_colors_dict= {'A': '#E61E3C', 'B': '#FF7F00'}
#using inside_colors_dict#these cells will be coloredif(cell.value=='A'):
returninside_colors_dict['A']
elif(cell.value=='B'):
returninside_colors_dict['B']
#using outside_colors_dict#these cells will not be coloredelif(cell.value=='C'):
returnoutside_colors_dict['C']
elif(cell.value=='D'):
returnoutside_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?
The text was updated successfully, but these errors were encountered:
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.
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?
The text was updated successfully, but these errors were encountered: