Giter Club home page Giter Club logo

py2vega's People

Contributors

johanmabille avatar martinrenou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

py2vega's Issues

Assignment support

Assignments could be potentially supported.

e.g.:

def foo(value):
    a = 36
    return a if value < 4 else 32

Could be transpiled to:
"value < 4 ? 36 : 32"

Fails on python3.9

When running https://github.com/bloomberg/ipydatagrid/blob/main/examples/ConditionalFormatting.ipynb on python3.9 the following error is raised:

File "/python3.9/site-packages/IPython/core/interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-5-d23227669e25>", line 28, in <module>
    background_color=Expr(format_based_on_other_column),

  File "/python3.9/site-packages/ipydatagrid/cellrenderer.py", line 25, in __init__
    super().__init__(value=value, **kwargs)

  File "/python3.9/site-packages/ipywidgets/widgets/widget.py", line 412, in __init__
    super(Widget, self).__init__(**kwargs)

  File "/python3.9/site-packages/traitlets/traitlets.py", line 1080, in __init__
    super_kwargs[key] = value

  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 124, in __exit__
    next(self.gen)

  File "/python3.9/site-packages/traitlets/traitlets.py", line 1191, in hold_trait_notifications
    value = trait._cross_validate(self, getattr(self, name))

  File "/python3.9/site-packages/traitlets/traitlets.py", line 618, in _cross_validate
    value = obj._trait_validators[self.name](obj, proposal)

  File "/python3.9/site-packages/traitlets/traitlets.py", line 975, in __call__
    return self.func(*args, **kwargs)

  File "/python3.9/site-packages/ipydatagrid/cellrenderer.py", line 33, in _validate_value
    return py2vega(

  File "/python3.9/site-packages/py2vega/main.py", line 354, in py2vega
    return VegaExpressionVisitor(whitelist, scope).visit(func.body[-1])

  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)

  File "/python3.9/site-packages/py2vega/main.py", line 133, in visit_Return
    return self.visit(node.value)

  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)

  File "/python3.9/site-packages/py2vega/main.py", line 251, in visit_IfExp
    self.visit(node.test),

  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)

  File "/python3.9/site-packages/py2vega/main.py", line 261, in visit_Compare
    left_operand = self._visit_binop_impl(left_operand, node.ops[idx], node.comparators[idx])

  File "/python3.9/site-packages/py2vega/main.py", line 227, in _visit_binop_impl
    left = left_node if isinstance(left_node, str) else self.visit(left_node)

  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)

  File "/python3.9/site-packages/py2vega/main.py", line 323, in visit_Subscript
    raise Py2VegaSyntaxError('Unsupported {} node'.format(node.slice.__class__.__name__))

  File "<string>", line unknown
Py2VegaSyntaxError: Unsupported Constant node, note that only a subset of Python is supported

Python 3.9 deprecation warnings

DeprecationWarning: visit_NameConstant is deprecated; add visit_Constant
DeprecationWarning: visit_Num is deprecated; add visit_Constant
DeprecationWarning: visit_Str is deprecated; add visit_Constant

Py2Vega Name Error with variables defined outside function scope

    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?

Assignment

We should use beniget for assignments and forward substitution. By using beniget we could allow this kind of functions:

def foo(value):
    if value < 3:
        b = 3
    else:
        b = 4
    return b

This should turn into an equivalent of value < 3 ? 3 : 4

Built-in functions

The Python built-in len function could be mapped to the Vega length function
The Python built-in slice function could be mapped to the Vega slice function
The Python built-in str function could be mapped to the Vega toString function
...

kwargs support

We should add kwargs support in function calls by mapping the kwarg to the argument position

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.