import base64


def data_url(data_comp, fmt):
    """Assemble compressed image data into URL data string
    """
    data_encode = base64.b64encode(data_comp)

    encoding = 'utf-8'
    template = 'data:image/{:s};charset={};base64,{:s}'

    # The decoding step here is necesary since we need to interpret byte data as text.
    # See this link for a nice explanation:
    # http://stackoverflow.com/questions/14010551/how-to-convert-between-bytes-and-strings-in-python-3
    result = template.format(fmt, encoding, data_encode.decode(encoding=encoding))

    return result