Skip to content

Commit 24591b4

Browse files
committed
Updated loading_widget.
1 parent f943300 commit 24591b4

5 files changed

Lines changed: 94 additions & 73 deletions

File tree

examples/examples_from_contributors/bootstrap.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818

1919
class MyApp(App):
2020
def __init__(self, *args):
21+
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
22+
#static_file_path can be an array of strings allowing to define
23+
# multiple resource path in where the resources will be placed
24+
super(MyApp, self).__init__(*args, static_file_path=res_path)
25+
26+
def idle(self):
27+
#idle loop, you can place here custom code
28+
# avoid to use infinite iterations, it would stop gui update
29+
pass
30+
31+
def main(self):
2132
#custom additional html head tags
2233
my_html_head = """<title>Bootstrap Test</title>"""
2334

@@ -38,18 +49,11 @@ def __init__(self, *args):
3849
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
3950
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
4051
"""
52+
#appending elements to page header
53+
self.page.children['head'].add_child('myhtml', my_html_head)
54+
self.page.children['head'].add_child('mycss', my_css_head)
55+
self.page.children['head'].add_child('myjs', my_js_head)
4156

42-
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
43-
#static_file_path can be an array of strings allowing to define
44-
# multiple resource path in where the resources will be placed
45-
super(MyApp, self).__init__(*args, static_file_path=res_path, html_head=my_html_head, css_head=my_css_head, js_head=my_js_head)
46-
47-
def idle(self):
48-
#idle loop, you can place here custom code
49-
# avoid to use infinite iterations, it would stop gui update
50-
pass
51-
52-
def main(self):
5357
#creating a container VBox type, vertical (you can use also HBox or Widget)
5458
main_container = gui.VBox(width='500px', height='500px', style={'margin':'0px auto','padding':'10px'})
5559

@@ -105,6 +109,6 @@ def main(self):
105109

106110
if __name__ == "__main__":
107111
# starts the webserver
108-
start(MyApp, address='127.0.0.1', port=8085, start_browser=True, username=None, password=None)
112+
start(MyApp, debug=True, address='127.0.0.1', port=8085, start_browser=True, username=None, password=None)
109113

110114

examples/page_internals_app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ def main(self):
2828
main_container = gui.VBox(width=300, height=200, style={'margin':'0px auto'})
2929
self.page.children['head'].add_child('test', '<meta patate>')
3030
self.page.children['body'].style['background-color'] = 'lightyellow'
31+
self.page.children['body'].onkeydown.connect(self.onkeydown)
3132
# returning the root widget
3233
return main_container
3334

35+
def onkeydown(self, emitter, key, keycode, ctrl, shift, alt):
36+
print("keydown: %s"%key)
37+
3438
def onload(self, emitter):
3539
print(">>>>>>>>> ON PAGE LOADED")
3640

examples/template_advanced_app.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818

1919
class MyApp(App):
2020
def __init__(self, *args):
21+
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
22+
#static_file_path can be an array of strings allowing to define
23+
# multiple resource path in where the resources will be placed
24+
super(MyApp, self).__init__(*args, static_file_path=res_path)
25+
26+
def idle(self):
27+
""" Idle loop, you can place here custom code,
28+
avoid to use infinite iterations, it would stop gui update.
29+
This is a Thread safe method where you can update the
30+
gui with information from external Threads.
31+
"""
32+
pass
33+
34+
def main(self):
2135
#custom additional html head tags
2236
my_html_head = """
2337
"""
@@ -31,21 +45,15 @@ def __init__(self, *args):
3145
my_js_head = """
3246
<script></script>
3347
"""
48+
#appending elements to page header
49+
self.page.children['head'].add_child('myhtml', my_html_head)
50+
self.page.children['head'].add_child('mycss', my_css_head)
51+
self.page.children['head'].add_child('myjs', my_js_head)
52+
#eventually set up body attributes/style
53+
#self.page.children['body'].style['background-color'] = 'lightyellow'
54+
#eventually set up body event listeners
55+
#self.page.children['body'].onkeydown.connect(self.onkeydown)
3456

35-
res_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res')
36-
#static_file_path can be an array of strings allowing to define
37-
# multiple resource path in where the resources will be placed
38-
super(MyApp, self).__init__(*args, static_file_path=res_path, html_head=my_html_head, css_head=my_css_head, js_head=my_js_head)
39-
40-
def idle(self):
41-
""" Idle loop, you can place here custom code,
42-
avoid to use infinite iterations, it would stop gui update.
43-
This is a Thread safe method where you can update the
44-
gui with information from external Threads.
45-
"""
46-
pass
47-
48-
def main(self):
4957
#creating a container VBox type, vertical (you can use also HBox or Widget)
5058
main_container = gui.VBox(width=300, height=200, style={'margin':'0px auto'})
5159

@@ -54,11 +62,34 @@ def main(self):
5462

5563
def on_close(self):
5664
""" Overloading App.on_close event allows to perform some
57-
activities before app termination.
58-
"""
65+
activities before app termination. """
5966
super(MyApp, self).on_close()
6067

