I am not able to reproduce the exact output of the content
-property of the dcc.Upload component.
If I upload the file my_excel.xlsx
to the dcc.Upload component, my callback-function receives a "base64 encoded string" (according to the dcc.Upload documentation). I don’t know how to reproduce the exact same string without the dcc.Upload component (I want to use the Output for Unit Tests).
my current approach:
import base64
import io
import pandas as pd
# This is what I try to reproduce the output of the dcc.Upload Component
with open('tests/data/my_excel.xlsx', 'rb') as file:
raw_data = file.read()
# raw_data is the output I receive from the dcc.Upload Component
# these steps are raise no Error with the output of dcc.Upload
_, content_string = raw_data.split(',') # this Fails
decoded = base64.b64decode(content_string)
df = pd.read_excel(io.BytesIO(decoded))
I get the error TypeError: a bytes-like object is required, not 'str'
.
if I add
raw_data = base64.b64encode(raw_data)
before the raw_data.split(',')
, I get the same error.
How do I get the exact same "base64 encoded string" without the dcc.Upload Component?