File upload zip?

Is it possible for user to upload zip file and save it?

Yes, with the FileInput widget.

getting error when trying to save the upload zip to disk.

import panel as pn

file_input = pn.widgets.FileInput(accept=’.zip’)

if file_input.value is not None:

file_input.save(r'C:\Users\username\Downloads\test.zip')

app = pn.Row(file_input.controls(jslink=True), file_input)

app.show()

TypeError: Object of type bytes is not JSON serializable
ERROR:tornado.application:Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x0000011FDACA4088>>, <Task finished coro=<_needs_document_lock.._needs_document_lock_wrapper() done, defined at C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\bokeh\server\session.py:77> exception=TypeError(‘Object of type bytes is not JSON serializable’)>)
Traceback (most recent call last):
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\tornado\ioloop.py”, line 741, in _run_callback
ret = callback()
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\tornado\ioloop.py”, line 765, in _discard_future_result
future.result()
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\bokeh\server\session.py”, line 97, in _needs_document_lock_wrapper
result = await result
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\reactive.py”, line 356, in _change_coroutine
self._change_event(doc)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\reactive.py”, line 370, in _change_event
self._process_events(events)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\reactive.py”, line 315, in process_events
self.param.update(**self_events)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 1898, in update
self
._batch_call_watchers()
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 2059, in batch_call_watchers
self
._execute_watcher(watcher, events)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 2021, in execute_watcher
watcher.fn(*args, **kwargs)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\param.py”, line 559, in link
widget.param.update(**updates)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 1898, in update
self
._batch_call_watchers()
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 2059, in batch_call_watchers
self
._execute_watcher(watcher, events)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\param\parameterized.py”, line 2021, in _execute_watcher
watcher.fn(*args, **kwargs)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\reactive.py”, line 294, in _param_change
msg = self._process_param_change({event.name: event.new})
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\site-packages\panel\widgets\input.py”, line 701, in process_param_change
value = json.dumps(value)
File "C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\json_init
.py", line 231, in dumps
return _default_encoder.encode(obj)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\json\encoder.py”, line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\json\encoder.py”, line 257, in iterencode
return _iterencode(o, 0)
File “C:\Users\agriffin\AppData\Local\ESRI\conda\envs\arcgispro-py3-pyviz\lib\json\encoder.py”, line 179, in default
raise TypeError(f’Object of type {o.class.name} ’
TypeError: Object of type bytes is not JSON serializable

Hi @Andre

The error message is caused by adding the file_input.controls(jslink=True). The file_input.controls cannot display a byte object (the file) because its not json serializable. Feel free to file a bug report.

But the below works

import panel as pn

file_input = pn.widgets.FileInput(accept=".zip")

@pn.depends(file_input, watch=True)
def save(value):
    if file_input.value is not None:
        file_input.save('test.zip')
        print("saved")
    else:
        print("nothing to save")

pn.Column(file_input).servable()