Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symmetry betwee PyWidget and Widget #698

Merged
merged 18 commits into from Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion flexx/app/_component2.py
Expand Up @@ -353,7 +353,9 @@ def _comp_init_property_values(self, property_values):
# This is a good time to register with the session, and
# instantiate the proxy class. Property values have been set at this
# point, but init() has not yet been called.

# Property values must be poped when consumed so that the remainer is used for
# instantiation of the Widget

# Keep track of what events are registered at the proxy
self.__event_types_at_proxy = []

Expand Down
2 changes: 1 addition & 1 deletion flexx/app/_flaskserver.py
Expand Up @@ -158,7 +158,7 @@ async def _thread_switch():
"""
while True:
time.sleep(0)
await asyncio.sleep(0)
await asyncio.sleep(1e-9) # any number above 0 will keep low CPU usage

def start(self):
# Register blueprints for all apps:
Expand Down
19 changes: 15 additions & 4 deletions flexx/event/_component.py
Expand Up @@ -203,9 +203,11 @@ def __init__(self, *init_args, **property_values):
for name in self.__properties__:
self.__handlers.setdefault(name, [])

# With self as the active component (and thus mutatable), init the
# With self as the active component (and thus mutable), init the
# values of all properties, and apply user-defined initialization
with self:
# This call will pop some of the consumed property values
# will remain properties used for the DOM component initialisation
self._comp_init_property_values(property_values)
self.init(*init_args)

Expand All @@ -218,6 +220,8 @@ def __repr__(self):
def _comp_init_property_values(self, property_values):
""" Initialize property values, combining given kwargs (in order)
and default values.
Property values are popped when consumed so that the remainer is used for
other initialisations without mixup.
"""
values = []
# First collect default property values (they come first)
Expand All @@ -227,18 +231,25 @@ def _comp_init_property_values(self, property_values):
if name not in property_values:
values.append((name, prop._default))
# Then collect user-provided values
for name, value in property_values.items(): # is sorted by occurance in py36
for name, value in list(property_values.items()): # is sorted by occurance in py36
if name not in self.__properties__:
if name in self.__attributes__:
raise AttributeError('%s.%s is an attribute, not a property' %
(self._id, name))
else:
raise AttributeError('%s does not have property %s.' %
# if the proxy instance does not exist, we want the attribute
# to be passed through to the Widget instantiation.
# No exception if the proxy does not exists.
if self._has_proxy is True:
raise AttributeError('%s does not have property %s.' %
(self._id, name))
if callable(value):
self._comp_make_implicit_setter(name, value)
property_values.pop(name)
continue
values.append((name, value))
if name in self.__properties__:
values.append((name, value))
property_values.pop(name)
# Then process all property values
self._comp_apply_property_values(values)

Expand Down
11 changes: 8 additions & 3 deletions flexx/ui/_widget.py
Expand Up @@ -1158,17 +1158,22 @@ def _comp_init_property_values(self, property_values):
# when this is the active component, and after the original
# version of this has been called, everything related to session
# etc. will work fine.

# Property values are poped when consumed so that the remainer is used for
# instantiation of the js Widget

# First extract the kwargs
kwargs_for_real_widget = {}
for name in list(property_values.keys()):
if name not in self.__properties__:
if name in self._WidgetCls.__properties__:
kwargs_for_real_widget[name] = property_values.pop(name)
# Call original version, sets _session, amongst other things
# it also pops the consumed property values so the remainer
# can be used to initialize the js _WidgetCls
super()._comp_init_property_values(property_values)
# Create widget and activate it
w = self._WidgetCls(**kwargs_for_real_widget)
# Create widget and activate it - at this point, all property values
# should be intended for the js Widget.
w = self._WidgetCls(**kwargs_for_real_widget, **property_values)
self.__exit__(None, None, None)
self._jswidget = w
self.__enter__()
Expand Down
2 changes: 1 addition & 1 deletion flexxamples/howtos/bokehdemo.py
Expand Up @@ -35,7 +35,7 @@ def init(self):
self.plot1 = flx.BokehWidget.from_plot(p1, title='Scatter')
with flx.VFix(title='Sine'):
Controls()
with flx.Widget(style='overflow-y:auto;', flex=1):
with flx.PyWidget(style='overflow-y:auto;', flex=1):
self.plot2 = flx.BokehWidget.from_plot(p2)
self.plot3 = flx.BokehWidget.from_plot(p3)

Expand Down