Skip to content

Commit 9a90a96

Browse files
committed
FileUploader.onprogress event. Issue #534
1 parent 4be71eb commit 9a90a96

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

examples/progress_bar_app.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
"""
14+
15+
import remi.gui as gui
16+
from remi import start, App
17+
18+
19+
class MyApp(App):
20+
21+
def main(self):
22+
# creating a container VBox type, vertical (you can use also HBox or Widget)
23+
main_container = gui.VBox(width=300, height=200, style={'margin': '0px auto'})
24+
25+
# creating a progress bar and appending it to the main layout
26+
self.progress = gui.Progress(0,100)
27+
main_container.append(self.progress)
28+
29+
# creating the file uploader
30+
self.file_uploader = gui.FileUploader()
31+
main_container.append(self.file_uploader)
32+
33+
# linking events
34+
self.file_uploader.onprogress.do(self.onprogress_listener)
35+
self.file_uploader.onsuccess.do(self.fileupload_on_success)
36+
37+
# returning the root widget
38+
return main_container
39+
40+
def onprogress_listener(self, emitter, filename, loaded, total):
41+
self.progress.set_value(loaded*100.0/total)
42+
print(filename, loaded, total)
43+
44+
def fileupload_on_success(self, emitter, filename):
45+
print('File upload success: ' + filename)
46+
47+
48+
if __name__ == "__main__":
49+
# starts the webserver
50+
start(MyApp, address='0.0.0.0', port=0, start_browser=True, username=None, password=None)

remi/gui.py

+24
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,18 @@ def set_internal_js(self, app_identifier, net_interface_ip, pending_messages_que
15821582
Remi.prototype.uploadFile = function(widgetID, eventSuccess, eventFail, eventData, file){
15831583
var url = '/';
15841584
var xhr = new XMLHttpRequest();
1585+
xhr.upload.addEventListener('progress', function(e) {
1586+
console.log('progress!', widgetID, %(emitter_identifier)s, e.loaded, e.total);
1587+
if(event.lengthComputable){
1588+
var params={};
1589+
params['filename'] = 'filename'/* file.name*/;
1590+
params['loaded'] = event.loaded;
1591+
params['total'] = event.total;
1592+
console.log("length is computable; sending callback");
1593+
remi.sendCallbackParam(widgetID,'onprogress',params);
1594+
}
1595+
});
1596+
15851597
var fd = new FormData();
15861598
xhr.open('POST', url, true);
15871599
xhr.setRequestHeader('filename', file.name);
@@ -1599,6 +1611,7 @@ def set_internal_js(self, app_identifier, net_interface_ip, pending_messages_que
15991611
console.log('upload failed: ' + file.name);
16001612
}
16011613
};
1614+
16021615
fd.append('upload_file', file);
16031616
xhr.send(fd);
16041617
};
@@ -4120,6 +4133,17 @@ def ondata(self, filedata, filename):
41204133
f.write(filedata)
41214134
return (filedata, filename)
41224135

4136+
@decorate_set_on_listener("(self, emitter, filename, loaded, total)")
4137+
@decorate_event
4138+
def onprogress(self, filename, loaded, total):
4139+
"""
4140+
Args:
4141+
filename (str): the file name that is uploading
4142+
loaded (int): loaded bytes
4143+
total (int): file size in bytes
4144+
"""
4145+
return (filename, int(loaded), int(total))
4146+
41234147

41244148
class FileDownloader(Container, _MixinTextualWidget):
41254149
"""FileDownloader widget. Allows to start a file download."""

0 commit comments

Comments
 (0)