quantstack / py2vega Goto Github PK
View Code? Open in Web Editor NEWLicense: BSD 3-Clause "New" or "Revised" License
License: BSD 3-Clause "New" or "Revised" License
We could automatically generate mocking vega functions out of a spec vega/vega#1938
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"
a simple function like this would not work as it returns indentation error
def make_fn(fmt):
def fn(cell):
return cell.value if not isNaN(cell.value) else format(cell.value, fmt)
return fn
Expr(make_fn('.2f'))
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
DeprecationWarning: visit_NameConstant is deprecated; add visit_Constant
DeprecationWarning: visit_Num is deprecated; add visit_Constant
DeprecationWarning: visit_Str is deprecated; add visit_Constant
We could map the Slice
node to a Vega slice
function call
We're seeing issues on test suites in the ipydatagrid
repo when using Python 3.9.
The error seems to originate in py2vega
: "Unsupported Constant node, note that only a subset of Python is supported".
Trace available below:
https://github.com/bloomberg/ipydatagrid/pull/191/checks?check_run_id=1991433011
@martinRenou perhaps you could point me in the right direction? Thanks :)
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?
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
Most of the Vega functions are actually not available, they throw Error: Unrecognized function
. See vega/vega#1939. We should comment out those functions until this is fixed.
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
...
As pointed out by @serge-sans-paille, it might be better to use SyntaxError or a custom Py2VegaSyntaxError class instead of RuntimeError
We should add kwargs
support in function calls by mapping the kwarg to the argument position
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.