68+
def onload(self, emitter):
69+
""" WebPage Event that occurs on webpage loaded """
70+
super(MyApp, self).onload(emitter)
71+
72+
def onerror(self, emitter, message, source, lineno, colno):
73+
""" WebPage Event that occurs on webpage errors """
74+
super(MyApp, self).onerror(emitter, message, source, lineno, colno)
75+
76+
def ononline(self, emitter):
77+
""" WebPage Event that occurs on webpage goes online after a disconnection """
78+
super(MyApp, self).ononline(emitter)
79+
80+
def onpagehide(self, emitter):
81+
""" WebPage Event that occurs on webpage when the user navigates away """
82+
super(MyApp, self).onpagehide(emitter)
83+
84+
def onpageshow(self, emitter):
85+
""" WebPage Event that occurs on webpage gets shown """
86+
super(MyApp, self).onpageshow(emitter)
87+
88+
def onresize(self, emitter, width, height):
89+
""" WebPage Event that occurs on webpage gets resized """
90+
super(MyApp, self).onresize(emitter, width, height)
91+
6192

6293
if __name__ == "__main__":
6394
# starts the webserver
64-
start(MyApp, address='0.0.0.0', port=0, start_browser=True, username=None, password=None)
95+
start(MyApp, debug=True, address='0.0.0.0', port=0, start_browser=True, username=None, password=None)

remi/gui.py

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ def set_identifier(self, new_identifier):
296296
self.attributes['id'] = new_identifier
297297
runtimeInstances[new_identifier] = self
298298

