Py2Vega Name Error with variables defined outside function scope
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- data-visualization
Research direction
Start by reproducing the provided DataGrid example and tracing how Expr processes the formatter function, especially the py2VegaNameError for outside_colors_dict. Identify the name-resolution path involved; done means expressions can reference variables defined outside the function while the existing inside_colors_dict behavior remains working.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Python
- Stars
- 10
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QuantStack/py2vega
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
QuantStack/py2vega#52 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
QuantStack/py2vega#51 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
QuantStack/py2vega#50 ·
-
pyproject.toml 😁 Open
Difficulty 3/5 1-2 days Newbie friendliness 52/100
QuantStack/py2vega#49 · 1 reaction ·
-
Assignment Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
QuantStack/py2vega#33 ·
All issues in QuantStack/py2vega
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·