File Upload w/ Drag & Drop

Aha – looks like this app was broken by the transition from Python 2 to Python 3, which draws a distinction between (text) strings and (data) bytestrings. The JS is sending the (data) contents of the file as a (text) string, and the Python is getting confused. In the absence of a more pleasant way to handle Python bytes objects in JS, here’s the Python code to translate. Put this at the top of @shaun’s drag_drop_upload() function (in the custom component):

    # JS sent this data over as a "byte string", ie a string whose codepoints match the bytes in the file
    # Transfrom from a  it into a byte string for Python 3
    data = bytes([ord(c) for c in data])
1 Like