Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
page = Page()
test_data = {'data_key': 'value1'}
test_props = {'props_key': 'value2'}
style = {'s1': 'v1'}
test_id = 'test_id'
class TestElement(view.Element):
def _init(self):
self.update_data(test_data)
self.update_props(test_props)
class TestElement2(view.Element):
pass
class TestElement4(view.Element):
def _init(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
assert page.root is page
assert page.root_id == 'root'
assert page.index == 0
assert page._version == 0
test_element = page._new_child(TestElement, style=style)
assert page._version == 2
test_element2 = page._new_child(TestElement2)
test_element3 = page._new_child(TestElement, id=test_id)
test_element4 = test_element3._new_child(TestElement4, arg1='val1', arg2='val2')
all_elements = [test_element, test_element2, test_element3, test_element4]
def test_new_prop():
page = Page()
class TestElement(view.Element):
pass
element = page._new_child(TestElement)
element.update_prop('other_prop', 'value')
prop1 = element.new_prop('prop1')
text1 = prop1.new_text('text1')
card1 = prop1.new_card('card1')
for e in [prop1, text1, card1]:
assert e.root is prop1
assert e.root_id == prop1.id
with pytest.raises(AssertionError):
element.new_prop('prop1')
with pytest.raises(AssertionError):
element.new_prop('other_prop')
def _add_row(self, row, action):
row_data = self._row_data(row)
if action == 'append':
self.data['rows'].append(row_data)
else:
self.data['rows'].appendleft(row_data)
self.update_element(path=['data', 'rows'], action=action, data=row_data)
def _row_data(self, row, offset=0):
if isinstance(row, dict):
row = [row[h] for h in self.data['headers']]
return {'data': row, 'id': len(self.data['rows']) + 1 + offset}
@builtin
class Button(Element):
allow_children = False
def _init(self, function, text='', icon=None, shape=None, type='default', block=False):
assert (not shape) or shape == 'circle'
self._function = function
self._register(function, self.id)
self.update_props({
'icon': icon,
'shape': shape,
'type': type,
'block': block
}, override=False)
text = text or ('' if shape else function.__name__)
self.update_data({'text': text})
assert (not two_tone_color) or theme == 'twoTone'
self.update_props({
'type': type,
'theme': theme,
'spin': spin,
'twoToneColor': two_tone_color
}, override=False)
@builtin
class Inline(Text):
allow_children = True
@builtin
class Markdown(Element):
allow_children = False
def _init(self, source):
self.update_props({'source': source}, override=False)
def _new_child(self, cls, **kwargs):
assert issubclass(cls, Tab)
return super(Tabs, self)._new_child(cls, **kwargs)
def new_tab(self, name, **kwargs):
"""
Add a tab child.
:param name: The name of the tab.
:return: The created tab element.
"""
return self._new_child(Tab, name=name, **kwargs)
@builtin
class Tab(Element):
def _init(self, name):
self.update_props({'tab': name}, override=False)
@builtin
class Icon(Element):
def _init(self, type, theme='outlined', spin=False, two_tone_color=None):
assert theme in ['outlined', 'filled', 'twoTone']
assert (not two_tone_color) or theme == 'twoTone'
self.update_props({
'type': type,
'theme': theme,
'spin': spin,
'twoToneColor': two_tone_color
series_data = current_chart.setdefault(series_key, [])
series_data.append((now, value))
transformers = {t.key: t for t in [
NoOpTransformer(),
NumberSequenceTransformer(),
]}
transformer_classes = {t.key: t for t in [
FlatDictTransformer
]}
@builtin
class Chart(Element):
allow_children = False
_transformer = None
def _init(self, data=None, options=None, transform=None, moving_window=None):
self.transformer = transform
self.update_data({
'data': self.transformer.transform(data),
'options': options or {},
'movingWindow': moving_window
})
def add(self, data):
"""
Add new data to a chart after it has been created.
def _init(self, header):
if header:
if isinstance(header, Element):
assert header.root_id != self.root_id
header = header.root
self.new_prop('header', header)
else:
self.update_props({'header': header}, override=False)
self.header = header
else:
self.header = self.new_prop('header')
def text(self, value):
"""
Set the text value.
:param value: The new text.
"""
self.update_data({'text': value or ''})
@builtin
class Card(Text):
allow_children = True
@builtin
class Table(Element):
allow_children = False
def _init(self, headers, page_size=None):
if isinstance(headers, dict):
headers = list(headers.keys())
self.update_data({'headers': headers, 'rows': deque()})
self.update_props({
'size': 'small',
'pagination': {'pageSize': page_size, 'position': 'top'} if page_size else False
}, override=False)
def clear(self):
"""
Clear all table rows.
"""
def _parse(self, obj, context):
return self.root._parse(obj, context)
def _stack_stash(self):
self._stack.append(self)
return self
def _stack_pop(self):
return self._stack.pop()
def _stack_next(self):
return self._stack[-1]
class Root(Element):
def __init__(self, owner, element_id=None):
super(Root, self).__init__(
root=self,
parent=None,
element_id=element_id,
props=None,
style=None,
stack=[self]
)
self._owner = owner # type: Element
def _get_view(self):
return [t._get_view() for t in self.children]
def _increase_version(self):