3

I am developing desktop software based on:

  • Python: PySide (WebKit)
  • JavaScript: AngularJS

Now, I need implement async feature:

  • while uploading images from FTP (Python part) I need to show some uploader on UI (HTML+JS)

Need your advices about the elegant solution.

Very simple solution:

  1. save some flags in Python dictionary
  2. request value of this flags in JS by timer :)

However, I need to hard code the time and once I have long operation such as downloading files from FTP, its issue for me

Code of the solution:

templateManager.js

function _loadNewTemplates() {
        var deferred = $.Deferred();

        asyncManager.run($.proxy( _loadNewTemplatesCallback, null, deferred ), "get_new_templates_list" );

        return deferred;
    }

    function _loadNewTemplatesCallback ( deferred, response ) {
        template.newList = response.result;
        deferred.resolve();
    }

app_mamager.py

    @QtCore.Slot(int, result=str)
    def get_new_templates_list(self, id):
       thread.start_new_thread(get_new_templates_list,
                                    (id,))

utils.py

def get_new_templates_list(id):
   config.set_response(id, json.dumps({'result': _get_new_templates_list(), 'error': None}, MyEncoder))


def _get_new_templates_list():
    request = {'category': PRODUCT_CODE}
    response = requests.post(URL,
                             data=request,
                             headers=HEADERS)
    return json.loads(response.text)

config.py

def set_response(id, request):
    '''
    Put pair (id, request) to the internal dictionary
    :param id - int, request id:
    :param request - string, request string:
    :return:
    '''
    global _async_request
    _async_request[id] = request

def get_response(id):
    '''
    Returns request or empty string if request does not exist
    :param id - int
    :return - string, request string:
    '''
    global _async_request
    if _async_request.has_key(id):
        return _async_request.pop(id)
    else:
        return ''
2
  • there is no part in the question that shows that you have tried to code up something - see stackoverflow.com/help/how-to-ask Commented Aug 26, 2015 at 6:23
  • Any updates? I am interested in resolving the same issue Commented Jun 29, 2016 at 17:38

1 Answer 1

2
+25

Here is what you can do:

  1. retrieve the file size with stat / stat.ST_SIZE,
  2. decide on a chunk size,
  3. call PySide.QtGui.QProgressBar.setRange() with the correct range, which is 0 to number of blocks ( filesize / ftp transfer chunk size ) for binary files or number of lines for text files.
  4. pass a callback to ftplib.FTP.storbinary() or ftplib.FTP.storlines() so that each block / line transferred increases your progress bar accordingly with PySide.QtGui.QProgressBar.setValue()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I am Ok with simple loader.gif without progress and I am looking for the best flexible solution to communicate between JS and Python in async mode. Becuase I think (maybe I am wrong) my approach above is not so good as I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.