Py2Vega Name Error with variables defined outside function scope

Open
#36 2 comments 0 reactions 0 assignees View on GitHub

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

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantStack/py2vega

All issues in QuantStack/py2vega

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.