299+
def innerHTML(self, local_changed_widgets):
300+
ret = ''
301+
for k in self._render_children_list:
302+
s = self.children[k]
303+
if isinstance(s, Tag):
304+
ret = ret + s.repr(local_changed_widgets)
305+
elif isinstance(s, type('')):
306+
ret = ret + s
307+
elif isinstance(s, type(u'')):
308+
ret = ret + s.encode('utf-8')
309+
else:
310+
ret = ret + repr(s)
311+
ret += '\n'
312+
return ret
313+
299314
def repr(self, changed_widgets=None):
300315
"""It is used to automatically represent the object to HTML format
301316
packs all the attributes, children and so on.
@@ -307,23 +322,13 @@ def repr(self, changed_widgets=None):
307322
if changed_widgets is None:
308323
changed_widgets = {}
309324
local_changed_widgets = {}
310-
innerHTML = ''
311-
for k in self._render_children_list:
312-
s = self.children[k]
313-
if isinstance(s, Tag):
314-
innerHTML = innerHTML + s.repr(local_changed_widgets)
315-
elif isinstance(s, type('')):
316-
innerHTML = innerHTML + s
317-
elif isinstance(s, type(u'')):
318-
innerHTML = innerHTML + s.encode('utf-8')
319-
else:
320-
innerHTML = innerHTML + repr(s)
325+
_innerHTML = self.innerHTML(local_changed_widgets)
321326

322327
if self._ischanged() or ( len(local_changed_widgets) > 0 ):
323328
self._backup_repr = ''.join(('<', self.type, ' ', self._repr_attributes, '>',
324-
innerHTML, '</', self.type, '>'))
329+
_innerHTML, '</', self.type, '>'))
325330
#faster but unsupported before python3.6
326-
#self._backup_repr = f'<{self.type} {self._repr_attributes}>{innerHTML}</{self.type}>'
331+
#self._backup_repr = f'<{self.type} {self._repr_attributes}>{_innerHTML}</{self.type}>'
327332
if self._ischanged():
328333
# if self changed, no matter about the children because will be updated the entire parent
329334
# and so local_changed_widgets is not merged
@@ -957,19 +962,7 @@ def repr(self, changed_widgets=None):
957962
if changed_widgets is None:
958963
changed_widgets={}
959964
local_changed_widgets = {}
960-
innerHTML = ''
961-
for k in self._render_children_list:
962-
s = self.children[k]
963-
if isinstance(s, Tag):
964-
innerHTML = innerHTML + s.repr(local_changed_widgets)
965-
elif isinstance(s, type('')):
966-
innerHTML = innerHTML + s
967-
elif isinstance(s, type(u'')):
968-
innerHTML = innerHTML + s.encode('utf-8')
969-
else:
970-
innerHTML = innerHTML + repr(s)
971-
innerHTML += '\n'
972-
return ''.join(('<', self.type, '>\n', innerHTML, '\n</', self.type, '>'))
965+
return ''.join(('<', self.type, '>\n', self.innerHTML(local_changed_widgets), '\n</', self.type, '>'))
973966

974967

975968
class HEAD(Tag):
@@ -1040,8 +1033,7 @@ def __init__(self, title, net_interface_ip, pending_messages_queue_length, webso
10401033
/*var idRootNodeWidget = received_msg.substr(0,index-1);*/
10411034
var content = received_msg.substr(index,received_msg.length-index);
10421035
1043-
document.body.innerHTML = '<div id="loading" style="display: none;"><div id="loading-animation"></div></div>';
1044-
document.body.innerHTML += decodeURIComponent(content);
1036+
document.body.innerHTML = decodeURIComponent(content);
10451037
}else if( received_msg[0]=='1' ){ /*update_widget*/
10461038
var focusedElement=-1;
10471039
var caretStart=-1;
@@ -1248,19 +1240,7 @@ def repr(self, changed_widgets=None):
12481240
if changed_widgets is None:
12491241
changed_widgets={}
12501242
local_changed_widgets = {}
1251-
innerHTML = ''
1252-
for k in self._render_children_list:
1253-
s = self.children[k]
1254-
if isinstance(s, Tag):
1255-
innerHTML = innerHTML + s.repr(local_changed_widgets)
1256-
elif isinstance(s, type('')):
1257-
innerHTML = innerHTML + s
1258-
elif isinstance(s, type(u'')):
1259-
innerHTML = innerHTML + s.encode('utf-8')
1260-
else:
1261-
innerHTML = innerHTML + repr(s)
1262-
innerHTML += '\n'
1263-
return ''.join(('<', self.type, '>\n', innerHTML, '\n</', self.type, '>'))
1243+
return ''.join(('<', self.type, '>\n', self.innerHTML(local_changed_widgets), '\n</', self.type, '>'))
12641244

12651245

12661246
class BODY(Widget):
@@ -1274,8 +1254,10 @@ class BODY(Widget):
12741254
def __init__(self, *args, **kwargs):
12751255
super(BODY, self).__init__(*args, _type='body', **kwargs)
12761256
loading_anim = Widget()
1257+
del loading_anim.style['margin']
12771258
loading_anim.set_identifier("loading-animation")
1278-
loading_widget = Widget(children=[loading_anim])
1259+
loading_widget = Widget(children=[loading_anim], style={'display':'none'})
1260+
del loading_widget.style['margin']
12791261
loading_widget.set_identifier("loading")
12801262

12811263
self.append(loading_widget)

remi/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def do_gui_update(self):
436436

437437
def websocket_handshake_done(self, ws_instance_to_update):
438438
with self.update_lock:
439-
msg = "0" + self.root.identifier + ',' + to_websocket(self.root.repr())
439+
msg = "0" + self.root.identifier + ',' + to_websocket(self.page.children['body'].innerHTML({}))
440440
ws_instance_to_update.send_message(msg)
441441

442442
def set_root_widget(self, widget):
@@ -447,7 +447,7 @@ def set_root_widget(self, widget):
447447
self.root._parent = self
448448
self.root.enable_refresh()
449449

450-
msg = "0" + self.root.identifier + ',' + to_websocket(self.root.repr())
450+
msg = "0" + self.root.identifier + ',' + to_websocket(self.page.children['body'].innerHTML({}))
451451
self._send_spontaneous_websocket_message(msg)
452452

453453
def _send_spontaneous_websocket_message(self, message):

0 commit comments

Comments
 (0)