question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File_uploader widget is problematic

See original GitHub issue

Summary

I tried to make an example answer for Discourse 1445 but found out its difficult to use the file_uploader widget as soon as its part of an interactive application where the user may wish to upload multiple files, interact with several widgets after file upload or clear the cache.

  • You can only upload one file at the time.
  • You get no additional information on the file like name, size, upload time, type etc. So you cannot distinguish the files without reading the content. The python object id changes for each script rerun so you cannot use that either.
  • every time you interact with any widget, the script is rerun and you risk processing or storing the file again!
  • The file uploader widget is not cleared when you clear the cache and there is no way to clear the file uploader widget programmatically.

See also https://discuss.streamlit.io/t/awesome-streamlit-org-change-log/1414/7?u=marc

# pylint: disable=line-too-long
"""This is example shows how to **upload multiple files** via the
[File Uploader Widget](https://streamlit.io/docs/api.html?highlight=file%20upload#streamlit.file_uploader)

As far as I can see you can only upload one file at a time. So if you need multiple files in your
app, you need to store them in a static List or Dictionary. Alternatively they should be uploaded
as one .zip file.

Please note that file uploader is a **bit problematic** because
- You can only upload one file at the time.
- You get no additional information on the file like name, size, upload time, type etc. So you
cannot distinguish the files without reading the content.
- every time you interact with any widget, the script is rerun and you risk processing or storing
the file again!
- The file uploader widget is not cleared when you clear the cache and there is no way to clear the
file uploader widget programmatically.

This example was based on
[Discourse 1445](https://discuss.streamlit.io/t/uploading-multiple-files-with-file-uploader/1445)
"""
# pylint: enable=line-too-long
from typing import Dict

import streamlit as st


@st.cache(allow_output_mutation=True)
def get_static_store() -> Dict:
    """This dictionary is initialized once and can be used to store the files uploaded"""
    return {}


def main():
    """Run this function to run the app"""
    static_store = get_static_store()

    st.info(__doc__)
    result = st.file_uploader("Upload", type="py")
    if result:
        # Process you file here
        value = result.getvalue()

        # And add it to the static_store if not already in
        if not value in static_store.values():
            static_store[result] = value
    else:
        static_store.clear()  # Hack to clear list if the user clears the cache and reloads the page
        st.info("Upload one or more `.py` files.")

    if st.button("Clear file list"):
        static_store.clear()
    if st.checkbox("Show file list?", True):
        st.write(list(static_store.keys()))
    if st.checkbox("Show content of files?"):
        for value in static_store.values():
            st.code(value)


main()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
starrabb1tcommented, Jan 30, 2020

Is there any hacks to clear uploader programmatically now?

3reactions
RichardOberdieckcommented, Feb 12, 2020

I’m having a problem with the fact that the file has to be loaded at every event. The underlying issue seems to be that in order to compute the hash and therefore understand whether the file should be loaded in again, it has to load it in the first place.

Especially for larger files this is really an issue, and makes applications quite annoying to use. My suggestion is (a) enable using the filename as the hash, and compare that straight away so as to reduce the overhead to a minimum, and/or (b) enable to use a global flag variable to indicate whether the uploader should fire.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File upload widget, Unable to get file content - OutSystems
Hello, I'm trying to upload the files to database using file upload widget. I add the widgets, upload the files from webpage, ...
Read more >
Why won't my file upload into the file widget?
If you're having trouble uploading a file into the file widget, the problem is most likely the file name. File names cannot include...
Read more >
DevExtreme file upload does not upload multiple files if you ...
Hi, I have a DevExtreme file upload widget on my page (markup ... what steps should be performed to reproduce the problematic behavior?...
Read more >
File Upload Widget not working after KNIME Server upgrade
We are currently investigating a bug that occurs in KNIME 4.4. 2, where default values of widgets may not be correctly overridden (internal ......
Read more >
dojox.form.Uploader — The Dojo Toolkit - Reference Guide
The widget hides the actual uploader and substitutes a dijit.form.Button, so that the file input matches the rest of the user interface. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found